Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: C99 mixed declarations / switch case / weird syntax behavior

Reply
Thread Tools

Re: C99 mixed declarations / switch case / weird syntax behavior

 
 
Michael Mair
Guest
Posts: n/a
 
      06-13-2005
This is a C thing, not a compiler problem:

Xpost and f'up2 comp.lang.c

Didier Verna wrote:
> Hi !
>
> GCC (3.3.6) has a weird syntax behavior WRT C99 mixed declarations in switch /
> case statements (sometimes it requires a brace to open a block, sometimes
> not). This is demonstrated by the following example:
>
> ------------------------------------------------------------------------
>
> typedef enum
> {
> CASE_1,
> CASE_2
> } enum_e;
>
> void foo ()
> {
> enum_e case_1 = CASE_1;
>
> switch (case_1)
> {
> case CASE_1:
> /* int i; */ /* This leads to a parse error. */
> foo ();
> break;
> case CASE_2:
> foo ();
> int i; /* This is OK. */
> foo ();
> break;
> }
> }
>
> ------------------------------------------------------------------------
>
> Any comment on this ?


Yep; a case-label may precede only a statement (C99, 6.8.1#1).
A statement in turn is either a labeled statement (i.e. case/default or
a label for goto followed by a statement), or a
selection/jump/iteration/expression statement, or a compound statement
(i.e. { block-item-list } where a block-item may be a declaration or a
statement) -- but no declaration.

Notes:
-You cannot do
if(foo == 42)
int i;
either.
-The fact that "int i;" is allowed _after_ foo(); is due to the
fact that switch(case_1) is followed by a compound statement.
The block-items preceded by a label have to be statements, the
others can be either statements or declarations.
-This of course means that
switch (a)
{
int i;
case 1:
i=47;
bar();
break;
case 2;
for (i=0; i<3; i++)
bar();
break;
}
is possible (but an initializer of i has no effect whatsoever, and a
forgotten initialisation of i for a certain case leads to undefined
behaviour). Furthermore,
case 1:
foo();
int i;
break;
case 2:
bar();
int i;
break;
means that you have a re-declaration of i after bar().
- A further educative example is Duff's device:
http://www.lysator.liu.se/c/duffs-device.html

So, yes, gcc is right in _this_ case. Note, however, that gcc is
not fully C99 compliant.

The c.l.c people probably will find lots to correct or expand on
but this should get you started.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      06-14-2005
In comp.lang.c Michael Mair <(E-Mail Removed)> wrote:
> Didier Verna wrote:


> > switch (case_1)
> > {
> > case CASE_1:
> > /* int i; */ /* This leads to a parse error. */
> > foo ();
> > break;
> > case CASE_2:
> > foo ();
> > int i; /* This is OK. */
> > foo ();
> > break;
> > }


> Yep; a case-label may precede only a statement (C99, 6.8.1#1).

[sniped what a statement was]

> Notes:

[snipped notes]

There's a similar pit-fall in situations like this:

switch (something)
{
case 1:
/*code*/
default:
}

void f()
{
if (do_nothing)
goto end;
/* do_something code */
end:
}

All require and empty statement (";") to be fixed.
For example, OP's code could be fixed by:

case CASE_1:
;
int i; /* Now should be correct. */
foo ();
break;

[If someone asked why I put "default:" at the end, where no code
follows, the answer is simply to silence warnings. ]

--
Stan Tobias
mailx `echo http://www.velocityreviews.com/forums/(E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixed declarations and "code" Keith Thompson C Programming 3 05-08-2011 08:11 AM
mixed declarations and code (and size_t)? Zach C Programming 7 11-15-2010 04:39 PM
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
C99 struct initialization (C99/gcc) jilerner@yahoo.com C Programming 3 02-20-2006 04:41 AM
C99: variable declarations inside switch statements Neil Zanella C Programming 5 10-28-2003 11:04 PM



Advertisments