如题,我在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]
这个是什么问题呢?如何才能看到文字输出?
ringa_lee2017-04-21 10:57:21
#include <stdio.h>
int main(int argc,char *argv[])
{
char str[] = "hello world";
printf("%s\n",str);
return 0;
}
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 prompt
Warning: In a function with a return value, control flow reaches the end of the function [-Wreturn-type]
大家讲道理2017-04-21 10:57:21
#include<stdio.h> int main() { const char *h="Hello World!"; printf("%s\n",h); return 0; }
ringa_lee2017-04-21 10:57:21
Actually, 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.
大家讲道理2017-04-21 10:57:21
I have a few suggestions:
PHP中文网2017-04-21 10:57:21
%c is the output character
%s is the output string
The code is like the classmate above