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

前端 未结 8 1766
清酒与你
清酒与你 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:25

    Change your Main.c like so

    #include <stdlib.h>
    #include <stdio.h>
    #include "ClasseAusiliaria.h"
    
    int main(void)
    {
      int risultato;
      risultato = addizione(5,6);
      printf("%d\n",risultato);
    }
    

    Create ClasseAusiliaria.h like so

    extern int addizione(int a, int b);
    

    I then compiled and ran your code, I got an output of

    11
    
    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
提交回复
热议问题