2

I have been seeing code like this usually in the start of source files in C

#ifndef _INCLUDE_GUARDS_C
#define _INCLUDE_GUARDS_C

main()
{

}

function1()
{
}

#endif

function2()
{
}

I am confused about the purpose of this ..?

I am aware if the include guards define in header files, but

  1. what is the purpose of these include guards in source files ? and

  2. why function2() is defined outside the include guards ?

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
user2798118
  • 385
  • 1
  • 2
  • 16
  • http://en.wikipedia.org/wiki/Include_guard – P.P Nov 21 '13 at 11:32
  • 1
    Asking the purpose of these guards in source files(.c) not in header files(.h) – user2798118 Nov 21 '13 at 11:34
  • There's nothing stopping you from including source files from other source files, aside from increasing the probability of your getting your P45. A multiple inclusion guard could be used in such an instance. – Bathsheba Nov 21 '13 at 11:35
  • '...been seeing code' is rather weird preambule, have to say. – raina77ow Nov 21 '13 at 11:36
  • There seems to be no purpose of the include guards in the question code... That is, it is an example of how to do things in a wrong way. So 1. there's no purpose in using them like that, and 2. because the example is not how C code should be done. – hyde Nov 21 '13 at 11:42
  • @Bathsheba The one-definition rule is stopping him from including the .c file from more than one "actual" source. So the guarded-source-as-header case applies only if you're sure you're including in one translation unit but might be including multiple times within it. – Potatoswatter Nov 21 '13 at 12:24

2 Answers2

6

There is no benefit to putting include guards in a C or C++ non-header source file.

I have implemented a preprocessor from scratch and studied include guards about as much as a person can, and that is totally pointless.

As for the function outside the guards, it looks like sloppiness to me. Or, sometimes when someone has a magic incantation, they aren't sure when it is supposed to apply, so they apply it randomly.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • I've not implemented a preprocessor nor studied guards, but I could still tell you that using guards in a source file is pointless (unless you're #include'ing that source file, in which case I have stronger things to say). – mah Nov 21 '13 at 11:45
  • @mah There doesn't seem to be a better argument here than from authority, and little else to separate any answer from a comment. – Potatoswatter Nov 21 '13 at 12:25
  • I wasn't expressing any concern about the answer you posted (or that you posted as answer rather than comment)... rather I was saying that in this case, authority doesn't feel particularly useful. To go hyperbolic in analogy, if you were telling me that dropping a watermelon from a 3 story building would cause it to splatter on impact, I wouldn't need to hear about your physics degree in order to believe you... I would take you at your word :) – mah Nov 21 '13 at 12:33
  • 1
    @mah I see your point, but this is *just* enough of a non-obvious, serious question and there are *just* enough corner cases and loopholes, which are really the original concern… – Potatoswatter Nov 21 '13 at 13:02
0

Old question, but...

I think it could be used when testing the code. When testing you need access to local functions that are not defined in the header, so you include the .c file... Yes, it's ugly. Yes, you have better options!

For the functions that are not defined in the header you don't need the include guard.

Community
  • 1
  • 1
MNV
  • 117
  • 1
  • 8