user4757345 user4757345 - 3 months ago 9
Linux Question

what is difference between include c file and giving as a parameter in C?

I have 2 c files. first is

main.c


#include <stdio.h>
//#include "file.c"

int main(int argc, char const *argv[])
{
fonk0(5);
fonk1();
return 0;
}


and second is
file.c


#include <stdio.h>
int aa;
void fonk0(int a)
{
aa=a;
}

void fonk1()
{
printf("aa is : %d \n", aa);
}


When I compile these files I use this command:

gcc -Wall main.c file.c -o main1


it gives warnings

main.c: In function ‘main’:
main.c:7:2: warning: implicit declaration of function ‘fonk0’ [-Wimplicit-function-declaration]
fonk0(5);
^
main.c:8:2: warning: implicit declaration of function ‘fonk1’ [-Wimplicit-function-declaration]
fonk1();
^


and
main1
file is 7104 bytes.

After that I added
#include "file.c"
line to
main.c
and compile it again with this command

gcc -Wall main.c -o main2


it gives no warnings

and
main2
file is 7072 bytes.

main1
and
main2
works correctly. So what is the difference between these ways ? Why
main1
and
main2
has different file size ? and which way I should use ?

I used
debian 8.5
and
gcc 4.9.2
.

Thanks.

Answer

You get the warning because there is no visible function declaration to the code in main. You need to create a header file "file.h" with the function prototypes inside, then include that header from main.c and file.c both.

Never #include .c files. It never makes sense and the need for doing so always originates from bad program design.

Comments