I began to learn the language of S. I want to find out why in the book "C programming language" Kernighan and Ritchie the program looks like this:

#include <stdio.h> /* печать таблицы температур по Фаренгейту и Цельсию для fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* нижняя граница таблицы температур */ upper = 300; /* верхняя граница */ step = 20; /* шаг */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf ("%d\t%d\n", fahr, celsius); fahr = fahr + step; } } 

There are no arguments in the main() function. At compilation the error gets out. But when I wrote this program using SumlimeText3, he suggested doing so int main(int argc, char const *argv[])

Why it happens? Why the book is not the right program?

PS If I compile using gcc compiler in Ubuntu 16.04.2

  • You do not use any environment variables, so it is not so important that you do not have argc and argv . Indicate which error appears during compilation. - isnullxbh
  • Maybe you have not an error, but a warning ( warning )? - eanmos
  • github.com/anotherlin/tcpl (Read the readme there) - 0andriy
  • You are inventing something. The compile error here, of course, should come out, but not because the main() function has no arguments. Your arguments are just fine. - AnT

2 answers 2

Let's compare the functions:

  1. main()
  2. int main(int argc, char const *argv[])

The first difference is that the function type ( int ) is not specified. This is not an error; if you do not specify a type, the compiler will itself substitute an int .

The second is not arguments int argc, char const *argv[] . These are command line arguments, you only need to write them if you are going to use them. ( here is the detailed answer to what it is and why - the link ).

So the program is correct, although not quite familiar.

PS

Personally, my program is compiled, but it gives one warning (which was written above):

 warning: return type defaults to 'int' main() ^ 

As was indicated below in the comment 0andriy , you can get rid of this warning if you specify the flag std=c89 during compilation.

  • The second is superfluous. Use the instructions to the compiler that the source code is ANSI C. - 0andriy

Even the second (most recent) edition of Kernigan and Ritchie’s book C Programming Language was written and published before C was standardized for the first time. After the release of the first standard of the language, the text of the book was corrected in hindsight, but most of the code examples used in the book did not affect these edits. Since then, the language standard has changed, but the book, of course, has not been brought into line with the new C standards. For this reason, this book today represents only mainly historical / entertainment value. And code samples in it and earlier were low-quality / obsolete.

As for the example you cited, if you close your eyes to the numerous stylistic flaws, it is obviously only the declaration of the main function that is erroneous. It relies on two [related] properties of the standard C language: the "implicit int" rule and the "empty decl-specifier-seq" rule. Namely, in standard C, ads of the form were allowed.

 a = 42; /* Пустая decl-specifier-seq */ foo(); /* Пустая decl-specifier-seq */ main(); /* Пустая decl-specifier-seq */ 

They are, however, prohibited in the standard C language, starting from the very first of its standard. The first standard of C, adopted in 1989 (the so-called C89 / 90, aka "ANSI C") retained the rule of "implicit int", but canceled the possibility of using "empty decl-specifier-seq", which made the above-mentioned declarations illegal . In C89 / 90 you can declare, for example,

 const a = 42; /* Непустая decl-specifier-seq, но подразумевается неявный `int` */ static foo(); /* Непустая decl-specifier-seq, но подразумевается неявный `int` */ extern main(); /* Непустая decl-specifier-seq, но подразумевается неявный `int` */ 

but by no means just foo() , main() or a . The modern language C (starting with C99) no longer supports the rule of "implicit int", i.e. In the modern C type in the ad, you must always indicate clearly

 const int a = 42; static int foo(); int main(); 

You may also notice that in the C language it is not recommended to use function declarations with empty brackets () . Such ads are obsolete (obsolescent) from the very first language standard, although no one has yet dared to ban them completely. Functions without parameters in C should be declared as (void) , and not as () .

And you can also notice that the absence of an explicit return in the main function in C89 / 90 leads to the fact that the value returned by the main function is not defined. Only starting from C99, the completion of main by the last closing parenthesis implies an implicit return 0; .

  • Remarkable answer. Plus :) - isnullxbh