K& r的代码示例是否曾经符合?
Brian Kernighan和Dennis Ritchie的C编程语言包含了许多示例,例如此著名的一种(K& r第二版1.1):
#include <stdio.h>
main()
{
printf("hello, world\n");
}
我在这里注意到以下问题:
否返回类型。
在C90中允许使用不返回类型的写作功能,第二版本书声称要遵循。这些将在C90中默认为
int
。在语言的后期版本中,它是无效的。否
返回
语句。具有返回类型的函数,否
返回
语句在C90中没有很好地定义。写main()
没有返回
等效于返回0;
是C99中添加的功能。空的参数列表
main()
。这是有效的C静止(从C17开始),但即使在C90中也一直是过时的功能。 (即将到来的C23谈论最终摆脱K&amp; r样式功能。)
我的问题:
k&amp; r第二版中的任何代码ever a e conforming程序在任何版本的标准中?
The C Programming Language by Brian Kernighan and Dennis Ritchie contains a lot of examples such as this famous one (K&R 2nd edition 1.1):
#include <stdio.h>
main()
{
printf("hello, world\n");
}
Here I note the following issues:
No return type.
Writing functions with no return type was allowed in C90 which the second edition of the book claims to follow. These will default to
int
in C90. It is invalid C in later versions of the language.No
return
statement.A function with a return type and no
return
statement was not well-defined in C90. Writingmain()
with noreturn
being equivalent toreturn 0;
was a feature added in C99.Empty parameter list of
main()
.This is valid C still (as of C17) but has always been an obsolescent feature even in C90. (Upcoming C23 talks of finally getting rid of K&R style functions.)
My question:
Was any code in K&R 2nd edition ever a conforming program, in any version of the standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据定义,通过符合C实现“接受”其“接受”其“接受”的任何源文本或集合都是“符合C程序”。因为实现了广泛的纬度,以任何不影响任何严格符合C程序的行为的方式扩展语言,所以任何不符合C程序的源文本t可以通过修改A来将其变成符合C程序符合C实施,以便如果给出了与t不匹配的程序,则它将正常处理,如果添加了T的副本,它的行为就会好像被给予了其他可以接受的其他程序。
尽管这似乎是一个荒谬的定义,但它满足了C标准委员会的主要目标之一,这是为了确保如果有任何现有计划可以完成任务,则可以通过符合C计划来完成任务。
至于该程序是否严格符合C89,这很难回答。该标准说,如果执行在
main()
的末尾都将返回到主机环境中,并且对这样做的后果没有任何要求,这将表明此类操作会调用不确定的行为。另一方面,标准也 对如果程序返回exit_success
,也不会议如果返回其他值。因此,所有这些行动都可以看作是援引未定义的行为。另一方面,以这种方式查看事物将使任何终止严格符合的程序都不可能。我认为解释标准的最合理方法是说,执行过程中执行的程序放弃了其可能不得不影响执行环境终止的任何控制。如果所有行动方案在计划退出同样可以接受之后可以执行的行动方案,则程序未能做任何事情来影响采取哪种行动的任何事情将不是缺陷。
在考虑未能指定返回值的程序或该问题的任何程序是“严格符合”的程序时,不能仅仅检查源文本,但也必须考虑应用程序要求。如果一个人需要一个程序来输出字符x和y一次,以某种顺序将以下是一个严格符合的程序,该程序可以完成:
如果另一方面,一个人需要一个程序来输出“ xy”,则为此目的,绝不是一个严格符合的程序。因此,我要说的是,如果某些任务的应用程序要求指定程序必须使用其返回值来影响主机环境,那么落在MAIN结束的程序将不是严格符合C程序来完成该任务。但是,如果对主机环境的这种影响不是任务应用程序要求的一部分,那么严格符合的C程序可以放弃这种控制。
引用下面:
N1570第4节第7节:
未定义的行为定义为3.4.3:
C99理由没有任何要求,谈论符合的定义[强调原始]:
程序未设置返回值的程序退出,这一事实可能使其不可存储,但是该标准是故意避免通过称其为不合格的“贬低”不可携带的程序。
By definition, any source text or collection thereof which is "accepted" by a Conforming C Implementation is a "Conforming C Program". Because implementations are given broad latitude to extend the language in any way which does not affect the behavior of any Strictly Conforming C Programs, any source text T which would not otherwise be a Conforming C Program could be turned into a Conforming C Program by modifying a Conforming C Implementation so that if it were given program that doesn't match T, it would process it normally, and if fed a copy of T it would behave as though it were fed some other program that it would accept.
While this may seem an absurdly broad definition, it satisfies one of the major goals of the C Standards Committee, which was to ensure that if any existing programs could accomplish a task, the task could be accomplished by a Conforming C Program.
As for whether the programs were Strictly Conforming under C89, that's a bit harder to answer. The Standard says that if execution falls through the end of
main()
it will return an Undefined Value to the host environment, and imposes no requirements about the consequence of doing so, which would suggest that such an action would invoke Undefined Behavior. On the other hand, the Standard also imposes no requirements upon what happens if a program returnsEXIT_SUCCESS
, nor what happens if it returnsEXIT_FAILURE
, nor if it returns some other value. Thus, all such actions could be viewed as invoking Undefined Behavior. On the other hand, viewing things in such fashion would make it impossible for any program which terminates to be Strictly Conforming.I think the most reasonable way of interpreting the Standard would be to say that a program whose execution falls through the end of main() waives any control it might have had to affect what the execution environment does once it terminates. If all courses of action the host environment could perform after a program exits would be equally acceptable, a program's failure to do anything to influence which course of action is taken would not be a defect.
In considering whether a program that fails to specify a return value, or any program for that matter, is "Strictly Conforming", one cannot merely examine the source text, but must also consider the application requirements. If one needs a program to output the characters x and y once each, in some order, the following would be a strictly conforming program that accomplishes that:
If, on the other hand, one need a program to output "xy", the above would not be a strictly conforming program for that purpose. Thus, I would say that if the application requirements for some task specify that a program must use its return value to influence the host environment, a program that falls through the end of main would not be a Strictly Conforming C Program to accomplish that task. If, however, such influence over the host environment is not part of the application requirements for a task, then a Strictly Conforming C Program could waive such control.
Citations below:
From N1570 section 4 pararaph 7:
Undefined Behavior is defined in 3.4.3:
From the C99 Rationale, talking about the definition of conformance [emphasis original]:
The fact that a program exits without setting a return value may make it non-portable, but the Standard deliberately avoids "demeaning" non-portable programs by calling them non-conforming.
编译了该程序
我在两个备受赞誉的编译器下 ,
gcc
和clang
。只是为了好玩,我还添加了- pedantic
选项。据我所知,这两个编译器都将被视为“符合”,我相信这是他们的作者当然要努力的事情之一。两个编译器都产生了一个可执行的可执行文件,该可执行文件打印
Hello,World
。根据定义我得出结论,
。我没有对该计划是否会根据C89符合的判断。
尽管我没有多年来研究过K&amp; r2中的代码示例,但我认为,尽管大多数/所有人都同样符合符合的代码,尽管各种教学或其他捷径可能会使它们不符合严格符合。
I compiled the program
under two well-regarded compilers,
gcc
andclang
. Just for fun I added the--pedantic
option also. As far as I know both of these compilers would be considered "conforming", and I believe that's one of the things their authors certainly strive for.Both compilers produced an executable which printed
hello, world
. Under the definition that, I conclude that the program is conforming.
I pass no judgement on the question of whether the program would have been conforming under C89.
Although I have not studied the code examples in K&R2 in some years, I believe that most/all of the rest of them are similarly conforming, despite various pedagogical or other shortcuts which might render them not strictly conforming.
一些引号:
。
c 换句话说,K&amp; r Edition 1早于任何官方C标准。当时,唯一的规范是Dennis M. Ritchie的“ C参考手册”。
(source wikipedia> wikipedia
)第一官方ANSI C标准,也称为C89。
但是,当时K&amp; r Edition 2出版了,C89尚未完成。根据
。 =“ nofollow noreferrer”> wikipedia )
因此,它们可能是k&amp; r所说的内容与ANSI C标准之间的差异。
Some quotes:
(source Wikipedia)
In other words, K&R edition 1 predates any official C standards. At the time the only specification was "The C Reference Manual" by Dennis M. Ritchie.
(source Wikipedia)
In other words, K&R edition 2 was "aligned with" the first official ANSI C standard otherwise known as C89.
However, at the time K&R edition 2 was published, C89 was not yet complete. According the the Wikipedia page on ANSI C.
(source Wikipedia)
Thus, they may be differences between what K&R says and the ANSI C standard.
不,K&amp; r Book中的程序从来都不是符合程序 1)(C17 4/7),根据标准的任何经验。
返回
语句,代码调用了未定义的行为。int
,代码不会编译。以下资源。
关于隐式
int
,可以在此处找到C90和后者版本之间函数返回类型的一个主要差异:C90 6.7.1功能定义
C17 6.9.1功能定义
主要区别是“完整的对象类型”措辞,完整对象类型的定义是基本类型之一或指针之一(C17 6.2.5)。我们可以得出结论,在C90中允许隐式
int
作为返回类型或(非概率)参数列表的一部分。关于
返回
语句,此文本始终存在用于一般函数:C90 6.6.6.4
C17 6.9.1/12
中添加了例外情况。
但是,
main()
是一种特殊情况,在C99: C99 5.1.2.2.3而在C90中,等效文本说:
C90 5.1.2.2.3
关于空参数列表,它已被标记为从C90到C17的过时。请参阅未来的语言方向,例如C17 6.11(或C90 6.9,相同的文本):
但是,这并不意味着使用该功能的代码不符合符合,至少直到ISO 9899:2018。这根本不是建议的练习,也不是在发布K&amp; r第二版的时候推荐的练习。
1) C17来自CHP 4:
这意味着一个符合程序可能会使用不可存储的符合实现的功能,但是它可能不会通过例如在标准中调用不确定的行为来改变严格符合程序的行为。
No, the programs in the K&R book were never conforming programs 1) (C17 4/7) under any verison of the standard.
return
statement.int
.Sources below.
Regarding implicit
int
, one major difference in function return types between C90 and latter versions can be found here:C90 6.7.1 Function definitions
C17 6.9.1 Function definitions
The main difference being the "complete object type" wording, the definition of complete object type being one of the basic types or a pointer to one (C17 6.2.5). We can conclude that implicit
int
was allowed in C90 both as the return type or as part of a (non-prototype) parameter list.Regarding no
return
statement, this text was always there for general functions:C90 6.6.6.4
C17 6.9.1/12
However,
main()
is a special case and an exception was added in C99:C99 5.1.2.2.3
Whereas in C90, the equivalent text says:
C90 5.1.2.2.3
Regarding empty parameter lists, it has been marked as obsolescent from C90 to C17. See future language directions, for example C17 6.11 (or C90 6.9, identical text):
This does however not mean that code using the feature isn't conforming, at least up to ISO 9899:2018. It's simply not recommended practice, and was not recommended practice at the point where K&R 2nd edition was released either.
1) C17 from chp 4:
This means that a conforming program may use features of a conforming implementation that are non-portable, but it may not alter the behavior of a strictly conforming program by for example invoking undefined behavior explicitly listed as such in the standard.