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

筅森魡賤 提交于 2019-11-26 20:54:03

问题


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.)



来源:https://stackoverflow.com/questions/12373538/warning-return-type-defaults-to-int-wreturn-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!