C89和c99 GCC编译器 [英] C89 vs c99 GCC compiler

查看:996
本文介绍了C89和c99 GCC编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用c89和c99编译以下程序,有什么区别吗?我得到相同的输出。两者之间真的有区别吗?

  #include< stdio.h> 

int main()
{
//打印字符串到屏幕。
printf(Hello World\\\
);
}

gcc -o helloworld -std = c99 helloworld.c
vs
gcc -o helloworld -std = c89 helloworld.c


解决方案


  • code>注释不是C89的一部分,但在C99中可以,

  • 不返回 main()任何值相当于C99中的 return 0; ,但在C89中不是这样。从 N1256 (pdf),5.1。 2.2.3p1:


    如果 main 函数的返回类型是兼容 int ,从初始调用到main函数的返回等同于调用 exit 函数, code> main 函数作为其参数;到达终止 main 函数的} 返回值为0.


    < blockquote>



因此,您的代码在C89中有未定义的行为,而在C99中定义的行为非常清楚。


Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two?

#include <stdio.h>

    int main ()
    {
      // Print string to screen.
      printf ("Hello World\n");
    }

gcc -o helloworld -std=c99 helloworld.c 
vs
gcc -o helloworld -std=c89 helloworld.c 

解决方案

  • // comments are not a part of C89 but are OK in C99,
  • falling off of main() without returning any value is equivalent to return 0; in C99, but not so in C89. From N1256 (pdf), 5.1.2.2.3p1:

    If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

So your code has undefined behavior in C89, and well-defined behavior in C99.

这篇关于C89和c99 GCC编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆