11

I am reading "The C programming Language" by Brian W. Kernighan and Dennis M. Ritchie. In chapter 1.2 "Variables and Arithmetic Expressions" they demonstrate a simple Fahrenheit to Celsius converter program. When I compile the program (Terminal.app, macOS Sierra), I get this warning:

$  cc FtoC.c -o FtoC.o
FtoC.c:5:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
1 warning generated.

This is the C program:

FtoC.c:
  1 #include <stdio.h>
  2 
  3 /* print Fahrenheit-Celsius table
  4     for fahr = 0, 20, ..., 300 */
  5 main()
  6 {
  7   int fahr, celsius;
  8   int lower, upper, step;
  9 
 10   lower = 0;      /* lower limit of temperature scale */
 11   upper = 300;    /* upper limit */
 12   step = 20;      /* step sze */
 13 
 14   fahr = lower;
 15   while (fahr <= upper) {
 16       celsius = 5 * (fahr-32) / 9;
 17       printf("%d\t%d\n", fahr, celsius);
 18       fahr = fahr + step;
 19   }
 20 }

If I understand this SO answer correctly, this error is the result of non-compliance with the C99 standard. (?)

The problem is not the compiler but the fact that your code does not follow the syntax. C99 requires that all variables and functions be declared ahead of time. Function and class definitions should be placed in a .h header file and then included in the .c source file in which they are referenced.

How would I write this program with the proper syntax and header information to not invoke this warning message?

For what it is worth, the executable file outputs the expected results:

$  ./FtoC.o 
0   -17
20  -6
40  4
60  15
80  26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148

This was useful:

return_type function_name( parameter list ) {
   body of the function
}

Also this overview of K&R C vs C standards, and a list of C programming books, or, for the C language standards.

Community
  • 1
  • 1
MmmHmm
  • 3,435
  • 2
  • 27
  • 49
  • 2
    Use `int main()` instead of just `main()`. Main should also return an integer value, usually 0 (or `EXIT_SUCCESS` if you include `stdlib.h`). –  May 06 '17 at 10:03
  • Does the linked answer not give you a hint? –  May 06 '17 at 10:03
  • 1
    Since you're passing a `-std=c99` flag, I think it will actually try to compile as ansi C (I think that's c89). No c99 issue here. But keep in mind that, while it's good, Kernighan & Ritchie is also old (I don't know if there are updated versions out there for current C standards & compilers); so some things will be, well, archaic. –  May 06 '17 at 10:04
  • 1
    Hint: get another book. – Jabberwocky May 06 '17 at 10:04
  • 1
    Note: the `.o` extension is usually used for object files. Which is not what you're creating here. Just use `cc FtoC.c -o FtoC`, or even just `cc FtoC.c` and run `./a.out` for simple test programs. –  May 06 '17 at 10:05
  • @InternetAussie I wasn't sure if I should be writing/incorporating `*.h` files and wasn't sure about what `int` meant prior to `main {}` but per Evert's comment, it is for a return value? – MmmHmm May 06 '17 at 10:17
  • 1
    @Mr.Kennedy Yes, it is for a return value. Zero for success, non-zero for failure. –  May 06 '17 at 10:18
  • @Evert thanks for the heads up. Any other C programming books you could recommend? – MmmHmm May 06 '17 at 10:20
  • @MichaelWalz Is there one you might recommend? – MmmHmm May 06 '17 at 10:20
  • 1
    @Mr.Kennedy In my opinion, K&R is still an excellent book to learn C. Although it is old, it covers the language very well, and teaches you all the basics you need to know. – Honza Remeš May 06 '17 at 10:30
  • 1
    Your code is not compilant with C89 standard either. Plain `main()` is not allowed by any C stanard. It is something from K&R C. – AnT stands with Russia May 06 '17 at 10:53
  • @AnT thank you, that is good to know! – MmmHmm May 06 '17 at 10:54
  • 1
    @HonzaRemeš: K&R apparently does **not** cover the language. C has evolved a lot since K&R and C90 times. Teaching people latin to communication in an english talking country is not really a good idea, although there are a lot of latin phrases in the english language. – too honest for this site May 06 '17 at 12:15
  • 1
    Get a more recent book about modern C. K&R is outdated since 18 years **at least**. – too honest for this site May 06 '17 at 12:16
  • @Olaf can you recommend a book about modern C? Thanks. – MmmHmm May 06 '17 at 12:44
  • There is a booklist on SO. How about searching yourself? I never used a book, the standard is sufficient **for me**. – too honest for this site May 06 '17 at 12:46

4 Answers4

14

Just give main a return type:

int main()

and make sure to add return 0; as the last statement.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
4

You can either tell the compiler to use the C language standard C90 (ANSI), which was modern when the book was written. Do it by using parameter -std=c90 or -ansi to the compiler, like this:

cc -ansi FtoC.c -o FtoC.o

or you can rewrite the program to adhere to a newer standard (C99), which is what your compiler uses by default, by adding a return type to the main function, like this:

int main()
Honza Remeš
  • 1,193
  • 8
  • 11
  • Thanks - I do plan on reading the whole book for the fundamnetals and historical value. – MmmHmm May 06 '17 at 10:42
  • 1
    Plain `main()` is not valid in C90 either. C90 does not allow declarations with empty *decl-specifier* sequences. – AnT stands with Russia May 06 '17 at 10:54
  • 1
    @AnT True, but the compilers (at least GCC) usually tolerate it in C90 mode. While it is not a good idea to write production code like this, I think it is OK to use the parameters to silence the compiler when studying example code in a book. – Honza Remeš May 06 '17 at 10:57
1

You should give return type for the main function. By default it takes int as its return type.

Either try,

int main() {
  //.....
return some_int_value;
}
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
1

Use int main() the function should return a value i.e. 0

int main()
{
 //.....
return 0;
}
Parthav
  • 21
  • 1
  • 7