1

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?

iBug
  • 35,554
  • 7
  • 89
  • 134

1 Answers1

4

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • There should also be an error for the attempted use of `//` comments,which are not valid in C89 – M.M Sep 12 '17 at 07:49
  • @M.M I already have it as a PM, since I thought it was not the intention of the OP. Should I higligh it by including that in the compiler erros? – gsamaras Sep 12 '17 at 07:52
  • How can I make GCC warn me about double-slash `//` comments? – iBug Sep 12 '17 at 07:53
  • 1
    If you compile the code in your question with the command I provide in my answer, you will see that you receive an error for these comments @iBug. – gsamaras Sep 12 '17 at 08:01