如题,我在UBUNTU系统下利用记事本编写了一段C程序,程序如下:
main() { char h="Hello World!"; printf("%c\n",h); }
利用终端查看,命令如下:gcc -g -Wall hello.c -o hello.c
结果出现这样的错误:
hello.c:1:1: 警告: 返回类型默认为‘int’ [-Wreturn-type] hello.c: 在函数‘main’中: hello.c:3:9: 警告: 初始化将指针赋给整数,未作类型转换 [默认启用] hello.c:4:2: 警告: 隐式声明函数‘printf’ [-Wimplicit-function-declaration] hello.c:4:2: 警告: 隐式声明与内建函数‘printf’不兼容 [默认启用] hello.c:5:1: 警告: 在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type]
这个是什么问题呢?如何才能看到文字输出?
The implicit declaration is because the header file is not included: stdio.h
yours
printf
参数用的是%c
(字符),而你想要打印的是字符串应该用%s
。偏要打印%c
,可以用printf("%c",str[0]);
main
函数里没有定义返回值,默认为nt
, and there is no return value at the end of the program, so the promptActually, I think the error message is very obvious. As for %d, %c, %s, etc., the questioner should understand it himself.
If you can’t find the problem after reading this error message, then you need to reflect on it.
I have a few suggestions:
%c is the output character
%s is the output string
The code is like the classmate above
There is an error in the program. To use strings in C, use arrays