Do I need -pedantic flag from GCC with C11?

后端 未结 6 2117
长情又很酷
长情又很酷 2021-02-09 05:42

I\'m currently running Linux Mint on my Machine with GCC-5.3 because C11 is included default.

I started learning C fo

6条回答
  •  走了就别回头了
    2021-02-09 06:08

    If you need it, you need it. If you don't, you don't.

    gcc's -pedantic option tells it to strictly enforce the rules of the C standard you've requested. This results in additional warning messages (or fatal errors if you use -pedantic-errors).

    The question is, do you want the compiler to warn you about code that violates the requirements of the C standard?

    If you want your code to be as portable as possible, use -pedantic and pay close attention to anything it tells you. If you want your code to depend on non-standard features, don't use -pedantic -- but then you run the risk that your code might not compile with a different compiler and/or for a different target system.

    The specific messages you're run into are for things that have changed between C90 and C11. C11 requires compilers to support at least 4095 characters in a string literal; C90 only requires 509. (In practice, for gcc, the actual limit is not fixed but is imposed by available memory at compile time. And the way the limits are described in the standard is not that simple, but I won't get into that.) Still, you'll rarely need to have a string literal that long.

    C99 added the %zu format for printing a value of type size_t. If you want your code to be portable to pre-C99 implementations, you'll need to avoid using it; for example, you can use printf("%lu\n", (unsigned long)sizeof foo). In practice, most current implementations do support %zu.

    A return statement with an expression, even an expression of type void, is not permitted in a function defined with a void return type. That's a warning you should want (IMHO).

    Bottom line: Use -pedantic if you want to strictly enforce the rules of the C standard. If you don't want to do that, don't use -pedantic. (But consider compiling your code with -pedantic at least occasionally to weed out any real errors it detects, in addition to warnings that you might not care about.)

提交回复
热议问题