C
Chris Croughton
In the following code, what is the scope of the variables?
int main(void)
{
int var;
for (var = 0; var < 10; ++var)
{
int var = var + 5;
printf("%d\n", var);
}
return 0;
}
I'm certain that I have read (here in clc) that a variable's scope
doesn't start until the end of its definition, so saying
int var = var + 5;
is valid and sets the block-local variable var to the value of the outer
variable var. However, GCC (2.95.4, 3.0.4, 3.3.3) disagrees with me.
Have I got confused, or is GCC broken in that way?
(Yes, that code is confusing. However, there are times, like in a
function-like macro, when it can occur.)
Chris C
int main(void)
{
int var;
for (var = 0; var < 10; ++var)
{
int var = var + 5;
printf("%d\n", var);
}
return 0;
}
I'm certain that I have read (here in clc) that a variable's scope
doesn't start until the end of its definition, so saying
int var = var + 5;
is valid and sets the block-local variable var to the value of the outer
variable var. However, GCC (2.95.4, 3.0.4, 3.3.3) disagrees with me.
Have I got confused, or is GCC broken in that way?
(Yes, that code is confusing. However, there are times, like in a
function-like macro, when it can occur.)
Chris C