Each example that is done is completed with main omitting "int". Why is this and why does it still compile the same without it. Are C compilers created with int implied?
-
1K&R predates standard C – Govind Parmar Jan 30 '19 at 02:37
-
K&R 1st Edition from 1978 predates the C standard by more than a decade. It was one of the primary inputs to the standard, though. K&R 2nd Edition from 1988 precedes the C89 ANSI standard and C90 ISO standard by a little, but the standard was mostly stable by the time the book was published. To ensure that the existing code base was not rendered obsolete, the C89/90 standard did not mandate specifying a return type on function definitions, and allowed functions to be declared implicitly if the return type was `int`. C99 removed that laxness. K&R 2nd Edition shows its age on such issues. – Jonathan Leffler Jan 30 '19 at 03:47
2 Answers
Yep. Blow the dust off your white book, and you will see exactly that. K&R is what it is and it happened well before ANSI C.
Another difference is function arguments. If i recall correctly it was done like this
some_function_name(a,b)
int a;
long float b;
{
printf("%d %lf\n", a, b);
}
C and C++ have mutated a lot over the decades.

- 28,120
- 21
- 85
- 141
-
ANSI C89 still allows the return type to be omitted (C99 made it compulsory) – M.M Jan 30 '19 at 03:16
-
-
ANSI C came after K&R, and so did the mutations in C++. It is not possible for things in the future to affect things in the past, and therefore this does not answer the question “Why don't Kernighan and Ritchie include int for the main functions?” – Eric Postpischil Jan 30 '19 at 03:45
-
@Eric Postpischil K&R second edition book, is entitled ANSI C in red ink in my copy. Which is the book i used, I never looked at is first edition book. As stated below, int was the default type of what a fcn returns, if you do not include a return type. That said, they recommend always using a return type, or Void if no return type. The only place in the book, they don't include a return type is main. They have many `main(){` in the book. It automatically defaults to int. – Manny_Mar Jan 30 '19 at 04:21
-
@EvilTeach Although the first C book 1978 declared fcn like you just showed above. The second book 1988 ANSI C declares functions the same way we do now. – Manny_Mar Jan 30 '19 at 04:51
-
-
@EvilTeach well, `long float` is not allowed in C89, and not allowed by K&R2 (page 36). I don't have a copy of K&R1 handy , does it differ? – M.M Jan 30 '19 at 22:39
-
At the time Kernighan and Ritchie wrote, int
was the default type. Computing was in general simpler than it is today and had more constrained resources. Languages were designed to satisfy simple needs. And they were not completely designed; they evolved from scratch, with changes contributed by different people in different places with different things in mind. So the C at the time was not designed with modern ideas about type safety or being strict about grammar to reduce the frequency of errors. Some features put into it were things that people thought were nice and immediately useful, not necessarily things that had been analyzed for their deeper effects or future effects.
One motivating factor in language design was brevity. int
was a frequently used type, because it was intended to be the “natural” type for whatever machine was being used, so it was convenient to make it the default. Therefore, many declarations defaulted to int
, so it was not required.
Modern compilers may still allow the absence of int
so that old code can still be compiled. Often, such compilers have switches to be more strict about which language dialect(s) they accept. For example, in GCC and Clang, you can specify -std=c11
to request stricter conformance to the C standard. (And -std=c17
or -std=c18
may be available soon.) When writing new programs, you should use such switches so that your compiler will issue diagnostics about code that does not conform to modern standards. This will help write code that better conforms to the current standard, has more well-defined behavior, and is more likely to remain useful longer into the future.

- 195,579
- 13
- 168
- 312