I can compile and run a program that includes <getopt.h> and uses getopt_long with keys -std=c11 --pedantic-errors. Can you explain why?
2 Answers
Nothing in the C 2011 standard says that C implementations may not provide additional headers and features, and nothing in the C 2011 standard says programs may not use such additional headers and features.
The C standard specifies a broad class of programs called conforming programs, which are largely those whose general form conforms to the grammar of the C language but that may use additional features and extensions. Any program that is compiled by any single conforming C implementation is a conforming program, even if no other C implementation compiles it. (A conforming C implementation is one that provides at least the required parts of the C language. It may also provide extensions.)
A program that uses only the features and properties specified by the C standard, without using any extensions or even implementation-specific properties such as the width of int is a strictly conforming program.
It is generally not useful for a compiler to diagnose programs that are not strictly conforming, because there are only a very few useful programs that are strictly conforming. A strictly conforming program can have text input and output and can do abstract mathematical calculations, but it cannot have a graphical user interface, cannot call routines in other languages, cannot call operating system routines outside those inherent in the standard C library, cannot read or store data in native binary formats, cannot rely on file system features, and so on. Almost every useful conforming program is not a strictly conforming program. Compilers may have some features to warn if some compiler extensions are used, but warning if a program were not strictly conforming in any way would generally be quite severe, so it is not a useful diagnostic.
(While strict conformance is an overly ambitious goal for most whole programs, it could be a useful goal for isolated subroutines. Compiler diagnostics for violating strict conformance could perhaps be useful in such limited circumstances.)
Comments
I can compile program and run a program that includes <getopt.h> and uses getopt_long with keys -std=c11 --pedantic-errors. Can you explain why?
GCC's -std=c11 flag disables GNU extensions that are incompatible with ISO C (2011 version), and only those. This is very different from ensuring that only features defined by ISO C are available to programs.
The flag disables certain language features that GCC recognizes by default, and it causes the standard library headers defined by ISO C to not declare any (otherwise-)unreserved identifiers. It does not affect certain other language extensions supported by GCC, and it has no effect on headers other than those defined by ISO C.
Note in particular that header inclusion is a standard language feature, so GCC must support it, and that ISO C does not limit the available headers to those it defines itself. Nor does ISO C limit what library functions an implementation is permitted to provide. The -std=c11 is not relevant to use of getopt.h and the functions it declares.
As for --pedantic-errors, this flag requests two things:
that all diagnostics required by ISO C in fact be emitted
that those otherwise-omitted diagnostics be classified as errors, such that GCC rejects the program if it elicits any of them.
That is altogether irrelevant to functions and headers not specified by ISO C, so long as they are of acceptable form.
(LLM said that it would not compile)
LLMs are not reliable sources of information.
3 Comments
asm and typeof keywords as examples. These not being keywords in ISO C 2011, programs are free to use them as ordinary identifiers. A program that does will be misinterpreted by gcc unless an appropriate -std= option is provided. (I note also that typeof is introduced as a new standard keyword in C23, but -std=c11 will not enable its recognition -- the opposite, in fact -- because it is not in C11.)
-std=cxx -pedanticis that the compiler strips all such non-conforming identifiers from standard C headers. That does however not affect completely unrelated libraries like getopt, pthreads, omp or whatever - these are mere application layer libraries which doesn't affect C conformance in any way since they are separate from the standard lib.