Position of variable declaration is causing "undeclared identifier" error.

Discussion in 'C Programming' started by craigbeanhead, Jul 24, 2003.

  1. Hi,

    I'm teaching myself C from K&R2. I've come across something that I
    really don't understand. When I try to compile the following code (in
    VC++7), I get an "undeclared identifier" error. When I move the second
    integer declaration to the beginning of the function, it compiles and
    runs correctly. I'm sure I read that you could declare a variable
    anywhere in a code block, as long as you don't attempt to use it
    *before* the declaration. Can anyone explain this to me?

    #include <stdio.h>

    int main(void)
    {
    int a;
    /* Works correctly if you move the "int b;" to here */
    a = 123;
    printf("%d\n", a);

    int b;
    b = 456;
    printf("%d\n", b);

    return 0;
    }

    -- Craig
     
    craigbeanhead, Jul 24, 2003
    #1
    1. Advertising

  2. craigbeanhead

    Eric Sosman Guest

    Re: Position of variable declaration is causing "undeclared identifier"error.

    craigbeanhead wrote:
    >
    > Hi,
    >
    > I'm teaching myself C from K&R2. I've come across something that I
    > really don't understand. When I try to compile the following code (in
    > VC++7), I get an "undeclared identifier" error. When I move the second
    > integer declaration to the beginning of the function, it compiles and
    > runs correctly. I'm sure I read that you could declare a variable
    > anywhere in a code block, as long as you don't attempt to use it
    > *before* the declaration. Can anyone explain this to me?
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int a;
    > /* Works correctly if you move the "int b;" to here */
    > a = 123;
    > printf("%d\n", a);
    >
    > int b;
    > b = 456;
    > printf("%d\n", b);
    >
    > return 0;
    > }


    In the latest ("C99") version of the Standard, you can
    declare variables as above. Earlier versions of the Standard,
    though, did not permit this: All block-local variables had to
    be declared before the block's first executable statement.
    Although C99-compliant compilers are starting to appear, most
    compilers available today still implement the older Standard,
    and will choke on the code you've shown.

    --
     
    Eric Sosman, Jul 24, 2003
    #2
    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. Ron Natalie

    Re: undeclared identifier?

    Ron Natalie, Jul 21, 2003, in forum: C++
    Replies:
    0
    Views:
    436
    Ron Natalie
    Jul 21, 2003
  2. Victor Bazarov

    Re: undeclared identifier?

    Victor Bazarov, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    2,683
    rajiv04
    Jul 22, 2003
  3. craigbeanhead
    Replies:
    6
    Views:
    554
    Jack Klein
    Jul 27, 2003
  4. yttrium
    Replies:
    12
    Views:
    1,463
    Jonathan Bromley
    Jan 5, 2007
  5. Replies:
    2
    Views:
    1,827
Loading...

Share This Page