C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What a stupid gcc!
Goto page 1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
zgene
Guest





PostPosted: Wed Jun 13, 2012 9:41 pm    Post subject: What a stupid gcc! Reply with quote



Look at this code:

1. #include<stdio.h>
2.
3. main()
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;
11. pc = &c;
12. printf("%d\n", *pc);
13. }

If I compile this code with gcc, I get:

myprompt> gcc PtrTest.c
PtrTest.c: In function `main':
PtrTest.c:9: parse error before `int'
PtrTest.c:10: `c' undeclared (first use in this function)
PtrTest.c:10: (Each undeclared identifier is reported only once
PtrTest.c:10: for each function it appears in.)
myprompt>

Whereas g++ compiles it nicely. Look:

myprompt> g++ PtrTest.c
myprompt>

However, if I move Line 10 to Line 6, gcc also compiles it nicely.

So, does g want us to declare all variables at the head of a block? What
a stupid gcc!
Back to top
James Kuyper
Guest





PostPosted: Wed Jun 13, 2012 9:41 pm    Post subject: Re: What a stupid gcc! Reply with quote



On 06/13/2012 05:41 PM, zgene wrote:
Quote:
Look at this code:

1. #include<stdio.h
2.
3. main()
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;
11. pc = &c;
12. printf("%d\n", *pc);
13. }

If I compile this code with gcc, I get:

myprompt> gcc PtrTest.c
PtrTest.c: In function `main':
PtrTest.c:9: parse error before `int'
PtrTest.c:10: `c' undeclared (first use in this function)
PtrTest.c:10: (Each undeclared identifier is reported only once
PtrTest.c:10: for each function it appears in.)
myprompt

Whereas g++ compiles it nicely. Look:

myprompt> g++ PtrTest.c
myprompt

However, if I move Line 10 to Line 6, gcc also compiles it nicely.

So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

Be more careful with the "stupid" label - it lacks historical
perspective. Do you do everything right the first time? Would you care
to be judged right now based upon how well you did something when you
were only 1/3 of your current age?

By default, gcc compiles gnu89, a language of it's own closely related
to C89. The ability to intermix declarations and statements that was
present in C++ almost from the beginning, was not added to C until C99,
and it was added to GnuC at around the same time. To put gcc into gnu99
or C99 mode, use the options -std=gnu99 or -std=c99, respectively.

C99 has had a very slow adoption rate; unlike C89, it had no strong
compelling advantage over existing versions of C (the feature whose
absence you're complaining about is one of the less compelling ones),
and some of it's new features were unpopular with many people. That's
why gnu89 is still the default, and why -ansi is equivalent to -std=c89.
Back to top
Eric Sosman
Guest





PostPosted: Wed Jun 13, 2012 9:41 pm    Post subject: Re: What a stupid gcc! Reply with quote



On 6/13/2012 5:41 PM, zgene wrote:
Quote:
[...]
So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

Any compiler conforming to the original "C90" ANSI Standard must
insist that all declarations precede all statements within a block.
It's a requirement of that edition of the C language.

The 1999 revision of the Standard ("C99") relaxed the requirement:
Statements and declarations can be intermixed freely, provided each
identifier's declaration precedes its first use.

The gcc compilers, like many others, can deal with code written
for either version of the Standard, and a few can even handle the
still later edition of 2011 ("C11"). The only requirement is that
the user invoke the compiler properly, telling it what dialect of C
it is supposed to adhere to. The documentation accompanying gcc
describes (quite clearly) how this is done, and what dialect is
chosen if you do not specify one (as you did not).

gcc cannot read your mind. What a stupid gcc!

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Jens Thoms Toerring
Guest





PostPosted: Wed Jun 13, 2012 9:41 pm    Post subject: Re: What a stupid gcc! Reply with quote

zgene <nospam (AT) nospam (DOT) com> wrote:
Quote:
Look at this code:

1. #include<stdio.h
2.
3. main()
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;
11. pc = &c;
12. printf("%d\n", *pc);
13. }

If I compile this code with gcc, I get:

myprompt> gcc PtrTest.c
PtrTest.c: In function `main':
PtrTest.c:9: parse error before `int'
PtrTest.c:10: `c' undeclared (first use in this function)
PtrTest.c:10: (Each undeclared identifier is reported only once
PtrTest.c:10: for each function it appears in.)
myprompt

Whereas g++ compiles it nicely. Look:

myprompt> g++ PtrTest.c
myprompt

However, if I move Line 10 to Line 6, gcc also compiles it nicely.

So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

You must either be using a very old version of gcc or 'gcc' is
an alias or similar that enforces C89 compatibility - in C89 you
definitely have to define all variables before the first "execu-
table" line. My version of gcc (4.6.1.) doesn't issue any war-
nings for your program when no options are set and several (but
different from yours) warnings when setting options for C89-
compatibility and/or a higher warning level.

g++ does compile a different language than C so it might accept
programs that gcc doesn't, so its similar to complaining that
your cars sinks when you drive it into a river while your boot
does swim.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt (AT) toerring (DOT) de
\__________________________ http://toerring.de
Back to top
Joe Pfeiffer
Guest





PostPosted: Wed Jun 13, 2012 9:41 pm    Post subject: Re: What a stupid gcc! Reply with quote

zgene <nospam (AT) nospam (DOT) com> writes:

Quote:
Look at this code:

1. #include<stdio.h
2.
3. main()
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;
11. pc = &c;
12. printf("%d\n", *pc);
13. }

If I compile this code with gcc, I get:

myprompt> gcc PtrTest.c
PtrTest.c: In function `main':
PtrTest.c:9: parse error before `int'
PtrTest.c:10: `c' undeclared (first use in this function)
PtrTest.c:10: (Each undeclared identifier is reported only once
PtrTest.c:10: for each function it appears in.)
myprompt

Whereas g++ compiles it nicely. Look:

myprompt> g++ PtrTest.c
myprompt

However, if I move Line 10 to Line 6, gcc also compiles it nicely.

So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

When you tell it to compile C, it compiles C (admittedly, as others have
pointed out, an old version of the standard). When you tell it to
compile C++, it compiles C++. I fail to see the grounds for your
complaint.
Back to top
Nick Bowler
Guest





PostPosted: Thu Jun 14, 2012 12:09 am    Post subject: Re: What a stupid gcc! Reply with quote

On Wed, 13 Jun 2012 21:41:53 +0000, zgene wrote:
Quote:
1. #include<stdio.h
2.
3. main()

Here, we have an example of the so-called "implicit int" in this
declaration of main.

Quote:
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;

And here, we have an example of the so-called "mixed declarations and
statements" with this declaration of c.

Quote:
11. pc = &c;
12. printf("%d\n", *pc);
13. }

"Mixed declarations and statements" were supported starting with the
1999 revision of the C standard. "Implicit int" ceased being supported
as of the very same revision. There is therefore no revision of the C
standard for which the above is valid. What a stupid program!
Back to top
Rui Maciel
Guest





PostPosted: Thu Jun 14, 2012 6:47 am    Post subject: Re: What a stupid gcc! Reply with quote

Jens Thoms Toerring wrote:

Quote:
You must either be using a very old version of gcc or 'gcc' is
an alias or similar that enforces C89 compatibility - in C89 you
definitely have to define all variables before the first "execu-
table" line. My version of gcc (4.6.1.) doesn't issue any war-
nings for your program when no options are set and several (but
different from yours) warnings when setting options for C89-
compatibility and/or a higher warning level.


Quite true. With gcc 4.6.3, the "forbids mixed declaration and code"
message is only presented as a warning when explicitly compiling the code
according to the C89/C90 standard, and only if the -pedantic flag is passed.


Rui Maciel
Back to top
James Kuyper
Guest





PostPosted: Thu Jun 14, 2012 8:56 am    Post subject: Re: What a stupid gcc! Reply with quote

On 06/14/2012 06:14 AM, Varun Tewari wrote:
....
Quote:
Its simply not stupid, its a simple fact the C++/Java allow decalaration anywhere in the code, but C wants them the first thing when you code. That's all

That's not been true for C since C99; we're already up to C2011.
--
James Kuyper
Back to top
Noob
Guest





PostPosted: Thu Jun 14, 2012 9:00 am    Post subject: Re: What a stupid gcc! Reply with quote

James Kuyper wrote:

Quote:
By default, gcc compiles gnu89, a language of it's own closely related
to C89. The ability to intermix declarations and statements that was
present in C++ almost from the beginning, was not added to C until C99,
and it was added to GnuC at around the same time. To put gcc into gnu99
or C99 mode, use the options -std=gnu99 or -std=c99, respectively.

James,

(I've snipped the OP's rant, because trolls deserve to be ignored.)

Please note that gnu89, the default mode, does allow mixed
declarations and code.

http://gcc.gnu.org/onlinedocs/gcc/Mixed-Declarations.html

$ cat mix.c
int main(void)
{
int a = 5;
a += 3;

int b = 2;
b += 1;

return a + b;
}

$ gcc -Wall -Wextra -std=gnu89 mix.c

No warning.
Back to top
Varun Tewari
Guest





PostPosted: Thu Jun 14, 2012 10:14 am    Post subject: Re: What a stupid gcc! Reply with quote

On Thursday, 14 June 2012 03:11:53 UTC+5:30, zgene wrote:
Quote:
Look at this code:

1. #include<stdio.h
2.
3. main()
4. { const int *pc;
5. int i = 10;
6.
7. pc = &i;
8. printf("%d\n", *pc);
9.
10. int c = 2;
11. pc = &c;
12. printf("%d\n", *pc);
13. }

If I compile this code with gcc, I get:

myprompt> gcc PtrTest.c
PtrTest.c: In function `main':
PtrTest.c:9: parse error before `int'
PtrTest.c:10: `c' undeclared (first use in this function)
PtrTest.c:10: (Each undeclared identifier is reported only once
PtrTest.c:10: for each function it appears in.)
myprompt

Whereas g++ compiles it nicely. Look:

myprompt> g++ PtrTest.c
myprompt

However, if I move Line 10 to Line 6, gcc also compiles it nicely.

So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

Its simply not stupid, its a simple fact the C++/Java allow decalaration anywhere in the code, but C wants them the first thing when you code. That's all
Back to top
Rui Maciel
Guest





PostPosted: Thu Jun 14, 2012 11:37 am    Post subject: Re: What a stupid gcc! Reply with quote

Rui Maciel wrote:

Quote:
Jens Thoms Toerring wrote:

You must either be using a very old version of gcc or 'gcc' is
an alias or similar that enforces C89 compatibility - in C89 you
definitely have to define all variables before the first "execu-
table" line. My version of gcc (4.6.1.) doesn't issue any war-
nings for your program when no options are set and several (but
different from yours) warnings when setting options for C89-
compatibility and/or a higher warning level.


Quite true. With gcc 4.6.3, the "forbids mixed declaration and code"
message is only presented as a warning when explicitly compiling the code
according to the C89/C90 standard, and only if the -pedantic flag is
passed.

Just for posterity.

<example>
rui@Kubuntu:tmp$ cat main.c
#include<stdio.h>

main()
{
const int *pc;
int i = 10;

pc = &i;
printf("%d\n", *pc);

int c = 2;
pc = &c;
printf("%d\n", *pc);
}
rui@Kubuntu:tmp$ gcc -std=c90 -Wall -pedantic main.c
main.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
main.c: In function ‘main’:
main.c:11:2: warning: ISO C90 forbids mixed declarations and code [-
pedantic]
main.c:14:1: warning: control reaches end of non-void function [-Wreturn-
type]
rui@Kubuntu:tmp$ gcc -std=c99 -Wall -pedantic main.c
main.c:3:1: warning: return type defaults to ‘int’ [enabled by default]
</example>


Rui Maciel
Back to top
Rui Maciel
Guest





PostPosted: Thu Jun 14, 2012 11:46 am    Post subject: Re: What a stupid gcc! Reply with quote

Nomen Nescio wrote:

Quote:
Yes, we know. With enough switches gcc will compile anything. The question
is, how long will K&R C and unsafe defaults be gcc's default mode.

Hello clang, bye bye gcc!

There might be a ton of issues which would make clang a preferable option
over gcc, but the default config values of a specific set of options is
clearly not one.


Rui Maciel
Back to top
Nomen Nescio
Guest





PostPosted: Thu Jun 14, 2012 12:33 pm    Post subject: Re: What a stupid gcc! Reply with quote

Noob <root (AT) 127 (DOT) 0.0.1> wrote:

Quote:
James Kuyper wrote:

By default, gcc compiles gnu89, a language of it's own closely related
to C89. The ability to intermix declarations and statements that was
present in C++ almost from the beginning, was not added to C until C99,
and it was added to GnuC at around the same time. To put gcc into gnu99
or C99 mode, use the options -std=gnu99 or -std=c99, respectively.

James,

(I've snipped the OP's rant, because trolls deserve to be ignored.)

Please note that gnu89, the default mode, does allow mixed
declarations and code.

http://gcc.gnu.org/onlinedocs/gcc/Mixed-Declarations.html

$ cat mix.c
int main(void)
{
int a = 5;
a += 3;

int b = 2;
b += 1;

return a + b;
}

$ gcc -Wall -Wextra -std=gnu89 mix.c

No warning.

Yes, we know. With enough switches gcc will compile anything. The question
is, how long will K&R C and unsafe defaults be gcc's default mode.

Hello clang, bye bye gcc!
Back to top
Noob
Guest





PostPosted: Thu Jun 14, 2012 2:19 pm    Post subject: Re: What a stupid gcc! Reply with quote

Nomen Nescio wrote:

Quote:
Yes, we know. With enough switches gcc will compile anything. The question
is, how long will K&R C and unsafe defaults be gcc's default mode.

GCC's default mode is not K&R C, it's C90 with extensions.

Thanks for trolling.

Quote:
Hello clang, bye bye gcc!

Competition is good.
Back to top
Kaz Kylheku
Guest





PostPosted: Thu Jun 14, 2012 11:21 pm    Post subject: Re: What a stupid gcc! Reply with quote

On 2012-06-13, zgene <nospam (AT) nospam (DOT) com> wrote:
Quote:
So, does g want us to declare all variables at the head of a block? What
a stupid gcc!

Stupid you for disparaging a tool which translates a family of languages you
haven't studied properly.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page 1, 2, 3, 4, 5, 6, 7  Next
Page 1 of 7

 
 


Powered by phpBB © 2001, 2006 phpBB Group