Home  >  Q&A  >  body text

ubuntu下编写C程序,如何看输出结果?

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

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

黄舟黄舟2759 days ago577

reply all(6)I'll reply

  • ringa_lee

    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]

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-21 10:57:21

    #include<stdio.h>
    int main()
    {
        const char *h="Hello World!";
        printf("%s\n",h);
        
        return 0;
    }

    reply
    0
  • ringa_lee

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-21 10:57:21

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-21 10:57:21

    %c is the output character
    %s is the output string

    The code is like the classmate above

    reply
    0
  • 怪我咯

    怪我咯2017-04-21 10:57:21

    There is an error in the program. To use strings in C, use arrays

    reply
    0
  • Cancelreply