Understanding Function Prototype in C

后端 未结 3 925
太阳男子
太阳男子 2021-01-21 05:07

Why does the following program works fine?

int main()
{
    int x;
    x = foo();
    printf(\"%d\",x);
    getchar();
    return 0;
}

int foo()
{
    return 2;         


        
3条回答
  •  醉酒成梦
    2021-01-21 05:39

    In C if a function is defined then its implicit return type is int.

    • In the first case the return type of the function is int so the main() recognizes the function and compiles without any errors.

    • In second case the return type of function is double so the main() fails to recognize the function thus generating an error so you need to declare the prototype of function.

    Also in older versions up till C89 if the return type is not mentioned then its is implicitly considered as int.

    In C99 standard doesn’t allow return type to be omitted even if return type is int.

    For more details you can check: implicit return type C

提交回复
热议问题