Simple question regarding the use of ;

A

Andrew

Hello,
I have been doing some microprocessor programming, and I have run into
some variations in the way people use the semi-colon. Please tell me
the difference between these two statements.

1. while(1)
{
value = read_adc(1);
}

2. while(1)
{
value = read_adc(1);
};

Is the semi-colon on statement 2 doing anything? The code seems to
run the same both ways. I just don't understand the purpose of using
a semi-colon where this is no statement. Please explain.

thanks,
Andy
 
E

Eric Sosman

pete said:
No.
The semicolon after the loop is referred to as an "empty statement".


The extra semicolon
mostly looks like an inconsequential typographical error.

"Almost" inconsequential. Precede the fragment with
`if (expression)' and follow it with `else' and a consequence
will become apparent. Even so, I tend to agree with your
diagnosis: it looks like a silly error, one that HHGTTG might
describe as "mostly harmless."
 
G

Guest

No.
The semicolon after the loop is referred to as an "empty statement".

the technical term is "null statement" which is actually an empty
expression.

It sometimes serves a purpose, eg.

while (p = p->next)
;
The extra semicolon
mostly looks like an inconsequential typographical error.

but in the example it just looks like someone
doesn't know C very well.
 
F

Flash Gordon

Andrew said:
Hello,
I have been doing some microprocessor programming, and I have run into
some variations in the way people use the semi-colon. Please tell me
the difference between these two statements.

1. while(1)
{
value = read_adc(1);
}

This loops forever calling read_adc
2. while(1)
{
value = read_adc(1);
};

This loops forever calling read_adc and once it has finished it does
nothing once...
Is the semi-colon on statement 2 doing anything? The code seems to
run the same both ways. I just don't understand the purpose of using
a semi-colon where this is no statement. Please explain.

You can have a null statement in C. In some assembler languages you have
an instruction "NOP" which does nothing but can be extremely useful
(overrite a siction of code you don't want to run with NOPs, for
example), this is the C equivilent but, in my opinion, it is far less
useful, especially in this case. The main reason it would be used in
cade like you have shown is that it was written by someone more used to
some other language where you might do
WHILE (some_condition) DO BEGIN
value := read_adc(1)
END;
more code

In the language I'm thinking of, without more code (i.e. if you then hit
the end of the function) you would not need that semicolon either, but a
lot of people in my experience put it in because they don't understand
that language properly either!
 
M

Moi

the technical term is "null statement" which is actually an empty
expression.

It sometimes serves a purpose, eg.

while (p = p->next)
;

Personally, I prefer:
while (p = p->next) {;}
, but that is a matter of style.

but in the example it just looks like someone doesn't know C very well.

It could well be a Pascal-ism.

"Transaction Processing" by Gray & Reuter did it in the code snippets
(that merely served as pseudocode).

HTH,
AvK
 
K

Keith Thompson

the technical term is "null statement" which is actually an empty
expression.
[...]

Quibble: There's no such thing as an empty expression.

The grammar for "expression-statement" is

expression-statement:
expression[opt] ;

Personally, I think it would have made more sense to define it like this:

expression-statement:
expression ;

null-statement:
;

But it's not a huge deal; either form defines exactly the same syntax.
 
B

Bartc

Andrew said:
Hello,
I have been doing some microprocessor programming, and I have run into
some variations in the way people use the semi-colon. Please tell me
the difference between these two statements.

1. while(1)
{
value = read_adc(1);
}

2. while(1)
{
value = read_adc(1);
};

Is the semi-colon on statement 2 doing anything? The code seems to
run the same both ways. I just don't understand the purpose of using
a semi-colon where this is no statement. Please explain.

C requires a semicolon as a statement terminator. Except apparently on a
block statement {...} like this one.

Often it doesn't matter if it's there or not (because it's seen as as empty
statement), but here it does matter:

if (...) {
};
else
 
K

Keith Thompson

Bartc said:
C requires a semicolon as a statement terminator. Except apparently on
a block statement {...} like this one.
[...]

More precisely, the terminating semicolon is part of the syntax of
certain kinds of statements, but not of others. For example, an if
statement isn't terminated by a semicolon:

if (condition) foo(42);

Here the semicolon is part of the expression statement, not directly
part of the if statement.

Looking at the grammar in C99 6.8, the following kinds of statements
are terminated by semicolons:

expression-opt ;
do statement while ( expression ) ;
goto identifier ;
continue ;
break ;
return expression-opt ;

(And the for statement includes semicolons, but not as a terminator.)

The net effect is a line that isn't either the first line of a
compound statement or an opening or closing brace usually ends in a
semicolon.
 
S

Stephen Sprunk

Andrew said:
Hello,
I have been doing some microprocessor programming, and I have run into
some variations in the way people use the semi-colon. Please tell me
the difference between these two statements.

1. while(1)
{
value = read_adc(1);
}

2. while(1)
{
value = read_adc(1);
};

Is the semi-colon on statement 2 doing anything? The code seems to
run the same both ways. I just don't understand the purpose of using
a semi-colon where this is no statement. Please explain.

A semicolon terminates a simple statement, as in "value = read_adc(1);".
A compound statement is enclosed by '{' and '}' and contains zero or
more simple or compound statement; there is no need for a ';' to
terminate a compound statement, because the '}' does that.

If you put a ';' after a compound statement, it actually forms a new
(null) simple statement that has nothing to do with the preceding
compound statement. In your second example, that means "while (1) {
value = read_adc(1); }" and ";" are entirely separate.

S
 
M

matt

Incidentally, this is one reason statement-like macros are often
written as a do { ... } while ( 0 ) loop:

#define SWAP( x, y ) do { int t = x; x = y; y = t; } while ( 0 )

if ( foo )
SWAP( a, b );
else
SWAP( b, c );

If SWAP were written as a simple compound block, the else would fail:

#define SWAP( x, y ) { int t = x; x = y; y = t; }

if ( foo )
{ int t = a; a = b; b = t; } ; /* note semicolon */
else /* error: no preceding if */
{ int t = b; b = c; c = t; } ;

or the user would have remember to avoid putting a semicolon after
invoking SWAP.

Another option is that the user remembers to protect any macro with a {...}
block like this

if ( foo ) {
SWAP( a, b );
}
else {
SWAP( b, c );
}

The do...while(0) idiom avoids this

The do...while(0) idiom is a good way to solve this problem. However, are there
any other suitable solutions?
 
B

Bartc

A semicolon terminates a simple statement, as in "value = read_adc(1);". A
compound statement is enclosed by '{' and '}' and contains zero or more
simple or compound statement; there is no need for a ';' to terminate a
compound statement, because the '}' does that.

I'm not sure that was the reason. Otherwise you could say the last (simple)
statement in a {...} block doesn't need a terminating semicolon because
the } does that job too. Then the semicolon reverts to a statement
separator.

I think that {...} statements not needing a semicolon terminator is just
another oddity of C.
 
S

Stephen Sprunk

Bartc said:
I'm not sure that was the reason. Otherwise you could say the last
(simple) statement in a {...} block doesn't need a terminating semicolon
because the } does that job too. Then the semicolon reverts to a
statement separator.

I think that {...} statements not needing a semicolon terminator is just
another oddity of C.

The compound statement itself is terminated by the '}'; the last
statement _inside_ the compound statement needs its own terminator to be
complete.

S
 
C

CBFalconer

Andrew said:
.... snip ...

2. while(1) {
value = read_adc(1);
};

Is the semi-colon on statement 2 doing anything? The code seems
to run the same both ways. I just don't understand the purpose
of using a semi-colon where this is no statement. Please explain.

The semi terminates the null statement that starts after the '}'
character. That null statement is outside the while loop.
 
C

CBFalconer

Moi said:
nick_keighley_nospam wrote:
.... snip ...


Personally, I prefer:
while (p = p->next) {;}
, but that is a matter of style.

I prefer:
while (p = p->next) continue;
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top