473,669 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch() parsing insanity

#include <stdio.h>

int main() {
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");
};
return 0;
}

When I try to compile the above code with gcc 3.3.4 on linux, I get the
following error:

bug.c: In function `main':
bug.c:6: error: parse error before "int"

Placing a statement such as printf, or even just a plain empty statement
(";") before "int msglen = 22;" makes it compile and work fine. Is
there some rule forbidding declaring variables directly after case
statements, or could it possibly be a bug with gcc? I'm not too keen to
try updating gcc since compiling kernels is apparently very touchy with
compiler versions.

Thanks in advance,
Robert
Nov 15 '05 #1
9 1569
Robert <wi******@nobod y.com> wrote:

[snipped at places]
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");

bug.c: In function `main':
bug.c:6: error: parse error before "int"

Placing a statement such as printf, or even just a plain empty statement
(";") before "int msglen = 22;" makes it compile and work fine. Is
there some rule forbidding declaring variables directly after case
statements, or could it possibly be a bug with gcc? I'm not too keen to


Syntax for labeles. Gcc is right.

Google clc for:
Subject: C99 mixed declarations / switch case / weird syntax behavior
Message-ID: <3h************ @individual.net >

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #2
Robert wrote:
#include <stdio.h>

int main() {
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");
};
return 0;
}

When I try to compile the above code with gcc 3.3.4 on linux, I get the
following error:

bug.c: In function `main':
bug.c:6: error: parse error before "int"

Placing a statement such as printf, or even just a plain empty statement
(";") before "int msglen = 22;" makes it compile and work fine. Is
there some rule forbidding declaring variables directly after case
statements, or could it possibly be a bug with gcc? I'm not too keen to
try updating gcc since compiling kernels is apparently very touchy with
compiler versions.


Labels precede statements; `int msglen = 22;' is not a
statement but a declaration. One way to solve your immediate
problem is to attach the label to a null statement

case 29: ;
int msglen = 22;
...

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
"S.Tobias" wrote:
Robert <wi******@nobod y.com> wrote:

[snipped at places]
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");

bug.c: In function `main':
bug.c:6: error: parse error before "int"

Placing a statement such as printf, or even just a plain empty
statement (";") before "int msglen = 22;" makes it compile and
work fine. Is there some rule forbidding declaring variables
directly after case statements, or could it possibly be a bug
with gcc? I'm not too keen to
Syntax for labeles. Gcc is right.

.... snip ...
From N869. Note the very last line.


6.2.1 Scopes of identifiers

.... snip ...

[#3] A label name is the only kind of identifier that has
function scope. It can be used (in a goto statement)
anywhere in the function in which it appears, and is
declared implicitly by its syntactic appearance (followed by
a : and a statement). *

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 15 '05 #4
CBFalconer wrote:
6.2.1 Scopes of identifiers

... snip ...

[#3] A label name is the only kind of identifier that has
function scope. It can be used (in a goto statement)
anywhere in the function in which it appears, and is
declared implicitly by its syntactic appearance (followed by
a : and a statement). *

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!


What about the following? This is what I generally do when I use
variables with that scope.

switch ( 29 )
{
case 29:
{
int msglen = 22;
printf("Hello\n ");
}
}

Nov 15 '05 #5


Chris Hulbert wrote:
CBFalconer wrote:
6.2.1 Scopes of identifiers

... snip ...

[#3] A label name is the only kind of identifier that has
function scope. It can be used (in a goto statement)
anywhere in the function in which it appears, and is
declared implicitly by its syntactic appearance (followed by
a : and a statement). *

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

What about the following? This is what I generally do when I use
variables with that scope.

switch ( 29 )
{
case 29:
{
int msglen = 22;
printf("Hello\n ");
}
}


That's fine: a brace-enclosed { block } is a compound
statement, which is a statement, so it can be labelled.
Prior to C99, this was the only way to declare new variables
"in medias res."

--
Er*********@sun .com

Nov 15 '05 #6
CBFalconer <cb********@yah oo.com> wrote:
"S.Tobias" wrote:
Robert <wi******@nobod y.com> wrote:

[snipped at places]
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");

bug.c: In function `main':
bug.c:6: error: parse error before "int"
[snip]
Syntax for labeles. Gcc is right.

... snip ...
From N869. Note the very last line.


6.2.1 Scopes of identifiers

[snip]

Yeah, right, my sloppy writing. I should've said "Syntax for
labeled-statements". But "case ??:" and "default:" things
are called labels, too.

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #7
Robert <wi******@nobod y.com> wrote:
# #include <stdio.h>
#
# int main() {
# switch (29) {
# case 29:
# int msglen = 22;
# printf("Hello\n ");
# };
# return 0;
# }
#
# When I try to compile the above code with gcc 3.3.4 on linux, I get the
# following error:
#
# bug.c: In function `main':
# bug.c:6: error: parse error before "int"

It's not a good idea to put declarations after any labels. If you want
msglen available in other switch branches, you can declare it above the
switch. If you only want to use it in this branch, make the branch a
block
switch (29) {
case 29: {
int msglen = 22;
printf("Hello\n ");
}
}

If you want the compiler to recognise switch(29) case 29: is a no-op,
you're expecting too much.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 15 '05 #8
Robert wrote on 21/07/05 :
#include <stdio.h>

int main() {
switch (29) {
case 29:
int msglen = 22;
printf("Hello\n ");
};
return 0;
}

When I try to compile the above code with gcc 3.3.4 on linux, I get the
following error:

bug.c: In function `main':
bug.c:6: error: parse error before "int"


This is fine:

#include <stdio.h>

int main (void)
{
switch (29)
{
case 29:
{
int msglen = 22;
printf ("Hello\n");
}
}
return 0;
}

In C90, you need a block to define a local.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #9
> If you want the compiler to recognise switch(29) case 29: is a no-op,
you're expecting too much.


Haha, no. This was just my boiled-down code from the original 700 lines
exhibiting the bug.

Thanks all for the spot-on replies.

-- Robert
Nov 15 '05 #10

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

Similar topics

24
3158
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and their types. I would like to somehow store a reference to parsing operations in this array (such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc), so I can invoke the appropriate one without writing a long switch.
1
27221
by: Jim P. | last post by:
I have a client server set of apps that can connect through socets and send data back and forth. I'm trying to get it to send XML messages back and both. Currently it works as string data. I collect all of the incoming data to a string but when I try to parse the incoming XML I get the following message: ------------------------------------------- Error Parsing message: System.Xml.Exception: There is no Unicode byte order mark. ...
8
2026
by: _eddie | last post by:
Is there a good way to code a switch/case-type construct for maximal speed? The goal is to parse text key/value pairs. IOW: // key = "Text of some kind" // value = "Value Text" string target1; string target2; switch (key) {
5
3919
by: _DS | last post by:
I'm currently using a switch with about 50 case statements in a stretch of code that's parsing XML attributes. Each case is a string. I'm told that switch statements will actually use hash tables when number of cases is around 10 or more, so I haven't changed the code. Just wondering if anyone knows about how that's structured internally. It does seem a bit slow.
11
3152
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test 1"); break;
21
52309
by: markpapadakis | last post by:
I was checking out the C-FAQ and read here ( http://c-faq.com/misc/nonconstcase.html ) that: " case labels are limited to single, constant, integral expression ". However, I have been using case with ranges for a long while ( gcc, VC++) so either the FAQ calls for an update or those two compilers provide this functionality as an extension. example; switch (a)
21
7720
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
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
7407
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...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
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.