473,408 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Legal C or bug in gcc

By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?

B2003
Feb 27 '08 #1
23 1323
In article <a2**********************************@o77g2000hsf. googlegroups.com>,
Boltar <bo********@yahoo.co.ukwrote:
>By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?

B2003
Time for me to re-post my "I wrote this hello, world program and it
compiled and ran just fine; why?" post.

Feb 27 '08 #2
On 27 Feb, 09:44, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
Time for me to re-post my "I wrote this hello, world program and it
compiled and ran just fine; why?" post.
I should've known better than to expect a sensible answer from this
group.

Anyway , I just realised its treating the bracketed part as a seperate
block so you can save anymore sarcastic responses.

B2003
Feb 27 '08 #3
Boltar <bo********@yahoo.co.ukwrites:
By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?
It is entirely legal. A compound statement (braces containing other
statements) is just another valid option where a statement is
required.

--
Ben.
Feb 27 '08 #4
Boltar said:
By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?
It's legal.

{
a = 2;
}

is a compound statement. You can have these pretty much anywhere you can
have an ordinary statement. If you like, you can do this:

#include <stdio.h>
int main(void)
{
{ printf("Hello, world!\n"); }
{ puts("How are you today?"); }
{ puts("Earthquake in UK - film at 11"); }
{ return 0; }
}

Although the above is a pastiche, this facility is nevertheless useful and
powerful, but does have the unfortunate consequence you have noted -
forgetting an "else" doesn't necessarily give you the syntax error you'd
have hoped for!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 27 '08 #5
Boltar said:
On 27 Feb, 09:44, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
>Time for me to re-post my "I wrote this hello, world program and it
compiled and ran just fine; why?" post.

I should've known better than to expect a sensible answer from this
group.
Mr McCormack is a troll. You get them in any group.

I have already posted a sensible answer.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 27 '08 #6
Boltar wrote:
By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?
No this is legal C. A opening { starts a compound statement or block,
closed by the next }. It's sometimes useful for data confinement.

<OT>
There *is* a related GCC extension which allows a compound statement
enclosed in parenthesis as an expression, but this is not an
illustration of one. This is pure Standard C.
</OT>

Feb 27 '08 #7
In article <a2**********************************@o77g2000hsf. googlegroups.com>,
Boltar <bo********@yahoo.co.ukwrote:
>By accident I came across a bug like this in some Linux code I'd
>main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}
Perhaps you should run it through an indenting tool for enlightenment:

main()
{
int a;

if (1 == 1)
a = 1;

{
a = 2;
}

printf("a = %d\n",a);
}

-- Richard
--
:wq
Feb 27 '08 #8
Richard Heathfield wrote:
>
Boltar said:
By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?

It's legal.

{
a = 2;
}

is a compound statement. You can have these pretty much anywhere you can
have an ordinary statement. If you like, you can do this:

#include <stdio.h>
int main(void)
{
{ printf("Hello, world!\n"); }
{ puts("How are you today?"); }
{ puts("Earthquake in UK - film at 11"); }
{ return 0; }
}

Although the above is a pastiche, this facility is nevertheless useful and
powerful, but does have the unfortunate consequence you have noted -
forgetting an "else" doesn't necessarily give you the syntax error you'd
have hoped for!
In case the OP (or anyone else) is wondering where this would be
useful, consider:

#if ENABLE_SOME_FEATURE
if ( new_feature_is_enabled() )
{
do_it_the_new_way();
do_another_thing_the_new_way();
}
else
#endif
{
do_it_the_old_way();
do_the_other_thing_the_old_way();
}

Compare this "clean" version to how you would have to write it if
compound statements weren't legal on their own.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Feb 27 '08 #9
On 27 Feb, 09:56, Richard Heathfield <r...@see.sig.invalidwrote:
I have already posted a sensible answer.
Yes , thanks for that. I realised what it was happening with the code
about 10 seconds after I posted. That'll teach me to post when I'm
half asleep instead of attempting to think.

B2003
Feb 27 '08 #10
Richard Heathfield wrote:
>
.... snip ...
>
Although the above is a pastiche, this facility is nevertheless
useful and powerful, but does have the unfortunate consequence
you have noted - forgetting an "else" doesn't necessarily give
you the syntax error you'd have hoped for!
Which is where the Pascal syntax of never preceding an else with a
semi is useful.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 27 '08 #11
Kenneth Brody <ke******@spamcop.netwrites:
[...]
In case the OP (or anyone else) is wondering where this would be
useful, consider:

#if ENABLE_SOME_FEATURE
if ( new_feature_is_enabled() )
{
do_it_the_new_way();
do_another_thing_the_new_way();
}
else
#endif
{
do_it_the_old_way();
do_the_other_thing_the_old_way();
}

Compare this "clean" version to how you would have to write it if
compound statements weren't legal on their own.
It's not *that* bad:

#if ENABLE_SOME_FEATURE
if ( new_feature_is_enabled() )
{
do_it_the_new_way();
do_another_thing_the_new_way();
}
else
{
#endif
do_it_the_old_way();
do_the_other_thing_the_old_way();
#if ENABLE_SOME_FEATURE
}
#endif

It also has the advantage that if you delete all the
#if ENABLE_SOME_FEATURE
...
#endif
sections, the remaining code still looks ok (apart from the
indentation).

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 27 '08 #12
Boltar <bo********@yahoo.co.ukwrites:
By accident I came across a bug like this in some Linux code I'd
written today. All that had happened is I forgot the "else" yet the
code still compiled and ran. A simplified example is below.

#include <stdio.h>

main()
{
int a;
if (1 == 1) a = 1;
{
a = 2;
}
printf("a = %d\n",a);
}

When run it will print "a = 2". Should it compile at all or is GCCs
parser broken?
Your question has already been answered to death (and BTW "Kenny
McCormack" is at least annoying to us as he is to you), but let me
offer a suggestion that can avoid such problems.

Whenever I write an if, while, for, or do-while statement, I always
use blocks for the controlled statements. For example:

if (1 == 1) {
a = 1;
}
else {
a = 2;
}

It's a habit I picked up from Perl, which requires it, but I find that
it's useful in C as well.

Strictly speaking you'll still have the same problem if you drop the
"else", but with a more uniform layout I think you're less liketly to
do so. This style also avoids problems like:

if (condition)
statement;
another_statement_added_later;

Of course, plenty of smart people don't like this style.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 27 '08 #13
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
[...]
In case the OP (or anyone else) is wondering where this would be
useful, consider:

#if ENABLE_SOME_FEATURE
if ( new_feature_is_enabled() )
{
do_it_the_new_way();
do_another_thing_the_new_way();
}
else
#endif
{
do_it_the_old_way();
do_the_other_thing_the_old_way();
}

Compare this "clean" version to how you would have to write it if
compound statements weren't legal on their own.

It's not *that* bad:

#if ENABLE_SOME_FEATURE
if ( new_feature_is_enabled() )
{
do_it_the_new_way();
do_another_thing_the_new_way();
}
else
{
#endif
do_it_the_old_way();
do_the_other_thing_the_old_way();
#if ENABLE_SOME_FEATURE
}
#endif
I've seen code like that, and it looks "ugly" to me. Plus, I think
mine is earier to maintain. Consider what happens with you start to
#define ENABLE_ANOTHER_FEATURE and ENABLE_YET_ANOTHER_FEATURE, and
start having multiple if/elseif's along the way.

It also has the advantage that if you delete all the
#if ENABLE_SOME_FEATURE
...
#endif
sections, the remaining code still looks ok (apart from the
indentation).
Then what about debugging code, with local variables?

#if DO_MY_LOGGING
{
int i;
for ( i=0 ; i < LengthOfSomething ; i++ )
{
DoMyLogging(something[i]);
}
}
#endif

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Feb 27 '08 #14
if (1 == 1) a = 1;
The semicolon brings the if to an end.
Feb 28 '08 #15
MisterE wrote:
>
> if (1 == 1) a = 1;

The semicolon brings the if to an end.
In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 28 '08 #16
CBFalconer said:
MisterE wrote:
>>
>> if (1 == 1) a = 1;

The semicolon brings the if to an end.

In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.
If that is true, the following code will compile, and will *not* print
"Hmmm". Otherwise, the compilation will fail.

#include <stdio.h>

int main(void)
{
int a = 0;
if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 28 '08 #17
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>> if (1 == 1) a = 1;
>The semicolon brings the if to an end.
>In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.
In C, the definitive thing is matching the grammar, which in this case
means that you can't just look for an else, a semicolon, or a closing
brace. You have to look for the end of the "then" part, then look to
see if there's an else immediately following.

-- Richard
--
:wq
Feb 28 '08 #18
Richard Heathfield wrote:
CBFalconer said:
>MisterE wrote:
>>> if (1 == 1) a = 1;
The semicolon brings the if to an end.
In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.

If that is true, the following code will compile, and will *not* print
"Hmmm". Otherwise, the compilation will fail.
...."presence of [an immediately] following 'else'."...

Howzat?

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Feb 28 '08 #19
Richard Heathfield wrote:
CBFalconer said:
>MisterE wrote:
>>>
if (1 == 1) a = 1;

The semicolon brings the if to an end.

In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.

If that is true, the following code will compile, and will *not* print
"Hmmm". Otherwise, the compilation will fail.

#include <stdio.h>

int main(void)
{
int a = 0;
if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}
That has a following printf, not a following else. As you knew
anyhow. However, in Pascal:

IF true THEN a := 1;
ELSE (* anything *)

will bring up large compiler screams at the ELSE. Remove the semi
and all is well.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 28 '08 #20
CBFalconer said:
Richard Heathfield wrote:
>CBFalconer said:
>>MisterE wrote:

if (1 == 1) a = 1;

The semicolon brings the if to an end.

In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.
<snip>
> if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}

That has a following printf, not a following else.
Er, that else sure looks like a following else to me.

Here's another counter-example:

int main(void)
{
if(1 == 1)
return 0;
}

According to you, that 'if' has no end (because there is no following
'else' present).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 28 '08 #21
Richard Heathfield wrote:
>> if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}
That has a following printf, not a following else.

Er, that else sure looks like a following else to me.
No, the else is following the printf.

Feb 28 '08 #22
Richard Heathfield wrote:
CBFalconer said:
>Richard Heathfield wrote:
>>CBFalconer said:
MisterE wrote:
>
> if (1 == 1) a = 1;
>
The semicolon brings the if to an end.

In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.
<snip>
>> if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}

That has a following printf, not a following else.

Er, that else sure looks like a following else to me.
Here's another counter-example:

int main(void)
{
if(1 == 1)
return 0;
}

According to you, that 'if' has no end (because there is no following
'else' present).
I said 'definitive thing', not 'end'. And was talking about the
presence/absence of the following 'else'. I don't think I have
been sloppy this time. Also don't count line endings as
significant in either language. They're all white space.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 29 '08 #23
CBFalconer said:
Richard Heathfield wrote:
<snip>
> if(1 == 1)
return 0;
}

According to you, that 'if' has no end (because there is no following
'else' present).

I said 'definitive thing', not 'end'.
Look at the context - the thing to which you were replying was: "The
semicolon brings the if to an end." You said that this was true in Pascal
but not in C, and /then/ said: "In C the definitive thing is the presence
of a following 'else'." The obvious way to read this is that you were
claiming the presence of the following 'else' is the definitive thing that
marks the end of the 'if'.

If you didn't mean that, fine, we're arguing over nothing, so let's stop.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 29 '08 #24

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

Similar topics

1
by: pakkocool | last post by:
¿Como ganar dinero en internet? Lee atentamente el siguiente texto, es super interesante y te hara ganar muchos dolares si sigues las instrucciones y le pones empeño: Hace unos días que...
3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
7
by: Nice | last post by:
Hi. I read somewhere that in XML the tag <xml> is not legal. But in W3C specs I can't find this statement. Does anyone know about it ?
0
by: mykidisgollum | last post by:
Greetings, I have code which prints a document who's attributes are saved as flags in a database. One of the those attributes is letter or legal. When I am printing, I use the following...
7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
6
by: aj | last post by:
I currently have 2 official DB2 Workgroup Edition licenses for my 2 v8 production servers. I also have tech support/software upgrade agreements in place for both servers. I am interested in...
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
1
by: Wiktor Zychla | last post by:
is it legal to distribute axshdocvw, shdocvw and mshtml.dll with my application? is it legal to distribute Microsoft.mshtml.dll from 'Primary Interop Assemblies' folder (I assume it comes with...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
2
by: Army1987 | last post by:
Is this program legal C89? /* no headers included */ int main(void) { if (sizeof (exit(0), 0), ((void (* )(int))&exit)( (puts((const char *)"hello, world"), 0) ), 0) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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...

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.