The behavior of a C compiler with old-styled functions without prototypes

后端 未结 3 1900
自闭症患者
自闭症患者 2020-12-19 12:24

When my program consists of two files:

main.c

#include 

int main(void) { 
     printf(\"%lf\\n\",f());   
     return 0;
 }
         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 13:01

    C will assume a function has the prototype int func(); unless you have told it otherwise.(Note that in C int func(); and int func(void); are different things)

    In your second case, you do a call to f() for which the compiler hasn't seen any prototype, so it assumes it is int f(); . Later on it sees your definition for f() which has a different prototype - and issues an error.

    This doesn't happen in the 1. case, as they're in different compilation units.

提交回复
热议问题