Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why does C standard allow this declaration

Reply
Thread Tools

why does C standard allow this declaration

 
 
sunny
Guest
Posts: n/a
 
      09-20-2006
Hi

Why does C allows declaration of variable inside switch block.
ex: foll prg does not gives "undeclared "b" error msg. but also does
not initialize b to 20
int a=1;
switch(a)
{
int b=20;
case 1: printf("b is %d\n",b);
break;
defaultrintf("b is %d\n",b);
break;
}

this prints some garbage.
( yeah even though i got warning msg "unreachable code int b = 20" why
is it declaring inside switch block but not initializing b)

 
Reply With Quote
 
 
 
 
Chris McDonald
Guest
Posts: n/a
 
      09-20-2006
"sunny" <(E-Mail Removed)> writes:

>Why does C allows declaration of variable inside switch block.
>ex: foll prg does not gives "undeclared "b" error msg. but also does
>not initialize b to 20
> int a=1;
> switch(a)
> {
>int b=20;
> case 1: printf("b is %d\n",b);
> break;
> defaultrintf("b is %d\n",b);
> break;
> }


>this prints some garbage.
>( yeah even though i got warning msg "unreachable code int b = 20" why
>is it declaring inside switch block but not initializing b)



Programs do not give warnings about themselves;
different compilers may give different warnings and errors.

You will probably be horrified by Duff's Device:

http://www.lysator.liu.se/c/duffs-de...l#duffs-device
http://www.catb.org/jargon/html/D/Duffs-device.html

--
Chris,
 
Reply With Quote
 
 
 
 
Tejas Kokje
Guest
Posts: n/a
 
      09-20-2006
sunny wrote:
> Hi
>
> Why does C allows declaration of variable inside switch block.
> ex: foll prg does not gives "undeclared "b" error msg. but also does
> not initialize b to 20
> int a=1;
> switch(a)
> {
> int b=20;
> case 1: printf("b is %d\n",b);
> break;
> defaultrintf("b is %d\n",b);
> break;
> }
>
> this prints some garbage.
> ( yeah even though i got warning msg "unreachable code int b = 20" why
> is it declaring inside switch block but not initializing b)



It depends on your compiler. I am using gcc and it throws following
warning message

[kokje@RocketFuel ~]$ gcc test.c -o test
test.c: In function `main':
test.c:7: warning: unreachable code at beginning of switch statement

What compiler are you using ?

Tejas Kokje

 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-20-2006

sunny wrote:
> Hi
>
> Why does C allows declaration of variable inside switch block.
> ex: foll prg does not gives "undeclared "b" error msg. but also does
> not initialize b to 20
> int a=1;
> switch(a)
> {
> int b=20;
> case 1: printf("b is %d\n",b);
> break;
> defaultrintf("b is %d\n",b);
> break;
> }



this is an unfortunate side efect of allowing declarations at the
beginning of any block, and the switch statement requiring a block, or
at least to be useful;

In general it's not fair to look for extreme sense in C, it was just
supposed to be a pragmatic, ad-hoc language for use by some genius
coders at Bell Labs, 30+ years ago, for hecks sake!

 
Reply With Quote
 
sunny
Guest
Posts: n/a
 
      09-20-2006
i am using gcc available with Cygwin

Tejas Kokje wrote:

> sunny wrote:
> > Hi
> >
> > Why does C allows declaration of variable inside switch block.
> > ex: foll prg does not gives "undeclared "b" error msg. but also does
> > not initialize b to 20
> > int a=1;
> > switch(a)
> > {
> > int b=20;
> > case 1: printf("b is %d\n",b);
> > break;
> > defaultrintf("b is %d\n",b);
> > break;
> > }
> >
> > this prints some garbage.
> > ( yeah even though i got warning msg "unreachable code int b = 20" why
> > is it declaring inside switch block but not initializing b)

>
>
> It depends on your compiler. I am using gcc and it throws following
> warning message
>
> [kokje@RocketFuel ~]$ gcc test.c -o test
> test.c: In function `main':
> test.c:7: warning: unreachable code at beginning of switch statement
>
> What compiler are you using ?
>
> Tejas Kokje


 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      09-20-2006

sunny wrote:

> Hi
>
> Why does C allows declaration of variable inside switch block.


because it allows declaration of variables inside any braced block.

> ex: foll prg does not gives "undeclared "b" error msg. but also does
> not initialize b to 20
> int a=1;
> switch(a)
> {
> int b=20;
> case 1: printf("b is %d\n",b);
> break;
> defaultrintf("b is %d\n",b);
> break;
> }
>
> this prints some garbage.
> ( yeah even though i got warning msg "unreachable code int b = 20" why
> is it declaring inside switch block but not initializing b)


automatic variables (of which b is an example) are initialized as if by
an assignment (I'm talking casually here).

So it's as if the block starts with :-
{
int b; /* declaration */
b = 20; /* initialization */
case 1: <etc>

But in a switch statement only the operations following "case" or
"default" are executed. Hence the variable b is declared, but not
initialized.

 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      09-20-2006
Chris McDonald wrote:
> You will probably be horrified by Duff's Device:


I'm not so much horrified as puzzled by the location of the while.
Is it the equivalent of a goto into a do loop?
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      09-20-2006

sunny wrote:

> Hi
>
> Why does C allows declaration of variable inside switch block.


I went back to my dog-eared copy of K & R (1st edition).

On P.203 it says "Declarations may appear at the head of [the subject
of a switch], but initializations of automatic and register variables
are ineffective".

So one answer to "why?" is "because that's what K & R said".

 
Reply With Quote
 
A. Bolmarcich
Guest
Posts: n/a
 
      09-20-2006
On 2006-09-20, sunny <(E-Mail Removed)> wrote:
> Hi
>
> Why does C allows declaration of variable inside switch block.
> ex: foll prg does not gives "undeclared "b" error msg. but also does
> not initialize b to 20
> int a=1;
> switch(a)
> {
> int b=20;
> case 1: printf("b is %d\n",b);
> break;
> defaultrintf("b is %d\n",b);
> break;
> }
>
> this prints some garbage.
> ( yeah even though i got warning msg "unreachable code int b = 20" why
> is it declaring inside switch block but not initializing b)


As the warning message stated, the initialization is unreachable code.
According to the specification:

Storage is guaranteed to be reserved for a new instance of such an
object on each normal entry into the block with which it is associated,
or on a jump from outside the block to a labeled statement in the
block or in an enclosed block. If any initialization is specified for
the value stored in the object, it is performed on each normal entry,
but not if the block is entered by a jump to a labeled statement.

The compiler is conforming to the specification, and it is giving a
warning about the initialization not being performed.
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      09-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> sunny wrote:
>
> > Hi
> >
> > Why does C allows declaration of variable inside switch block.

>
> because it allows declaration of variables inside any braced block.


Old C doesn't allow variables to be declared everywhere, right?

> > ex: foll prg does not gives "undeclared "b" error msg. but also does
> > not initialize b to 20
> > int a=1;
> > switch(a)
> > {
> > int b=20;
> > case 1: printf("b is %d\n",b);
> > break;
> > defaultrintf("b is %d\n",b);
> > break;
> > }
> >
> > this prints some garbage.
> > ( yeah even though i got warning msg "unreachable code int b = 20" why
> > is it declaring inside switch block but not initializing b)

>
> automatic variables (of which b is an example) are initialized as if by
> an assignment (I'm talking casually here).
>
> So it's as if the block starts with :-
> {
> int b; /* declaration */
> b = 20; /* initialization */
> case 1: <etc>
>
> But in a switch statement only the operations following "case" or
> "default" are executed. Hence the variable b is declared, but not
> initialized.


Your explanation seems reasonable. The control flow goes to "int b;"
and "b" is defined. It also goes to "b = 20;", why doesn't the
assignment take effect?

What's the difference between a declaration and a statement? When does
the declaration is executed and the variable "b" is defined? Why does
the assignment statement above is skipped but the declaration is not?

Thank you.

 
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
A Java "interface" declaration does not allow a constructor to be specified in it ... Casey Hawthorne Java 12 01-19-2010 02:37 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Does XP Home Editon allow receive fax via standard home phoneline? Move the topic up. CJ Computer Support 2 10-16-2006 05:36 AM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM



Advertisments