Getting a “ return type defaults to 'int' ” warning when I declare Shortf function before int main(). Why? [duplicate]

☆樱花仙子☆ 提交于 2021-01-29 17:53:40

问题


#include <stdio.h>
int w=7,v=0;
shortf(short a, short *b)
{
    a++;(*b)++;w++;v++;
    printf("13: %d %d %d",a,v,w); return a;
}
int main()
{
    return 0;
}

This is part of my code. It was given to me by a teacher in my University, but when I write it I get 10+ warnings and errors, this is one of them. I get a " return type defaults to 'int' " warning for Line 4. Why? Note: I am only allowed to use C.


回答1:


You did not declare a return type for the function shortf. And so the compiler warned you that the default type of int will be used.

You should always declare a return value type. My guess is that the code has just been transcribed incorrectly. I think it should be:

short f(short a, short *b)



回答2:


You forget to declare the return type of your function shortf. In C89, if return type of a function is omitted, the function is presumed to return a value of int type (by default). It should be

short shortf(short a, short *b) {...}

In C99 and latter, it is illegal to omit the return type of a function.



来源:https://stackoverflow.com/questions/21079849/getting-a-return-type-defaults-to-int-warning-when-i-declare-shortf-functi

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