Can't we include the .c file?

Today I had an interview, there they asked me, can we include .c filein the source file? I said yes. Since a few years ago I saw the same thing in some kind of project in which they include .c file. But now I tried to do the same.

abc.c

#include<stdio.h>
void abc()
{ printf("From ABC() \n"); }


main.c

#include<stdio.h>
#include "abc.c"
int main()
{   void abc();
    return 0;
}

Getting error:

D:\Embedded\...\abc.c :- multiple definition of 'abc' >

Where is this going?

I wrote the file abc.h (the body of abc.h is equal { extern void abc(void); }), and included the file in abc.c(by commenting #include abc.c). It works great.

+5
source share
4 answers

Do it as follows:

abc.c:

#include <stdio.h>
void abc()
{printf("From ABC() \n");}

main.c:

#include<stdio.h>
#include "abc.c"
int main()
{   
    abc();
    return 0;
}

(no need for header file)

, , main.c. abc.c, main.c, abc(), .

, #include "copy-paste", . #include "abc.c", abc.c "" main.c. , main.c, , , main.c ( #include <stdio.h> s):

#include<stdio.h>
#include <stdio.h>
void abc()
{printf("From ABC() \n");}
int main()
{   
    abc();
    return 0;
}

.

, ; .c .

+14

C , C , C, .

, ( ), , .

, , . ( ).

+5

.

void abc(); main(). abc ();

+4

You can include whatever you like, the preprocessor does not care about file extensions. Only some traditions call the headers “.h” and the source files “.c” or “.cpp”.

You only need to be sure that after compiling the entire project, you will not encounter linker problems (for example, providing the compiler "abc.c" and "main.c" will lead to several definitions of your function).

+2
source

All Articles