samedi 1 août 2015

Error in c program when not using header file

Scenario :
A c application created in netbeans ide with below two files

some_function.c

#include <stdio.h>
int function_1(int a, int b){
    printf("Entered Value is = %d & %d\n",a,b);
    return 0;
}

newmain.c

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    //function_2(); //Error //function name not allowed
    function_1();
    function_1(1);
    function_1(1,2);
    return (EXIT_SUCCESS);
}

When learning the need of the header file in a c program, I tried the above application (as it is). It got compiled and gave the output as below

Entered Value is = 4200800 & 102
Entered Value is = 1 & 102
Entered Value is = 1 & 2

Question 1 : Is my assumption correct, that when linking, "the linker will check for the function name and not the arguments" when the header file not used?

Regarding the header file usage, I came across this link and there it said as, we can include the c file itself using the #include. So i used the below line in the file newmain.c

#include "some_function.c"

As expected it shown the below error

error: too few arguments to function 'function_1()'
error: too few arguments to function 'function_1(1)'

And also i got the below (unexpected) error.

some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here

Question 2: What error I did when including the 'c' file itself, as it gives the above said (unexpected) error?

Aucun commentaire:

Enregistrer un commentaire