declaration of variables inside functions in C

Discussion in 'C Programming' started by Henri Manson, Sep 25, 2003.

  1. Henri Manson

    Henri Manson Guest

    Today I discovered that the following code fragment compiles under gcc 3.0
    (even with the -ansi option) and doesn't compile under gcc 2.95. As far as I
    know it's not allowed in C to declare variables in a block after the first C
    statement.

    #include <stdio.h>
    main()
    {
    int x;

    printf("Hello world\n");
    int y;
    return 0;
    }

    But propably the C standard has changed. Can somebody tell me something
    about that change?
     
    Henri Manson, Sep 25, 2003
    #1
    1. Advertising

  2. Henri Manson

    Mike Wahler Guest

    "Henri Manson" <> wrote in message
    news:bkvlq2$4n5$1.nb.home.nl...
    > Today I discovered that the following code fragment compiles under gcc 3.0
    > (even with the -ansi option) and doesn't compile under gcc 2.95. As far as

    I
    > know it's not allowed in


    The 'C90' incarnation of

    >C to declare variables in a block after the first C
    > statement.


    It is valid for 'C99'.
    Check your configuration settings. Your compiler
    is probably configured to compile 'C99' (or possibly
    C++, where this construct has always been valid)


    >
    > #include <stdio.h>
    > main()


    If the compiler accepted 'inline' object declarations like you say,
    I'd expect it to reject a function sans a return type.

    int main()

    > {
    > int x;
    >
    > printf("Hello world\n");
    > int y;
    > return 0;
    > }
    >
    > But propably the C standard has changed.


    Yes. About four years ago.

    > Can somebody tell me something
    > about that change?


    Not here. That would be a very long post. :)

    http://www.eskimo.com/~scs/C-faq/q11.2.html

    -Mike
     
    Mike Wahler, Sep 25, 2003
    #2
    1. Advertising

  3. Henri Manson

    Micah Cowan Guest

    "Henri Manson" <> writes:

    > Today I discovered that the following code fragment compiles under gcc 3.0
    > (even with the -ansi option) and doesn't compile under gcc 2.95. As far as I
    > know it's not allowed in C to declare variables in a block after the first C
    > statement.


    That's correct.

    > #include <stdio.h>
    > main()
    > {
    > int x;
    >
    > printf("Hello world\n");
    > int y;
    > return 0;
    > }
    >
    > But propably the C standard has changed. Can somebody tell me something
    > about that change?


    The C standard has changed, but that's somewhat irrelevant, as
    the -ansi option for gcc still implies C90. Note, too, that -ansi
    does not put gcc into ISO-conformance mode: you need -pedantic as
    well. And usually it is recommended to add -W -Wall -O2 for good
    measure, as well. If you add -pedantic, then gcc-3.0 will at
    least issue the required diagnostic for the constraint violation.

    The main thing that appears to have changed, is that gcc-3.0
    added declarations intermixed with statements as an extension,
    whereas gcc-2.95.* did not.

    Ironically, the code above is not legal in either C90 (which
    does not allow intermixed declarations and statements) nor in C99
    (which does); since C99 has disallowed the implicit "int" return
    type in function declarations (and, FYI, has also deprecated
    empty parentheses, too). For maximum portability, try:

    #include <stdio.h>
    int main(void)
    {
    int x, y;

    printf("Hello world!\n");
    return 0;
    }

    HTH,
    Micah
     
    Micah Cowan, Sep 26, 2003
    #3
  4. On 25 Sep 2003 16:11:53 -0700, Micah Cowan <> wrote:

    > "Henri Manson" <> writes:

    [ mixed declaration and statements ]
    > > But propably the C standard has changed. Can somebody tell me something
    > > about that change?

    >
    > The C standard has changed, but that's somewhat irrelevant, as
    > the -ansi option for gcc still implies C90. Note, too, that -ansi
    > does not put gcc into ISO-conformance mode: you need -pedantic as
    > well. And usually it is recommended to add -W -Wall -O2 <snip>


    Right. -ansi only causes those cases where GNU extensions _conflict_
    with standard C to be resolved in favor of the standard. (Including
    disabling the GNU extension of not doing trigraphs. <G>)

    > The main thing that appears to have changed, is that gcc-3.0
    > added declarations intermixed with statements as an extension,
    > whereas gcc-2.95.* did not.
    >
    > Ironically, the code above is not legal in either C90 (which
    > does not allow intermixed declarations and statements) nor in C99
    > (which does); since C99 has disallowed the implicit "int" return
    > type in function declarations


    And everywhere in all declarations and definitions; also implicit
    function declarations.

    > (and, FYI, has also deprecated
    > empty parentheses, too). <snip>


    But this is not a change. C89 deprecated old-style/K&R1 definitions
    (identifier-list followed by declarations) and declarations (empty
    parens). C99 retained them as still deprecated.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Oct 6, 2003
    #4
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,652
    Victor Bazarov
    Jun 21, 2004
  2. =?Utf-8?B?SWJyYWhpbS4=?=

    Session variables inside shared functions

    =?Utf-8?B?SWJyYWhpbS4=?=, Jun 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    640
    Eliyahu Goldin
    Jun 20, 2006
  3. Azdo
    Replies:
    6
    Views:
    407
    Howard
    Jun 28, 2005
  4. beginner
    Replies:
    7
    Views:
    566
    Alex Martelli
    Aug 19, 2007
  5. Mehmet Yavuz S. Soyturk

    variable declaration inside eval inside with

    Mehmet Yavuz S. Soyturk, Apr 11, 2007, in forum: Javascript
    Replies:
    3
    Views:
    135
    Mehmet Yavuz S. Soyturk
    Apr 12, 2007
Loading...

Share This Page