How can I not raise a warning: missing type specifier?

I'm 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 program. When I compile a program (Terminal.app, macOS Sierra), I 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 a 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 not adhering to the C99 standard. (?)

The problem is not with the compiler, but that your code doesn't match the syntax. C99 requires all variables and functions to be declared in advance. Function and class definitions must be placed in a .h header file and then included in the .c source file in which they are specified.

How can I write this program with the correct syntax and header information so as not to raise this warning?

For what it's worth, the executable 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 helpful:

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

      

See also this overview of K & RC vs C standards and the book's C programming list , or for the C locales.

+3


source to share


4 answers


Just put in the main

return type:

int main()

      



and be sure to add return 0;

as the last operator.

+5


source


You can either tell the compiler to use the C90 CSI (ANSI) standard, which was modern when the book was written. Do it with a parameter -std=c90

or -ansi

for the compiler, for example:

cc -ansi FtoC.c -o FtoC.o

      



or you can rewrite the program to conform to the new standard (C99) that your compiler uses by default by adding the return type to the function main

, for example:

int main()

      

+2


source


You must specify the return type for the main function. It accepts by default as its return type int

.

Try,

int main() {
  //.....
return some_int_value;
}

      

+1


source


Use int main () , the function should return ie 0

int main()
{
 //.....
return 0;
}

      

+1


source







All Articles