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
intin C90. It is invalid C in later versions of the language.No
returnstatement.A function with a return type and no
returnstatement was not well-defined in C90. Writingmain()with noreturnbeing 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?
No, the programs in the K&R book were never conforming programs 1) (C17 4/7) under any verison of the standard.
returnstatement.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
intwas allowed in C90 both as the return type or as part of a (non-prototype) parameter list.Regarding no
returnstatement, 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.