- If i have a header file in which i declare and define a function, and i include the header in a source file, will it return an error? and if yes which one? I tried doing it and it worked but when someone else tried it on his PC, it didn't. so in which step did the problem occur? is it the pre-processing or linking part?
- If I define the same function twice (twice in the source file or once in the header and another in the source), what type of error is that? I need to know it for an exam but couldn't find answers myself

- 117
- 6
-
21) No, you'll be fine. But if you include that header in *two* source files, and try to link them, then you'll get a linker error. 2) What is it *called?* I don't know. – Beta Jun 23 '20 at 14:06
-
@Beta it is not OK. – 0___________ Jun 23 '20 at 14:20
-
"when someone else tried it on his PC, it didn't" lacks much information. What error message occurred? – chux - Reinstate Monica Jun 23 '20 at 14:46
3 Answers
- I think you should first gather some information about the subject of linkage.
- Then you should do some research on header file descriptions.
- Finally, you should learn the undefined behavior.
- Linkage
- Linkage 2
- Header File Create
- Undefined Behavior / UB

- 68
- 10
If i have a header file in which i declare and define a function, and i include the header in a source file, will it return an error? and if yes which one? I tried doing it and it worked but when someone else tried it on his PC, it didn't. so in which step did the problem occur? is it the pre-processing or linking part?
It depends. If you don't have any include guards around the function definition and the file gets #include
d more than once within the same translation unit, or if the name of the function defined in the header conflicts with another variable or function name defined elsewhere in the source, then yes you'll get some kind of duplicate or conflicting definition error. If nothing in the header conflicts with other code, you won't. Without knowing the details of your code or your friend's code, there's really no way to answer definitively.
Short answer is "don't do that" - don't put function or variable definitions in header files. That way you avoid this kind of problem completely.
If I define the same function twice (twice in the source file or once in the header and another in the source), what type of error is that? I need to know it for an exam but couldn't find answers myself
Most compilers call it a "duplicate definition" error. There's no formally defined name for it. Here's the relevant text from the C language definition:
6.9 External definitionsC 2011 Online Draft
...
Constraints
...
3 There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. Moreover, if an identifier declared with internal linkage is used in an expression (other than as a part of the operand of asizeof
or_Alignof
operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.
...
Semantics
...
5 An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object. If an identifier declared with external linkage is used in an expression (other than as part of the operand of asizeof
or_Alignof
operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.161)
161) Thus, if an identifier declared with external linkage is not used in an expression, there need be no external definition for it.

- 119,563
- 19
- 122
- 198
The rule of thumb - do not place any code (ie function definitions) or variable definitions in the header files.
There are some possible exception (for example static inline functions).
Header files: Function dclariations, type declaration and extern object definitions.
Source files: Function bodies and data definitions.

- 60,014
- 4
- 34
- 74
-
Yes i'm totally aware of that, i'm asking it only because they might ask us the consequences of doing this sort of things, so if something is not recommended or not acceptable but will still work, i need to know it – Itamar Ivri Jun 23 '20 at 14:32
-
1try yourself.. include the same header file with function and data definitions into the two c files. link them – 0___________ Jun 23 '20 at 15:26