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

    Ben Pfaff Guest

    (craigbeanhead) writes:

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


    This is true in C99, but not in any earlier version. You
    probably don't have a C99 compiler. (C++ also supports
    declarations mid-block.)
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*p=
    b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
    2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Jul 24, 2003
    #2
    1. Advertising

  3. craigbeanhead

    Kevin Easton Guest

    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?


    As others have said, you can't do this in the widely-implemented C
    standard. However, you can open a block statement anywhere a normal
    statement can go:

    #include <stdio.h>

    int main(void)
    {
    int a;
    a = 123;
    printf("%d\n", a);

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

    return 0;
    } }

    - Kevin.
    Kevin Easton, Jul 25, 2003
    #3
  4. "E. Robert Tisdale" <> wrote in message news:<>...
    > craigbeanhead wrote:


    > > I'm teaching myself C from K&R2.


    > Sigh.


    Sigh? Is it a bad idea to learn from K&R2, then? Or is there another
    reason for your sighing? I do realise that the book is *very* old, in
    computer terms. I also realise that if I'm aiming for up-to-date
    standards compliance with my code, that I'll need to supplement the
    book with fresher information. (Thinking of buying the ISO standard,
    actually). I think th previous C standard is a good place to aim,
    actually - If the newest compilers don't support the newest features
    yet?

    > > When I try to compile the following code (in VC++7),
    > > I get an "undeclared identifier" error.


    > That's true for C++ and the new C 99 standard
    > but *not* for the old C 89 standard.
    > Check your compiler options.


    I see. This explains my problem. Haven't checked my compiler options,
    I'll stick to the traditional method.

    > Until more compilers comply with the new C 99 standard,
    > it might be better to write:
    >
    > #include <stdio.h>
    >
    > int main(int argc, char* argv[]) {
    > int a = 123;
    > printf("%d\n", a);
    > { int b = 456;
    > printf("%d\n", b);
    > }
    > return 0;
    > }


    Not necessary - I had the beginnings of a space invaders clone that I
    wrote a while ago (in pseudo C++, C really), and I wanted to make it
    as "C" as possible. Now I know that I'm not allowed to make variable
    declarations in the middle of a code block, I'll remember not to do it
    again.

    Thanks

    -- Craig
    craigbeanhead, Jul 25, 2003
    #4
  5. Re: Position of variable declaration is causing "undeclared identifier"error.

    jacob navia wrote:
    >
    > If you compile that with lcc-win32 the output is
    > 123
    > 456
    >
    > http://www.cs.virginia.edu/~lcc-win32
    >

    I am pretty disappointed to see that instead of being technically
    helpful on this newsgroup (and, as the developer of a compiler, you
    would be expected to know enough to be very helpful), you repeatedly
    advertise your own product in the lamest and most unhelpful way.

    I shall not want to read from you again.
    --
    Bertrand Mollinier Toublet
    "No sea vivo, Buendia" -- El presidente del tribunal,
    in Cien anos de soledad, de Gabriel Garcia Marquez
    Bertrand Mollinier Toublet, Jul 25, 2003
    #5
  6. craigbeanhead

    Mark Gordon Guest

    Re: Position of variable declaration is causing "undeclaredidentifier" error.

    On 25 Jul 2003 00:03:48 -0700
    (craigbeanhead) wrote:

    > "E. Robert Tisdale" <> wrote in message
    > news:<>...
    > > craigbeanhead wrote:

    >
    > > > I'm teaching myself C from K&R2.

    >
    > > Sigh.

    >
    > Sigh? Is it a bad idea to learn from K&R2, then? Or is there another
    > reason for your sighing? I do realise that the book is *very* old, in
    > computer terms.


    It may be old but it is still the book to use IMHO.

    > I also realise that if I'm aiming for up-to-date
    > standards compliance with my code, that I'll need to supplement the
    > book with fresher information. (Thinking of buying the ISO standard,
    > actually). I think th previous C standard is a good place to aim,
    > actually - If the newest compilers don't support the newest features
    > yet?


    Correct. Most compilers do not support C99 so C90 is still better for
    portability.

    > > > When I try to compile the following code (in VC++7),
    > > > I get an "undeclared identifier" error.

    >
    > > That's true for C++ and the new C 99 standard
    > > but *not* for the old C 89 standard.
    > > Check your compiler options.

    >
    > I see. This explains my problem. Haven't checked my compiler options,
    > I'll stick to the traditional method.


    It is worth turning up the warnings. It allows the compiler to tell you
    about *some* things that won't work as expected.

    > > Until more compilers comply with the new C 99 standard,
    > > it might be better to write:
    > >
    > > #include <stdio.h>
    > >
    > > int main(int argc, char* argv[]) {
    > > int a = 123;
    > > printf("%d\n", a);
    > > { int b = 456;
    > > printf("%d\n", b);
    > > }
    > > return 0;
    > > }


    Introducing a new block just to add a variable is IMHO pointless.

    > Not necessary - I had the beginnings of a space invaders clone that I
    > wrote a while ago (in pseudo C++, C really), and I wanted to make it
    > as "C" as possible. Now I know that I'm not allowed to make variable
    > declarations in the middle of a code block, I'll remember not to do it
    > again.


    Since you are prepared to change your habits so that you produce
    conforming code you should find most people here to be helpful.

    Many of the people that know the C standards far better think that ERT
    should be ignored. From what I've seen I agree.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Jul 25, 2003
    #6
  7. craigbeanhead

    Jack Klein Guest

    On Fri, 25 Jul 2003 08:48:58 -0700, Bertrand Mollinier Toublet
    <> wrote in comp.lang.c:

    > jacob navia wrote:
    > >
    > > If you compile that with lcc-win32 the output is
    > > 123
    > > 456
    > >
    > > http://www.cs.virginia.edu/~lcc-win32
    > >

    > I am pretty disappointed to see that instead of being technically
    > helpful on this newsgroup (and, as the developer of a compiler, you
    > would be expected to know enough to be very helpful), you repeatedly
    > advertise your own product in the lamest and most unhelpful way.
    >
    > I shall not want to read from you again.


    I would dispute the word "advertise" here, since lcc-win32 can be
    downloaded for free. And that is really free, no registration or
    personal information required, just point a browser at that page and
    click on the download links. So Jacob isn't selling anything at all.

    There are other free compilers available for the Windows platform that
    provide some effort at C99 conformance, but none is as small a
    download or as easy to use as Jacob's lcc-win32.

    Personally I use lcc-win32 frequently and recommend it to anyone
    looking for a free compiler to begin learning with. Especially anyone
    still limited to dial-up Internet access, since it is a vastly smaller
    download than other alternatives.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jul 27, 2003
    #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. Ron Natalie

    Re: undeclared identifier?

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

    Re: undeclared identifier?

    Victor Bazarov, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    2,638
    rajiv04
    Jul 22, 2003
  3. craigbeanhead
    Replies:
    1
    Views:
    408
    Eric Sosman
    Jul 24, 2003
  4. yttrium
    Replies:
    12
    Views:
    1,374
    Jonathan Bromley
    Jan 5, 2007
  5. Replies:
    2
    Views:
    1,772
Loading...

Share This Page