C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Strange behavior on an if statement.

    6 answers - 729 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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
  • No.1 | | 1058 bytes | |

    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

  • No.2 | | 1381 bytes | |

    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.
  • No.3 | | 1517 bytes | |

    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;
    }

  • No.4 | | 1543 bytes | |


    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

  • No.5 | | 600 bytes | |

    3 Dec 2005 14:51:24 -0800, in comp.lang.c , "Chad"
    <cdalten@gmail.comwrote:

    >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.
  • No.6 | | 80 bytes | |

    Eric Sosman wrote:
    "C Classic"
    "Australian Rules C"
    %2D1991

Re: Strange behavior on an if statement.


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 5 =
QUESTION ON "C/C++"

EMSDN.COM