Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Legal C or bug in gcc

Reply
Thread Tools

Legal C or bug in gcc

 
 
Boltar
Guest
Posts: n/a
 
      02-27-2008
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
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      02-27-2008
In article <a28f28eb-52b0-4fd3-b045->,
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?
>
>B2003


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

 
Reply With Quote
 
 
 
 
Boltar
Guest
Posts: n/a
 
      02-27-2008
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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-27-2008
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;

here you assign 1 to a, always, as 1 always equals 1

> {
> a = 2;

here you assign 2 to a, always

> }
> printf("a = %d\n",a);
> }
>
> When run it will print "a = 2". Should it compile at all or is GCCs
> parser broken?

Why do you think it is broken or should print something else?

Bye, Jojo


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-27-2008
Boltar <> writes:

> 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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-27-2008
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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-27-2008
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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-27-2008
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>

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-27-2008
In article <a28f28eb-52b0-4fd3-b045->,
Boltar <> wrote:
>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
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      02-27-2008
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: <private.php?do=newpm&u=>


 
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
Gcc 3.4.X to Gcc 4.1.X upgrading kas C++ 1 04-22-2010 08:56 PM
GCC 3.4.3 and GCC 4.1.2 ashnin C++ 1 07-07-2008 01:10 PM
Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4 eknecronzontas@yahoo.com C++ 5 09-17-2005 12:27 AM
gcc 2.95 and gcc 3.2 gouqizi.lvcha@gmail.com C++ 8 03-16-2005 02:34 AM
C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1 Kevin P. Fleming C Programming 2 11-06-2003 05:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57