Can't define variables after any operation?

156 views Asked by At

My C language teacher claims that all variables must be defined before any operation. I can somehow recall it's a very old feature of C (no later than 1990) but I can't reproduce it with GCC 7.2.0.

My teacher claims this:

int main(){
    int a; /* Valid */
    a = 1; /* An operation */
    int b; /* Invalid because an operation has already occurred */
    return 0;
}

I tried compiling with

gcc test.c -std=c89 -Wall -Wextra -pedantic

but it gives no error, not even a warning.

How can I verify (or prove wrong) that statement?

1

There are 1 answers

4
gsamaras On BEST ANSWER

Compile with -pedantic-errors, like this:

gcc test.c -std=c89 -Wall -Wextra -pedantic-errors

and you should see this error (among the unused variable warnings):

test.c:4:5: error: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     int b;
     ^~~

in GCC 7.20.

PS: The comments are invalid too, I removed them before compiling.