如何根据当前标准检查 C 源代码?

发布于 2024-09-25 00:40:02 字数 780 浏览 6 评论 0原文

我正在继续学习 C,并希望遵守当前的标准,但找到一个很好的参考似乎是个问题。

根据我在网上(主要是通过 Google 和 Wikipedia)发现的信息,当前使用的标准是 C99,更正式地说是 ISO/IEC 9899:1999 标准。

当我编写 C 代码时,我经常打开浏览器并进行简单的网络搜索,例如找出 stdio.h 函数 scanf 的确切返回值。大多数情况下,我只是想养成遵守当前标准的良好习惯,但即使我搜索特定的字符串“C99 printf”或类似的东西,似乎也没有一个地方可以找到明确的规范。

所以我有两个问题:

1)是否有一个可在线获取的中央 C99 规范,由负责该标准的组织维护?

[编辑]:第一个问题已经在这里得到解答:在哪里可以找到当前的 C 或 C++ 标准文档?。感谢詹姆斯·麦克内利斯指出了这一点。

2) 是否有一个程序可以解析 C 源文件以确保它符合 C99 规范?我知道有这样的程序可以解析 XHTML 文件,而且似乎也应该有一个用于 C99 的程序...

[编辑]: 我还应该提到我正在使用 gcc 进行 C 开发,特别是版本 3.4.4。 当我访问 gcc 主要网站 (http://gcc.gnu.org/) 时,我仍然难以确定哪个编译器版本支持哪个 C 规范。

I'm continuing to learn C and would like to adhere to whatever is the current standard, but finding a good reference to that seems to be problem.

From what I've found online (mostly through Google and Wikipedia) is that the current standard used today is C99, more formally the ISO/IEC 9899:1999 standard.

When I'm writing C code, I often bring up a browser and do simple web searches for things like finding out the exact return values to the stdio.h function scanf. Mostly I just want to get into a good practice of adhering to the current standard, but even if I search for the specific string "C99 printf" or something like it, there doesn't seem to be one single place to find a definitive spec.

So I have two questions:

1) Is there a central C99 spec that is available online, maintained by the organization responsible for this standard?

[edit]: This first question has already been answered here: Where do I find the current C or C++ standard documents?. Thanks to James McNellis for pointing this out.

2) Is there a program that can parse a C source file to make sure it adheres to the C99 spec? I know there are programs like this to parse XHTML files and it seems like there should be one for C99 as well...

[edit]:
I should also mention that I'm doing C development using gcc, specifically version 3.4.4.
When I go to the main gcc website (http://gcc.gnu.org/) I'm still running into difficulty figuring out which compiler version supports which C specification.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-10-02 00:40:02

当前的编程标准
语言 C 是 ISO/IEC 9899:1999,
发表于1999-12-01

已发布的 ISO 和 IEC 标准可以
从 ISO 成员机构购买或
国际电工委员会。

来自标准领域最优秀的伙伴,ISO

还值得注意的是,NEXT C 标准草案已发布 PDF 格式

The current standard for Programming
Language C is ISO/IEC 9899:1999,
published 1999-12-01

and

Published ISO and IEC standards can be
purchased from a member body of ISO or
IEC.

From your bestest buds in the standards world, ISO.

It's also worth noting the draft of the NEXT C standard is available in PDF.

忆悲凉 2024-10-02 00:40:02

根据您使用的编译器,您可以要求它为您检查。例如,使用 gcc,您可以像这样运行它:

gcc -Wall -Wextra -pedantic -std=c99 -o program program.c

当然,您可以将program.c 和program 替换为您的程序的名称。

-Wall-Wextra 提供更多警告,并告诉您是否做了一些奇怪的事情。 -pedantic 还提供了更多这样的功能。

如果你真的想个人遵循ansi规范,你可以使用-ansi,我不使用它,因为我很懒,但它更合适。

Depending on what compiler you use you can ask it to check for you. For example with gcc you would run it like this:

gcc -Wall -Wextra -pedantic -std=c99 -o program program.c

You would replace program.c and program with the name of your program of course.

-Wall and -Wextra provide more warnings and tell you if you have done something funky. -pedantic provides more of that as well.

you can use -ansi if you really want to follow the ansi spec personally I don't use it since I'm lazy but it is more proper.

薄凉少年不暖心 2024-10-02 00:40:02

实际上不可能分析源文件并最终确定它符合 C99 标准。 C 在这方面与 XHTML 不同,因为 C 是图灵完备的语言,而 XHTML 不是。

当然有可能找到许多个不合格实例,但不可能找到全部。例如,考虑一个即时生成 printf 格式字符串的程序 - 如何静态地确定格式字符串是否符合要求?如果它取决于给程序的输入怎么办?还考虑一个右移有符号整数的程序 - 如果所讨论的有符号整数永远不是负数,那么该程序可能符合要求,但如果不是,那么它可能依赖于实现定义的结果。

It is not actually possible to analyse a source file and conclusively determine that it complies with the C99 standard. C is different to XHTML in this regard, because C is a Turing-complete language and XHTML is not.

It is certainly possible to find many instances of non-conformance, but it's impossible to find them all. Consider, for example, a program that generates a printf format string on-the-fly - how can you statically determine that the format string will be conforming? What if it depends on the input given to the program? Consider also a program that right shifts signed integers - if the signed integer in question is never negative, then the program may be conforming, but if not then it is probably relying on implementation-defined results.

与风相奔跑 2024-10-02 00:40:02

草案可在此处免费获取。您还可以从ansi 商店购买官方版本。

The draft for can be obtained here for free. You can also purchase the official one from the ansi store.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文