I've got asked this frequently, that I think is worth pointing out, as it might be useful for writing test cases etc.
To force gcc to throw and error with non-C89 compliant type declaration, one has to pass std=c89 and -pedantic-errors to the compiler.
$ cat main.c
int
main()
{
int a;
a = 1;
int b = 2;
return 0;
}
$ gcc -std=c89 main.c # succeeds
$ gcc -pedantic -std=c89 -Wall main.c
main.c:6:7: warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement]
int b = 2;
^
main.c:6:7: warning: unused variable 'b' [-Wunused-variable]
2 warnings generated.
$ gcc -pedantic-errors -std=c89 main.c
main.c:6:7: error: ISO C90 forbids mixing declarations and code [-Werror,-Wdeclaration-after-statement]
int b = 2;
I've got asked this frequently, that I think is worth pointing out, as it might be useful for writing test cases etc.
To force gcc to throw and error with non-C89 compliant type declaration, one has to pass
std=c89and-pedantic-errorsto the compiler.