By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
cr***********@hotmail.com (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,0xa6 7f6aaa,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;}}}
Nov 13 '05 #2

P: n/a
craigbeanhead <cr***********@hotmail.com> 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.

Nov 13 '05 #3

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>...
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
Nov 13 '05 #4

P: n/a
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

Nov 13 '05 #5

P: n/a
On 25 Jul 2003 00:03:48 -0700
cr***********@hotmail.com (craigbeanhead) wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:<3F**************@jpl.nasa.gov>...
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.
Nov 13 '05 #6

P: n/a
On Fri, 25 Jul 2003 08:48:58 -0700, Bertrand Mollinier Toublet
<be***********************@enst-bretagne.fr> 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
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.