This C function should always return false, but it doesn’t

后端 未结 4 1813
天命终不由人
天命终不由人 2021-01-29 18:15

I stumbled over an interesting question in a forum a long time ago and I want to know the answer.

Consider the following C function:

f1.c

#incl         


        
4条回答
  •  别那么骄傲
    2021-01-29 18:39

    Please compile with a command such as this one:

    gcc -Wall -Wextra -Werror -std=gnu99 -o main.exe main.c
    

    Output:

    main.c: In function 'main':
    main.c:14:5: error: implicit declaration of function 'f1' [-Werror=impl
    icit-function-declaration]
         printf( f1() == true ? "true\n" : "false\n");
         ^
    cc1.exe: all warnings being treated as errors
    

    With such a message, you should know what to do to correct it.

    Edit: After reading a (now deleted) comment, I tried to compile your code without the flags. Well, This led me to linker errors with no compiler warnings instead of compiler errors. And those linker errors are more difficult to understand, so even if -std-gnu99 is not necessary, please try to allways use at least -Wall -Werror it will save you a lot of pain in the ass.

提交回复
热议问题