ubuntu下编写C程序,如何看输出结果?
黄舟
黄舟 2017-04-21 10:55:21
0
6
538

如题,我在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]

这个是什么问题呢?如何才能看到文字输出?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
左手右手慢动作
#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]

大家讲道理
#include<stdio.h>
int main()
{
    const char *h="Hello World!";
    printf("%s\n",h);
    
    return 0;
}
左手右手慢动作

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.

大家讲道理

I have a few suggestions:

  1. Take some time to study this book carefully. http://book.douban.com/subject/113933...
  2. It is not recommended to use a Chinese environment, as it is not very beneficial for future reading and learning, etc. Personally, it is recommended to set the locale to en_US.UTF8.
  3. Debugging, of course, requires mastering tools such as gdb. There should be some very good articles in coolshell.
Ty80

%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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!