Understanding Function Prototype in C

后端 未结 3 918
太阳男子
太阳男子 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条回答
  •  萌比男神i
    2021-01-21 05:26

    Anything called as a function C is by default of type int, with no parameters (such as in your first case). If the compiler then finds a function which complies, there is no error.

    In the second case, the compiler compiles main() thinking the function is int, but then finds it's not true, and reports an error!

    COMMENT: Jonathan Leffler commented:

    Only in C89/C90. Not in C99; not in C11. Of course, some vendors still only implement C89; a notable example is Microsoft!

提交回复
热议问题