Strange behavior on an if statement.

C

Chad

I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}


This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?

Thanks in advance.

Chad
 
U

Ulrich Eckhardt

Chad said:
int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}


This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

Well, how about some exact compiler versions? I'm pretty sure it doesn't
have anything to do with Suse vs FreeBSD but rather with compiler vs
compiler.

Anyhow, I suspect that one of the compilers is C99 aware, while the other
(the one on FreeBSD) is targetting C89. C89 requires that all variables
are declared at the beginning of the block they are used in, which is why
it chokes on the declaration of 'b'.

BTW: never compile without high warning levels (-Wall for gcc), you just
miss too many possible errors.

Uli
 
E

Eric Sosman

Chad said:
I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}


This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?

In "C Classic" all variable declarations in a block had
to appear at the beginning, before any executable statements.
The original 1989 C Standard kept that rule.

The new 1999 C Standard relaxed the rule, allowing you
to intermix declarations and statements in any order you
please (as long as variables are declared before you try to
use them).

It seems you're using two gcc versions, one that obeys
the older declarations-first rule and one that permits the
newer intermixed style. Some gcc versions support both
versions of the Standard, depending on the compile-time
options. Check your gcc versions, and gcc's documentation.
 
S

slebetman

Chad said:
I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

Given the following:

#include <stdio.h>

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;
return 0;
}


This will compile with no syntax errors on Suse Linux 9.1 using the gnu
compiler. However, when I try to compile this under FreeBSD 4.8 using
the gnu compiler, I get the following error message:

$gcc -g iffy.c -o iffy
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

What is going on here?

I think C99 allows this C++ like form:

/* declaration */
int a = 0;
/* processing */
if (a == 0) {
...
/* declaration */
int b = 0;
/* processing */
....


but all older C specs only allows:

/* declaration */
int a = 0;
int b = 0;
/* processing */
if (a == 0) {
....


So, unless your compiler conforms to C99. It can generate errors
because you declare variables after doing some processing (in this case
an if and a printf).

Since currently few compilers are C99, you should put all declarations
at the top of your function. The corrected code that should compile
everywhere:

#include <stdio.h>

int main(void) {
int a = 0;
int b = 2;

if(a == 0) {
fprintf(stderr, "exiting \n");
}
return 0;
}
 
C

Chad

Ulrich said:
Well, how about some exact compiler versions? I'm pretty sure it doesn't
have anything to do with Suse vs FreeBSD but rather with compiler vs
compiler.

Anyhow, I suspect that one of the compilers is C99 aware, while the other
(the one on FreeBSD) is targetting C89. C89 requires that all variables
are declared at the beginning of the block they are used in, which is why
it chokes on the declaration of 'b'.

BTW: never compile without high warning levels (-Wall for gcc), you just
miss too many possible errors.

Uli

Hmmm..... fascinating. Here is more on what is going on;
Suse Linux 9.1 is running gcc v 3.3.3. When I enable full warnings, I
get:
iffy.c: In function `main':
iffy.c:9: warning: unused variable `b'

FreeBSD 4.8 is running 2.95.3. When I enable full warnings, I still the
the exact same error message:
iffy.c: In function `main':
iffy.c:9: syntax error before `int'

Interesting. Thanks.

Chad
 
M

Mark McIntyre

I'm not too sure if the question would fall under comp.lang.c or some
kind of compiler newsgroup. I'm going to ask anyhow.

int main(void) {
int a = 0;
if(a == 0) {
fprintf(stderr, "exiting \n");
}

int b = 2;

you're not allowed to do this in C89 - variable declarations may not
occur after statements.
You /can/ do this in C++ and in C99, which some compilers partially
support. So this explains your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top