Strange behavior on an if statement.

Discussion in 'C Programming' started by Chad, Dec 3, 2005.

  1. Chad

    Chad Guest

    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
    Chad, Dec 3, 2005
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Dec 3, 2005
    #2
    1. Advertising

  3. Chad

    Eric Sosman Guest

    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
    Eric Sosman, Dec 3, 2005
    #3
  4. Chad

    Guest

    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;
    }
    , Dec 3, 2005
    #4
  5. Chad

    Chad Guest

    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
    Chad, Dec 3, 2005
    #5
  6. 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 =----
    Mark McIntyre, Dec 3, 2005
    #6
  7. Chad

    pete Guest

    pete, Dec 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    802
  2. Jay McGavren
    Replies:
    11
    Views:
    1,130
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,079
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    461
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,053
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page