How to invoke function from external .c file in C?

前端 未结 8 1817
清酒与你
清酒与你 2020-12-23 19:31

My files are

// main.c  

#include \"add.c\"

int main(void) {
    int result = add(5,6);
    printf(\"%d\\n\", result);
}  

and

         


        
8条回答
  •  再見小時候
    2020-12-23 20:28

    There are many great contributions here, but let me add mine non the less.

    First thing i noticed is, you did not make any promises in the main file that you were going to create a function known as add(). This count have been done like this in the main file:

        int add(int a, int b); 
    

    before your main function, that way your main function would recognize the add function and try to look for its executable code. So essentially your files should be

    Main.c

        int add(int a, int b);
    
        int main(void) {
            int result = add(5,6);
            printf("%d\n", result);
        }  
    

    and // add.c

        int add(int a, int b) {
            return a + b;
        }
    

提交回复
热议问题