-2
  1. 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?
  2. 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
Itamar Ivri
  • 117
  • 6

3 Answers3

1
  1. I think you should first gather some information about the subject of linkage.
  2. Then you should do some research on header file descriptions.
  3. Finally, you should learn the undefined behavior.
  4. Linkage
  5. Linkage 2
  6. Header File Create
  7. Undefined Behavior / UB
bekici
  • 68
  • 10
1

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 #included 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 definitions
...
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 a sizeof 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 a sizeof 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.
C 2011 Online Draft
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

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.

0___________
  • 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
  • 1
    try 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