代码
int main() {
// 定义变量a,赋值为10
int a = 10;
// 定义变量b,赋值为20
int b = 20;
// 定义变量c,将a和b相加的结果赋值给c
int c = a + b;
// 输出c的值
printf("%d", c);
// 返回0,表示程序正常结束
return 0;
}
问题:
vscode中使用code-runner插件允许c程序报错:
[Running] cd "d:\dev\c\study\" && gcc demo1.c -o demo1 && "d:\dev\c\study\"demo1
demo1.c: In function 'main':
demo1.c:9:5: error: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
9 | printf("%d", c);
| ^~~~~~
demo1.c:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'
+++ |+#include <stdio.h>
1 | int main() {
demo1.c:9:5: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-declaration-mismatch]
9 | printf("%d", c);
| ^~~~~~
demo1.c:9:5: note: include '<stdio.h>' or provide a declaration of 'printf'
[Done] exited with code=1 in 0.239 seconds
原因
未添加c头文件
#include <stdio.h>