Skip to content

Enforcing C90 style declarations in gcc with -pedantic-errors #15

@fyquah

Description

@fyquah

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions