0

I got an error while compiling a program in GCC in windows. When i put an initialization expression inside an if condition without braces, I get an error : expected expression before short/int. When I put braces, for the body of the if, the source is compiled without any errors. This problem persists in any program. I wrote a program containing only an if condition, and still I get the error. This is the code :

int main(void)
{
    if (1 < 2) // Doesn't work with any condition I've tested so far.
        int a = 0; // Expected expression before int here.
}

But the problem is resolved when I put the "int a = 0" part within braces. I just want to know why this is happening. Is it a bug in the compiler?

rmn
  • 81
  • 1
  • 11
  • A better question would be "how on earth does the code `if (1 < 2) int a = 0;` make any sense?" Since `a` immediately goes out of scope. – Lundin Oct 02 '17 at 08:59
  • Right. So no point in that. The error generated could be more clear. – rmn Oct 07 '17 at 14:01

0 Answers0