warning: return type defaults to ‘int’ [-Wreturn-t

2019-01-15 12:36发布

问题:

I'm a Linux user who started learning C and I'm trying to compile this source that I typed:

#include <stdio.h>
main()
{
        float c,d;
        c = 10215.3;
        d = c / 3;
        printf("%3.2f\n",d);
        return 0;
}

It compiled with this using a makefile that I wrote:

cc -Wall -g     printf.c   -o printf

but I'm getting this warning:

printf.c:2:1: warning: return type defaults to ‘int’ [-Wreturn-type]

it compiles the code and I get the desired output but I want to understand what this means

回答1:

main()

should be

int main()

In C89, the default return type is assumed to be int, that's why it works.



回答2:

In C89, the default return type is int. This default was removed in C99 and compilers are helpful reminding you that your C-style with no int before main() is out of date.

See the C89 specification Section 3.5.2 "Type specifiers":

  • "Each list of type specifiers shall be one of the following sets: [...] + int, signed, signed int, or no type specifiers".

  • And in the second paragraph of semantics: "Each of the [...] sets designates the same type, except that for bit-fields [blabla]". So this means "no type specifiers" is the same as int.

In C99, the part "or no type specifier" is removed. (But you can still write signed without the int part.)