In C, you cannot have the function definition/implementation inside the header file. However, in C++ you can have full method implementation inside the header file. Why is the behaviour different?
In C, if you define a function in a header file, then that function will appear in each module that is compiled that includes that header file, and a public symbol will be exported for the function. So if function additup is defined in header.h, and foo.c and bar.c both include header.h, then foo.o and bar.o will both include copies of additup.
When you go to link those two object files together, the linker will see that the symbol additup is defined more than once, and won't allow it.
If you declare the function to be static, then no symbol will be exported. The object files foo.o and bar.o will still both contain separate copies of the code for the function, and they will be able to use them, but the linker won't be able to see any copy of the function, so it won't complain. Of course, no other module will be able to see the function, either. And your program will be bloated up with two identical copies of the same function.
If you only declare the function in the header file, but do not define it, and then define it in just one module, then the linker will see one copy of the function, and every module in your program will be able to see it and use it. And your compiled program will contain just one copy of the function.
So, you can have the function definition in the header file in C, it's just bad style, bad form, and an all-around bad idea.
(By "declare", I mean provide a function prototype without a body; by "define" I mean provide the actual code of the function body; this is standard C terminology.)
C and C++ behave very much the same in this regard -- you can have
inline functions in headers. In C++, any method whose body is inside the class definition is implicitly
inline. If you want to do the same in C, declare the functions
The concept of a header file needs a little explanation:
Either you give a file on the command line of compiler, or do a '#include'. Most of the compilers accept a command file with extension c, C, cpp, c++, etc. as the source file. However, they usually include a command-line option to enable usage of any arbitrary extension to a source file also.
Generally the file given on the command line is called 'Source', and the one included is called 'Header'.
The preprocessor step actually take them all and makes everything appear like a single big file to the compiler. What was in the header or in the source is actually not relevant at this point. There is usually an option of a compiler which can show the output of this stage.
So for each file that was given on the compiler command line, a huge file is given to the compiler. This may have code/data which will occupy memory and/or create a symbol to be referenced from other files. Now each of these will generate an 'object' image. The linker can give a 'duplicate symbol' if the same symbol is found in more than two object files which are being linked together. Perhaps this is the reason; it's not advised to put code in a header file, which can create symbols in the object file.
The 'inline' are usually inlined .. but when debugging they may not be inlined. So why does the linker not give multiply defined errors? Simple... These are 'weak' symbols, and as long as all the data/code for a weak symbol from all the objects are same size and content, the linked will keep one copy and drop copy from other objects. It works.
You can do this in C99:
inline functions are guaranteed to be provided somewhere else, so if a function was not inlined, its definition is translated into a declaration (i.e., implementation is discarded). And, of course, you can use
Probably for the same reason that you must put the full method implementation inside the class definition in Java.
They might look similar, with squiggly brackets and many of the same keywords, but they are different languages.