Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange behavior on an if statement.

Reply
Thread Tools

Strange behavior on an if statement.

 
 
Chad
Guest
Posts: n/a
 
      12-03-2005
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

 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      12-03-2005
Chad wrote:
> 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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-03-2005
Chad wrote:

> 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.

--
Eric Sosman
lid
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-03-2005
Chad wrote:
> 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;
}

 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      12-03-2005

Ulrich Eckhardt wrote:
> Chad wrote:
> > 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


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

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-03-2005
On 3 Dec 2005 14:51:24 -0800, in comp.lang.c , "Chad"
<> wrote:

>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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-04-2005
Eric Sosman wrote:

> "C Classic"


"Australian Rules C"

http://webstore.ansi.org/ansidocstor...AS+3955%2D1991

--
pete
 
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
Thunderbird strange behavior... Jim Firefox 5 11-17-2005 03:09 PM
Firefox 1.04 and Strange Find Behavior Thomas Firefox 5 06-28-2005 08:40 PM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
strange behavior when using 'read' sstark Perl 0 03-06-2005 02:27 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments