473,703 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C99: variable declarations inside switch statements


Hello,

Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2 seems to reveal that there
are still places where this is illegal. For instance, consider the
following code snippet.

int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}

Here the compiler complains with the error:

hello.c: In function `main':
hello.c:6: parse error before "char"

It seems like declaring a variable inside a switch statement is illegal.
Anyone know why this is the case. After all, the following compiles fine
with gcc 3.2.2:

int main(void) {
int i = 0;
switch (i) {
case 0: {
char ch;
break;
}
}
}

The only difference is the introduction of the block within the C
statement. Anyone know of similar cases where it is illegal to declare
variable in places where we would expect it to be legal in C99? Also,
can anyone explain the reason for the compiler complaint?

Thanks,

Neil

Nov 13 '05 #1
5 10151
On Mon, 27 Oct 2003 00:13:06 -0330, Neil Zanella wrote:
Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2 seems to reveal that there
are still places where this is illegal. For instance, consider the
following code snippet.
You can't count on conformance of current versions of GCC
to the C99 standard. With that out of the way I'll continue.
int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}

Here the compiler complains with the error:

hello.c: In function `main':
hello.c:6: parse error before "char"

can anyone explain the reason for the compiler complaint?


The answer is in the C99 grammar. Declarations can appear at
any place within a block because of these productions:

A.2.3 Statements

(6.8.2) compound-statement: { block-item-list? }
(6.8.2) block-item-list: block-item
block-item-list block-item
(6.8.2) block-item: declaration
statement

But you can't put a declaration after a case construct because
of these productions:

A.2.3 Statements

(6.8) statement: labeled-statement
(6.8.1) labeled-statement: case constant-expression : statement

IOW, only a statement can follow a case.

-Sheldon
Nov 13 '05 #2
In article
<Pi************ *************** **********@garf ield.cs.mun.ca> ,
Neil Zanella <nz******@cs.mu n.ca> wrote:
Hello,

Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2 seems to reveal that there
are still places where this is illegal. For instance, consider the
following code snippet.

int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}

Here the compiler complains with the error:

hello.c: In function `main':
hello.c:6: parse error before "char"

It seems like declaring a variable inside a switch statement is illegal.
Anyone know why this is the case. After all, the following compiles fine
with gcc 3.2.2:


No, declaring a variable inside a switch statement is not illegal.
However, the syntax is slightly different then you think it is: When you
write

switch (i) {
<body of switch statement>
}

the body of the switch statement can contain the following in any order:

Unlabeled statements
Labeled statements
Declarations

case 0: char ch;

would be a labeled declaration, and they are not allowed. Write

case 0: ;
char ch;
break;

instead.
Nov 13 '05 #3
Hello,

Quite interesting. I wonder whether adding a rule to the C99 grammar for
accepting declarations following labeled statements would cause the
resulting grammar to be ambiguous (I doubt it), or whether there
would be some shift/reduce, reduce/shift, reduce/reduce conflicts
of some sort (more likely). How would such conflicts arise in
such a situation?

Thanks,

Neil

Sheldon Simms <sh**********@y ahoo.com> wrote:
The answer is in the C99 grammar. Declarations can appear at
any place within a block because of these productions:

A.2.3 Statements

(6.8.2) compound-statement: { block-item-list? }
(6.8.2) block-item-list: block-item
block-item-list block-item
(6.8.2) block-item: declaration
statement

But you can't put a declaration after a case construct because
of these productions:

A.2.3 Statements

(6.8) statement: labeled-statement
(6.8.1) labeled-statement: case constant-expression : statement

IOW, only a statement can follow a case.

-Sheldon

On Mon, 27 Oct 2003 00:13:06 -0330, Neil Zanella wrote:
int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}

Nov 13 '05 #4

top-posting corrected

On Tue, 28 Oct 2003 03:00:59 -0800, Neil Zanella wrote:

Sheldon Simms <sh**********@y ahoo.com> wrote:
On Mon, 27 Oct 2003 00:13:06 -0330, Neil Zanella wrote:
> int main(void) {
> int i = 0;
> switch (i) {
> case 0:
> char ch;
> break;
> }
> }


The answer is in the C99 grammar. Declarations can appear at
any place within a block because of these productions:

A.2.3 Statements

(6.8.2) compound-statement: { block-item-list? }
(6.8.2) block-item-list: block-item
block-item-list block-item
(6.8.2) block-item: declaration
statement

But you can't put a declaration after a case construct because
of these productions:

A.2.3 Statements

(6.8) statement: labeled-statement
(6.8.1) labeled-statement: case constant-expression : statement

IOW, only a statement can follow a case.

-Sheldon

Quite interesting. I wonder whether adding a rule to the C99 grammar for
accepting declarations following labeled statements would cause the
resulting grammar to be ambiguous (I doubt it), or whether there
would be some shift/reduce, reduce/shift, reduce/reduce conflicts
of some sort (more likely). How would such conflicts arise in
such a situation?


I don't know if the C standards committee worries too much about
those kinds of problems when writing the grammar. I wouldn't in any
case. The grammer in the standard isn't supposed to be directly
usable in bison.

FWIW C++ *does* allow declarations after case and other labels with
these productions:

ISO/IEC 14882:1998(E)
A.5
statement: labeled-statement
declaration-statement

labeled-statement: case constant-expression : statement

declaration-statement : block-declaration

where block-declaration is the kind of declaration that can occur
inside a block (i.e. - no function definitions, template declaration,
etc.)

-Sheldon

Nov 13 '05 #5
In article <b6************ **************@ posting.google. com>,
nz******@cs.mun .ca (Neil Zanella) wrote:
Hello,

Quite interesting. I wonder whether adding a rule to the C99 grammar for
accepting declarations following labeled statements would cause the
resulting grammar to be ambiguous (I doubt it), or whether there
would be some shift/reduce, reduce/shift, reduce/reduce conflicts
of some sort (more likely). How would such conflicts arise in
such a situation?


Unlikely that this could introduce any conflicts.

We assume that there is no conflict between unlabeled statement and
declaration (otherwise C has a big problem anyway). Labels are quite
trivial to recognise and to distinguish from anything else. Once the
label is recognised, you are left again with the task of distinguishing
between unlabeled statement and declaration.
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
6490
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
2
8831
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
134
7872
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
193
9571
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
8
74576
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
3
1701
by: albert.neu | last post by:
Hello! What is the difference between "library parts" of C99 and "language parts" of C99. see http://groups.google.at/group/microsoft.public.vc.language/browse_thread/thread/e9a67f0ff20a954b/bd2bada2bbdbce56?lnk=st&rnum=1#bd2bada2bbdbce56 I know that "Dinkum Compleat Libraries" (http://www.dinkumware.com/) support the "library parts" of C99 - this probably relates to the C99
4
2123
by: jaime | last post by:
Hi all. Apologies, since this is more a tool question, than strictly a language question, but hey, it seemed like an appropriate place to ask... I'm a c newbie (and have been now for about 6 years!) and I'd like to use an automatic tool to show me warnings about the following code: #include <stdio.h> int main(void){
14
13054
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
80
3040
by: jacob navia | last post by:
Several people in this group argue that standard C is not portable since there are no compilers for it, etc. I propose this program in Standard C, that I have compiled in several OSes to test if this is actually true. My basic idea is to see which systems do not have a compiler that supports standard C. The program is designed to produce the sum of the natural integers up to a user provider argument. For instance
0
8658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9096
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7842
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6583
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5919
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2419
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.