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

C

craigbeanhead

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
 
B

Ben Pfaff

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.)
 
K

Kevin Easton

craigbeanhead said:
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.
 
C

craigbeanhead

E. Robert Tisdale said:
craigbeanhead wrote:

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

Bertrand Mollinier Toublet

jacob said:
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.
 
M

Mark Gordon

On 25 Jul 2003 00:03:48 -0700
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.
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.
 
J

Jack Klein

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top