Extra semicolons in C++.

Jason Cipriani jason.cipriani@gmail.com
Thu Jan 24 18:21:00 GMT 2008


Oh, I think I can answer my own question. I guess it was pretty
stupid, it's been bugging me forever but I guess I just needed to type
it out: Only extra semicolons outside of -functions- are illegal in C
and C++, right? But empty statements themselves are no problem at all
in either language?

Thanks, and sorry about that,
Jason

On Jan 24, 2008 2:26 AM, Jason Cipriani <jason.cipriani@gmail.com> wrote:
> Hello all,
>
> I have been wondering this for a really long time now, since it was
> changed in GCC. In a C source file compiled with -pedantic, an extra
> semicolon would produce the following error, of course:
>
> test.c:9: warning: ISO C does not allow extra `;' outside of a function
>
> I'm assuming this is for the reason it says: because ISO C doesn't
> allow extra semicolons.
>
> What I've always wondered is, why does compiling a C++ file with g++
> -pedantic produce errors about extra semicolons as well?
>
> test.cpp:9: error: extra `;'
>
> I though that extra semicolons were perfectly valid in C++. Is this
> not true? Somebody on another mailing list recently pointed out to me
> (and this is what brought up this question), that the C++ standard
> says:
>
>    statement:
> expression-statement
> compound-statement
> ...
>
>    expression-statement:
> expression_opt ;
>
>    compound-statement:
> { statement-seq_opt }
>
> And the _opt items are optional. So a single semicolon and nothing
> else is a valid "expression statement" and therefore a valid
> "statement". Is something being interpreted wrong here?
>
> Thanks!
> Jason
>
> P.S. Here is the test program:
>
> int main (int argc, char **argv) {
>
>   if (0) {
>   } else {
>   };
>
>   return 0;;
>
> };
>



More information about the Gcc-help mailing list