【发布时间】:2024-04-13 04:40:04
【问题描述】:
我正在学习 C,并做一些测试。
Ubuntu 16.01
gcc --version
gcc (GCC) 6.3.0 版权所有 (C) 2016 Free Software Foundation, Inc. 这是免费软件;查看复制条件的来源。没有 保修单;甚至不是为了适销性或特定用途的适用性。
test.c
main() {
printf("Hello Word.\n");
printf("pow function : %f \n", pow(10,2));
}
我使用这个命令编译:
gcc -std=c11 test.c -o test
test.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^~~~
test.c: In function ‘main’:
test.c:4:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("Hello Word.\n");
^~~~~~
test.c:4:3: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:4:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
test.c:5:35: warning: implicit declaration of function ‘pow’ [-Wimplicit-function-declaration]
printf("powf function : %d \n", pow(10,2));
^~~
test.c:5:35: warning: incompatible implicit declaration of built-in function ‘pow’
test.c:5:35: note: include ‘<math.h>’ or provide a declaration of ‘pow’
最后:
./test
Hello Word.
pow function : 100.000000
问:我是否需要使用#include <stdio.h> 或C standard library 中的任何一个?
更改文件:
#include <stdio.h>
#include <math.h>
int main() {
printf("Hello Word.\n");
printf("powf function : %f \n", pow(10,2));
return 0;
}
编译命令(这种情况下没有警告):
gcc -std=c11 test.c -o test
执行输出:
./test
Hello Word.
pow function : 100.000000
在这两种情况下,输出是相同的。
【问题讨论】:
-
这在多个方面显然不是有效的 C。 “Hello world”是any C书的第一个例子,那么你发现了什么?仅通过反复试验来学习 C 是一个非常糟糕的主意。
-
警告 (
test.c:4:3: warning) 告诉您存在问题,而下一行 (test.c:4:3:note) 会告诉您需要做什么来解决它。下一个块 (test.c:5:35:warning' andtest.c:5:35:note`) 为不同的功能重复这些相同的指令。您是否阅读了邮件中的文字?它们不只是为了填满屏幕上的空白空间;他们拥有非常有用的信息,但前提是您实际阅读消息才能获得它。 -
我对问题进行了更新。