Do I need -pedantic flag from GCC with C11?

后端 未结 6 2122
长情又很酷
长情又很酷 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:04

    The original C C89/C90 standard only required the compiler to allow string literals up to 509 bytes long. Code compiled to C90 standard using a string longer than 509 bytes long is not maximally portable; a standard-conforming compiler could reject the code. It is unlikely to be a problem in practice, but in theory that could happen.

    The limit was raised in C99 to 4095 bytes (and stayed the same in C11). Consequently, you have to have a much longer string to run foul of the limit with C99 or C11.

    The GCC 4.x compilers worked with the C90 standard by default. The GCC 5.x compilers work with the C11 standard by default. Thus, code that is not maximally portable to C90 will generate warnings when compiled with -pedantic under GCC 4.x, but won't generate the same warnings with GCC 5.x unless the construct is also not portable to all C11 compilers — unless it violates one of the C11 compile-time limits too.

    The -pedantic flag has its uses. For example, yesterday, someone was running into problems because they were using:

    void *p = malloc(sizeof(*p));
    

    That's malformed code according to the standard, but GCC (5.3.0 specifically tested, but the other 5.x and 4.x versions behave the same) allows it, interpreting sizeof(*p) as being 1. That's not portable to other compilers. Using -pedantic reports the problem; not using -pedantic does not.

提交回复
热议问题