Why can I use gets() in gcc -std=c11?

前端 未结 3 1807
余生分开走
余生分开走 2021-01-02 00:43

The gets() function has been removed from the C language. No such function exists in the standard.

Yet I compile the following code:

#in         


        
3条回答
  •  没有蜡笔的小新
    2021-01-02 00:58

    gcc is just the compiler, not the entire implementation.

    On my system (Linux Mint 17.3, gcc 4.8.4, GNU libc 2.19), I get:

    $ gcc -std=c11 -pedantic-errors -Wall -Wextra -c c.c
    c.c: In function ‘main’:
    c.c:5:3: error: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
       (void) gets (NULL);
       ^
    

    To correctly diagnose the error, the implementation needs to be conforming. That means both the compiler (which never provided gets in the first place) and the library.

    You're using a library that still provides the gets function. Because of that the implementation as a whole (which consists of the compiler gcc, the library, and a few other pieces) does not conform to C11.

    Bottom line: This is not a gcc issue, and there's not much that gcc can do about it. (Well, it could issue a special-case diagnostic for gets, but then it would have to determine that it's not a valid call to a user-defined function with the same name.)

提交回复
热议问题