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