role of semicolon

M

Merrill & Michele

#include <stdio.h>
int main(void)
{
int i = 0;
++ i; ++ i;
printf("i equals %d\n", i);
return 0;
}

I've been trying to determine the great and manifold uses of the semicolon
in c. It's not such an easy thing. Q1) Is the above prog conforming? My
compiler likes it just fine. Q2) If so, how many statements are in main?
MPJ
 
A

Alex Fraser

Merrill & Michele said:
#include <stdio.h>
int main(void)
{
int i = 0;
++ i; ++ i;
printf("i equals %d\n", i);
return 0;
}

I've been trying to determine the great and manifold uses of the
semicolon in c. It's not such an easy thing. Q1) Is the above prog
conforming? My compiler likes it just fine.
Yes.

Q2) If so, how many statements are in main?

In terms of the language grammar, there are four statements inside main: the
two pre-increments and the call to printf() (which are all
expression-statements), plus the return (which is a jump-statement). "int i
= 0;" is a declaration, not a statement, despite the fact that code will
normally be generated due to the presence of an initialiser.

Part of the definition of main is the compound-statement, from the '{' to
the '}' inclusive. A compound-statement is a statement, so if you count that
too, then the answer is five.

Again according to the language grammar, uses of semi-colon seem to be:
- terminating declarations,
- terminating expression-statements (expression statements may consist of
only the terminating ';'),
- terminating do..while loops (a type of iteration-statement),
- seperating the three expressions at the top of a for loop (another type
of iteration-statement), and
- terminating jump-statements (goto, continue, break, return).

Open to correction...

Alex
 
M

Merrill & Michele

Alex Fraser said:
In terms of the language grammar, there are four statements inside main: the
two pre-increments and the call to printf() (which are all
expression-statements), plus the return (which is a jump-statement). "int i
= 0;" is a declaration, not a statement, despite the fact that code will
normally be generated due to the presence of an initialiser.

Part of the definition of main is the compound-statement, from the '{' to
the '}' inclusive. A compound-statement is a statement, so if you count that
too, then the answer is five.

Again according to the language grammar, uses of semi-colon seem to be:
- terminating declarations,
- terminating expression-statements (expression statements may consist of
only the terminating ';'),
- terminating do..while loops (a type of iteration-statement),
- seperating the three expressions at the top of a for loop (another type
of iteration-statement), and
- terminating jump-statements (goto, continue, break, return).

Open to correction...

Thank you for the generous detail. The only germane correction I saw is
that semicolon is spelled without a hyphen. MPJ
 
L

Lawrence Kirby

On Fri, 17 Dec 2004 14:37:53 +0000, Alex Fraser wrote:

....
Part of the definition of main is the compound-statement, from the '{' to
the '}' inclusive. A compound-statement is a statement, so if you count that
too, then the answer is five.

Interestingly this is an example (and the only one) of when a compound
statement ISN'T a statement. Check the grammar.

Lawrence
 
M

Merrill & Michele

"Lawrence Kirby"

Interestingly this is an example (and the only one) of when a compound
statement ISN'T a statement. Check the grammar.

[uses cordless keyboard+mouse for 1st time and wonders how he endured life
before]

I believe Mr. Kirby refers to a statement such as:
i = 6; ++j;
and calls this a compound statement. I can speculate on how many statements
it is and know that I could get it right if I got two swings at the pitch.
I will drag this forum wildly OT if I start into English grammar and its
inability to express truth about number, so which is it? MPJ
 
M

Mike Wahler

Merrill & Michele said:
#include <stdio.h>
int main(void)
{
int i = 0;
++ i; ++ i;
printf("i equals %d\n", i);
return 0;
}

I've been trying to determine the great and manifold uses of the semicolon
in c.

There's only *one* use. To terminate a statement.
It's not such an easy thing.

Sure it is. It's a syntactical element that has only
one meaning, unlike some others whose meaning depends
upon context (e.g. the '&' character).
Q1) Is the above prog conforming?
Yes.

My
compiler likes it just fine. Q2) If so, how many statements are in main?

Five.

-Mike
 
M

Mike Wahler

Alex Fraser said:
In terms of the language grammar, there are four statements inside main: the
two pre-increments and the call to printf() (which are all
expression-statements), plus the return (which is a jump-statement). "int i
= 0;" is a declaration, not a statement,

A declaration is a statement.

despite the fact that code will
normally be generated due to the presence of an initialiser.

Part of the definition of main is the compound-statement, from the '{' to
the '}' inclusive.

Ah, I forgot about that.
A compound-statement is a statement, so if you count that
too, then the answer is five.

Then my answer would be six. :)

-Mike
 
K

Keith Thompson

Merrill & Michele said:
Lawrence said:
Interestingly this is an example (and the only one) of when a compound
statement ISN'T a statement. Check the grammar.
[snip]

I believe Mr. Kirby refers to a statement such as:
i = 6; ++j;
and calls this a compound statement. I can speculate on how many statements
it is and know that I could get it right if I got two swings at the pitch.
I will drag this forum wildly OT if I start into English grammar and its
inability to express truth about number, so which is it? MPJ

(Malformed attribution lines for Lawrence Kirby and Alex Fraser fixed.
"Merrill & Michele", whatever you're doing that creates this problem,
*please* stop doing it. I've noticed that some of your posts have
this problem and some don't. If there are actually two different
people posting from your account, the one who gets this right should
educate the other one.)

No, he's not referring to "i = 6; ++j;". That's not a compound
statement, it's just two statements on one line. It should be clear
from the context that he's referring to the compound-statement from
the '{' to the '}' inclusive.

In most contexts in C source, an end-of-line is just like any other
whitespace. (String literals and macro definitions are the major
exceptions.) There's no grammatical significance to the fact that
both statements are on one line.

If you look at the C grammar, a "compound-statement" consists of a
'{' delimiter, followed by a sequence of zero or more declarations or
statements, followed by a '}' delimiter. (In C90, declarations must
precede statements; in C99, they can be mixed.) The line "i = 6; ++j;"
is not a compound statement because it's not surrounded by '{' and '}'
delimiters.

In general, a compound-statement can appear wherever a statement can
appear. In addition, a compound-statement appears as part of a
function-definition; in that context, according to the grammar, the
compound-statement is not a statement.

The grammar for a function-definition is:

function-definition:
declaration-specifiers declarator declaration-list(opt) compound-statement

(the optional declaration-list is for an old-style non-prototyped
definition). This is the only place in the grammar that allows
(actually requires) a compound-statement without allowing any other
kind of statement.
 
M

Mike Wahler

Merrill & Michele said:
"Lawrence Kirby"
count
that

Interestingly this is an example (and the only one) of when a compound
statement ISN'T a statement. Check the grammar.

[uses cordless keyboard+mouse for 1st time and wonders how he endured life
before]

I believe Mr. Kirby refers to a statement such as:

I'm absolutely sure what he's referring to, but
I think he means that the { and } which delimit
a function body do not comprise a 'compound statement'.
(So I believe my 'didn't think of that' remark elsethread should
be retracted. :))
i = 6; ++j;
and calls this a compound statement.

No, those are *two* statements (on one line).
AFAIK, a 'compound statement' begins with { and
ends with } (but a function body does not qualify
if I understand Mr. Kirby.)
I can speculate on how many statements
it is and know that I could get it right if I got two swings at the pitch.

Most batters get three. :)
I will drag this forum wildly OT if I start into English grammar and its
inability to express truth about number, so which is it? MPJ

Two. Or to. Or too. Take your pick. :)

-Mike
 
K

Keith Thompson

Mike Wahler said:
A declaration is a statement.

No, it isn't, according to the grammar in the standard. A "statement"
can be any of the following:

labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
 
K

Keith Thompson

Mike Wahler said:
I'm absolutely sure what he's referring to, but
I think he means that the { and } which delimit
a function body do not comprise a 'compound statement'.
(So I believe my 'didn't think of that' remark elsethread should
be retracted. :))

Actually, according to the grammar, it is a "compound-statement", but
it's not a "statement".

A "function-definition" consists of:

declaration-specifiers declarator declaration-list(opt) compound-statement

This is the only occurrence of "compound-statement" in the grammar
that's not a "statement".

Note that if the grammar required a "statement" here rather than a
"compound-statement", the following would be legal:

int foo(void)
return 42; /* not legal C */

Instead, we have to write:

int foo(void)
{
return 42;
}

The claim that this "compound-statement" is not a "statement" is based
on a painfully literal reading of the grammar. If you want to argue
that a compound statement must be a statement (by the rules of
ordinary English grammar), I won't disagree too strongly.
 
E

E. Robert Tisdale

Keith said:
Mike Wahler writes:
[...]
A declaration is a statement.


No, it isn't, according to the grammar in the standard.
A "statement" can be any of the following:

labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement

Honestly Keith,

You remind me of Beetle Bailey's friend Zero.
You take the statements in the standard
just a little too literally.

According to
the American Heritage Dictionary of the English language

http://www.bartleby.com/61/

"declaration
1. An explicit, formal announcement, either oral or written.
2. The act or process of declaring.
3. A statement of taxable goods or of properties subject to duty.
4. Law
a. A formal statement by a plaintiff
specifying the facts and circumstances
constituting his or her cause of action.
b. An unsworn statement of facts that is admissible as evidence.
5. Games A bid,
especially the final bid of a hand in certain card games."
 
M

Mike Wahler

Keith Thompson said:
No, it isn't, according to the grammar in the standard. A "statement"
can be any of the following:

labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement

I stand corrected.

-Mike
 
D

Dik T. Winter

> The claim that this "compound-statement" is not a "statement" is based
> on a painfully literal reading of the grammar.

Depends on which way you wish to go in the grammar. A compound-statement
is a specific form of a statement (and has been so since the introduction
of the term in Algol 60). So, by all means, the function body is a
statement of particular form (and that was not the case in Algol 60).
In my opinion when Lawrence wrote that that particular compound-statement
was not a statement, he was wrong.
 
M

Merrill & Michele

"Mike Wahler"
"Merrill & Michele"

There's only *one* use. To terminate a statement.


Sure it is. It's a syntactical element that has only
one meaning, unlike some others whose meaning depends
upon context (e.g. the '&' character).
main?

Five.

Along the same lines, the following code compiles for me:

static int doNothing1(void)
{
return 0;
}
int main(void)
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
void doNothing2(void);

doNothing1();
doNothing2();

return 0;
}

void doNothing2(void)
{
}

Q1) Does this prog conform?
Q2) If yes, then how many statements are in each of the 3 functions?
[OT to Mr. Thompson] Q3) Are the attribution arrows correct? MPJ
 
P

pete

Dik said:
So, by all means, the function body is a
statement of particular form (and that was not the case in Algol 60).
In my opinion when Lawrence wrote that that particular
compound-statement was not a statement, he was wrong.

According to K&R2, A9.3 Compound Statement, page 222:
"The body of a function definition is a compound statement."
 
P

pete

pete said:
According to K&R2, A9.3 Compound Statement, page 222:
"The body of a function definition is a compound statement."

I don't think that what I posted
was relevant to what you were discussing. Sorry about that.
 
M

Mike Wahler

Merrill & Michele said:
"Mike Wahler"

Along the same lines, the following code compiles for me:

static int doNothing1(void)
{
return 0;
}
int main(void)
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
void doNothing2(void);

doNothing1();
doNothing2();

return 0;
}

void doNothing2(void)
{
}

Q1) Does this prog conform?
Yes.

Q2) If yes, then how many statements are in each of the 3 functions?

If Keith T's assertion is correct (and I believe it is),
that declarations aren't statements, then:

doNothing1() contains one statement.
doNothing2() contains no statements.
main() contains three statements.

-Mike
 
M

Mike Wahler

Mike Wahler said:
If Keith T's assertion is correct (and I believe it is),
that declarations aren't statements, then:

doNothing1() contains one statement.
doNothing2() contains no statements.
main() contains three statements.

And if you want to include 'compound statement' and
a function body qualifies as one, then add one to
each of the above.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top