1

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?

14
  • 1
    Don't blindly trust whatever llms say. They're often wrong. Commented Jun 17 at 15:16
  • 4
    There are dozens of libraries on my system that are not part of the C spec. Why is it surprising that I can compile programs that use them? Commented Jun 17 at 15:17
  • 2
    LLMs do not recognize truth and do not know how to make true statements. They recognize patterns in language, and know how to produce responses that, based on their training data, are probable. This can be useful if you are prepared to evaluate the response for fitness. But if you are asking LLM to provide information that you do not already know, then you are not prepared to do that. You should not be surprised to discover that LLM responses -- though sounding plausible -- are factually wrong. Commented Jun 17 at 15:51
  • 1
    If the C standard (any version) forbade any non-standard functions, you wouldn't have Windows, Linux, macOS, and all the programs that are running on those systems. With plain standard C functions and nothing else, not even using C standard functions that might themselves use non-C-standard functions, then all you could realistically do is some calculations. But you could not even do input or output, as those are relying on functions defined by the operating system, which are not in the C standard. Commented Jun 17 at 21:17
  • 2
    The root of the problem here is the dysfunctional POSIX "standard" which states that non-standard crap should be dumped inside standard C headers. This isn't allowed by the C language so POSIX is not harmonized with C. What happens when you compile with -std=cxx -pedantic is 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. Commented Jun 24 at 8:31

2 Answers 2

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

3

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

can you give example of "GNU extensions that are incompatible with ISO C ", please?
@Akramat, the manual offers the 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.)
@Akramat Have a look at: GCC Extensions to the C Language Family.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.