Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Legal C or bug in gcc (http://www.velocityreviews.com/forums/t594514-legal-c-or-bug-in-gcc.html)

Boltar 02-27-2008 09:41 AM

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

Kenny McCormack 02-27-2008 09:44 AM

Re: Legal C or bug in gcc
 
In article <a28f28eb-52b0-4fd3-b045-4b29f46161b3@o77g2000hsf.googlegroups.com>,
Boltar <boltar2003@yahoo.co.uk> 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.


Boltar 02-27-2008 09:48 AM

Re: Legal C or bug in gcc
 
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

Joachim Schmitz 02-27-2008 09:48 AM

Re: Legal C or bug in gcc
 
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



Ben Bacarisse 02-27-2008 09:50 AM

Re: Legal C or bug in gcc
 
Boltar <boltar2003@yahoo.co.uk> 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.

Richard Heathfield 02-27-2008 09:55 AM

Re: Legal C or bug in gcc
 
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

Richard Heathfield 02-27-2008 09:56 AM

Re: Legal C or bug in gcc
 
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

santosh 02-27-2008 10:28 AM

Re: Legal C or bug in gcc
 
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>


Richard Tobin 02-27-2008 10:31 AM

Re: Legal C or bug in gcc
 
In article <a28f28eb-52b0-4fd3-b045-4b29f46161b3@o77g2000hsf.googlegroups.com>,
Boltar <boltar2003@yahoo.co.uk> 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

Kenneth Brody 02-27-2008 02:51 PM

Re: Legal C or bug in gcc
 
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:ThisIsASpamTrap@gmail.com>




All times are GMT. The time now is 06:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.