原文链接:【中英双语】C语言编程标准


The idea of this article is to introduce C standard.

本文的思路是介绍C标准。

What to do when a C program produces different results in two different compilers?

当一个C程序在两个不同的编译器中产生不同的结果时,该怎么办?

For example, consider the following simple C program.

例如,请考虑以下简单的 C 程序。

1 void main() {  }

The above program fails in gcc as the return type of main is void, but it compiles in Turbo C. How do we decide whether it is a legitimate C program or not?

上述程序在gcc中编译失败,因为main的返回类型是无效的,但它可以在Turbo C中编译。我们如何决定它是否是一个合法的C程序?

Consider the following program as another example. It produces different results in different compilers.

将以下程序视为另一个示例。它在不同的编译器中产生不同的结果。

 1#include<stdio.h>2int main()3{4    int i = 1;5    printf("%d %d %d\n", ++i, i++, i);67    return 0;8}9
10/*output
112 1 3 - using g++ 4.2.1 on Linux.i686
121 2 3 - using SunStudio C++ 5.9 on Linux.i686
132 1 3 - using g++ 4.2.1 on SunOS.x86pc
141 2 3 - using SunStudio C++ 5.9 on SunOS.x86pc
151 2 3 - using g++ 4.2.1 on SunOS.sun4u
161 2 3 - using SunStudio C++ 5.9 on SunOS.sun4u
17*/

Which compiler is right?

The answer to all such questions is C standard. In all such cases, we need to see what C standard says about such programs.

哪个编译器是正确的?

所有这些问题的答案都是C标准。在所有这些情况下,我们需要看看C标准对此类程序的看法。

What is C standard?

The latest C standard is ISO/IEC 9899:2011, also known as C11 as the final draft was published in 2011. Before C11, there was C99. The C11 final draft is available here. See this for a complete history of C standards.

什么是C标准?(标准文档见文末)

最新的C标准是ISO/IEC 9899:2011,也称为C11,因为最终草案于2011年发布。在C11之前,有C99。C11 最终草案可在此处获取。有关 C 标准的完整历史,请参阅此内容。

Can we know the behavior of all programs from C standard?

C standard leaves some behavior of many C constructs as undefined and some as unspecified to simplify the specification and allow some flexibility in implementation. For example, in C the use of any automatic variable before it has been initialized yields undefined behavior and order of evaluations of subexpressions is unspecified. This specifically frees the compiler to do whatever is easiest or most efficient, should such a program be submitted.

我们可以从C标准中知道所有程序的行为吗?

C标准将许多C构造的某些行为保留为未定义,一些行为未指定,以简化规范并允许实现的一些灵活性。例如,在 C 中,在初始化之前使用任何自动变量都会产生未定义的行为,并且子表达式的计算顺序未指定。这特别释放了编译器,以便在提交此类程序时执行最简单或最有效的操作。

So what is the conclusion about above two examples?

Let us consider the first example which is “void main() {}”, the standard says following about prototype of main().

那么,关于上述两个例子的结论是什么呢?

让我们考虑第一个例子,即“void main() {}”,标准说下面关于main()的原型。

The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names
may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner./*
程序启动时调用的函数被命名为 main。实现 没有为此函数声明任何原型。它应该用返回 类型的 int 定义,并且没有参数: int main(void) { /* ... */ } 或两个参数(这里称为 argc 和 argv,尽管可以使用任何名称 ,因为它们是声明它们的函数的本地名称): int main(int argc, char *argv[]) { /* ... */ } 或等效;10) 或以某种其他实现定义的方式。
*/

So the return type void doesn’t follow the standard, and it’s something allowed by certain compilers.

Let us talk about second example. Note the following statement in C standard is listed under unspecified behavior.

因此,返回类型 void 不遵循标准,这是某些编译器允许的。

让我们谈谈第二个示例。请注意,C 标准中的以下语句列在“未指定的行为”下。

The order in which the function designator, arguments, and
subexpressions within the arguments are evaluated in a function
call (6.5.2.2). /*
在函数 调用 (6.5.2.2) 中计算参数中的函数指示符、参数和子表达式的顺序。
*/

What to do with programs whose behavior is undefined or unspecified in standard?

As a programmer, it is never a good idea to use programming constructs whose behavior is undefined or unspecified, such programs should always be discouraged. The output of such programs may change with the compiler and/or machine.

如何处理行为未定义或未在标准中指定的程序?

作为程序员,使用行为未定义或未指定的编程结构绝不是一个好主意,应始终不鼓励使用此类程序。此类程序的输出可能会随编译器和/或机器而更改。

参考资料:

[1]ISO/IEC 9899:2011 - C11 (C standard revision)

https://en.wikipedia.org/wiki/C11_(C_standard_revision)

[2]C99

https://en.wikipedia.org/wiki/C99

[3]C (programming language) history

https://en.wikipedia.org/wiki/C_(programming_language)#History

END


扫描下方二维码

添加好友,备注【英语】

可私聊交流,也可进资源丰富学习群

原文链接:【中英双语】C语言编程标准

【中英双语】C语言编程标准相关推荐

  1. 中英双语多语言外贸企业网站源码系统 - HanCMS - 安装部署教程

    随着跨境独立站的流行,中英双语的公司官网越来越受到重视. 此项目是基于开源CMS开发出的中英文双语外贸企业网站内容管理系统,命名HanCMS HanCMS 汉CMS中英双语多语种外贸网站系统,是一个轻 ...

  2. 编程的修炼(中英双语)

    编程的修炼(中英双语)(图灵奖获得者EdsgerW. Dijkstra 是每个在计算机领域学习和工作的人都应该了解和尊重的先驱者,本书为他最重要的述著,堪称编程领域里,经典著作的经典!) [荷]Eds ...

  3. 【中英双语】C语言介绍

    这是双语编程学习 - 「C语言篇」的第「1」篇文章,感谢关注,点赞,转发! 原文链接:[中英双语]C语言介绍 C is a procedural programming language. It wa ...

  4. 2008年《斯坦福大学开放课程: 编程方法学》(Open Stanford Course : Programming Methodology )[人人影视中英双语字幕][WMV]

    <斯坦福大学开放课程: 编程方法学>(Open Stanford Course : Programming Methodology )[人人影视中英双语字幕][WMV] http://ww ...

  5. 2010年《斯坦福大学开放课程: 编程方法》(Open Stanford Course : Programming Methodology)[中英双语字幕][RMVB]更新中

    <斯坦福大学开放课程: 编程方法>(Open Stanford Course : Programming Methodology)[中英双语字幕][RMVB] http://www.ver ...

  6. 【中英双语】高级Scala函数式编程

    [中英双语]高级Scala函数式编程 成为顶级 Scala 程序员,这样您就可以使用 Spark.Akka.Cats 或任何 Scala 工具!此教程共13.5小时,中英双语字幕,画质清晰无水印,源码 ...

  7. Python Tutorial中英双语对照文档5

    Python Tutorial中英双语对照文档4 CHAPTER THIRTEEN WHAT NOW? 现在咧? Reading this tutorial has probably reinforc ...

  8. Python Tutorial中英双语对照文档2

    接 Python Tutorial中英双语对照文档1 CHAPTER SIX MODULES 模块 If you quit from the Python interpreter and enter ...

  9. Python Tutorial中英双语对照文档3

    接 Python Tutorial中英双语对照文档2 CHAPTER NINE CLASSES 类 Classes provide a means of bundling data and funct ...

最新文章

  1. 写文章 使用conda管理python环境
  2. 从一点儿不会开始——Unity3D游戏开发学习(一)
  3. Java主线程等待子线程、线程池
  4. SAP CRM Product ACE check调试
  5. 技术专家:为什么我们最终选择Apache Pulsar替代Kafka?
  6. Linux 根文件系统目录结构与功能,4.Linux根文件系统和目录结构及bash特性
  7. 谷歌Chrome浏览器开发者工具教程—基础功能篇
  8. 苹果mac视觉效果和动态图形设计软件:After Effects 2022 (ae 2022)
  9. Linux下TCP Socket编程实例
  10. Spring boot 整合WebSocket
  11. 如何利用python监控主机存活并邮件、短信通知
  12. python淘宝自动发货源码_发货100虚拟商品自动发货系统下载
  13. eclipse-embed
  14. 一文弄懂BIN、HEX、AXF、ELF文件格式的区别
  15. ibmx3400M3服务器装系统教程,免费安装系统!IBM x3400 M3不足1万2
  16. docker-compose up:ERROR: Encountered errors while bringing up the project.报错问题
  17. java中映射关系Map
  18. 多分类问题中sklearn的RFECV指定scoring函数
  19. apply方法和unapply方法
  20. 计算机在医学教学,计算机辅助教学在医学高校的运用

热门文章

  1. 计算机开源期刊电子和计算机方向的
  2. PIC单片机芯片引脚常用符号的功能说明
  3. 百度链接提交自动推送JS代码版
  4. Cucumber使用进阶
  5. 性能测试计划包含那些内容?这份从零开始实施的性能测试指南千万不能错过
  6. 计算机电源 方案,PC电源功率如何确定? 电源选择的终极方案来了~
  7. 将本地代码上传gitlab操作
  8. 软件工程复习重点知识
  9. C语言怎么消除前置0,C语言如何把转换进制数前面的0去掉呢?
  10. 采购人员在下采购订单时,如何快速准确选择可替代料