4

Please, could you tell me what does the code below do?

...code...
#include file.h
...code...

I was used to put includes a the beggining of each file. I have never seen this before and also wasn't able to find anything in the internet.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Martin Plávek
  • 335
  • 1
  • 3
  • 17
  • 2
    Please show a few lines above and under the include! Maybe it's before a function declaration i don't know (And what the code does is: it includes a file! Because you know, you can...) – Rizier123 Dec 06 '14 at 18:51
  • 2
    The preprocessor allows directives *anywhere* in the source files (as long as they are on their own lines, with the `#` in the first column), it doesn't have to be at specific places. So the `#include` directive does just what you're used to, it includes the contents of the other file into the current file. – Some programmer dude Dec 06 '14 at 18:53
  • it does the same thing as #include at the top of the file. It just does it in a different place. Essentially, it opens the included file and copys the content of that file into the space occupied by the #include statement. – Brad S. Dec 06 '14 at 18:53
  • I think it could be above a function declaration where then you need this library or so – Rizier123 Dec 06 '14 at 18:53
  • What standard is the code following, ANSI or Turbo standard?? – Maximin Dec 06 '14 at 18:55

3 Answers3

5

#include is a pre-processor directive that takes the file given as the argument and dumps its contents in the current file. Typically, this is used to include definitions of commons functions from header files, but there's no necessity to use it in that way.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

It's usage is not limited to the starting of the file, but note that the variables, macros or functions declared in this header will not be usable before the include statement even if on the same file.

1

As everyone tells you #include can be used every where (provided it is on its own logicial line). And there are cases where you want to #include several times the same file. Read first the X macro wikipage, and the C preprocessor wikipage.

And I have a concrete example in my MELT monitor (related to MELT ...).

I have a predef-monimelt.h (generated) file containing lines like
MOM_PREDEFINED_NAMED( name, id,hash) e.g.

MOM_PREDEFINED_NAMED(GET,_9dsak0qcy0v_1c5z9th7x3i,1573018885)
MOM_PREDEFINED_NAMED(HEAD,_47fatww79x6_vh8ap22c0ch,3922245622) 
MOM_PREDEFINED_NAMED(web_handler,_7sav6zery1v_24sa6jwwu6c,2339220870)
#undef MOM_PREDEFINED_NAMED

My monimelt.h file (a real header file) define external pointers and an enum, so has notably:

// declare the predefined
#define MOM_PREDEFINED_NAMED(Name,Id,H) extern momitem_t* mom_named__##Name;
#include "predef-monimelt.h"

/// declare the hash of the predefined as an enum
#define MOM_PREDEFINED_NAMED(Name,Id,H) mom_hashname__##Name = H,
enum {
#include "predef-monimelt.h"
};

My main.c file contains notably a routine :

  // if this routine is compiled, we are sure that all predefined hashes
  // are unique
  const momitem_t *
  mom_predefined_item_of_hashcode (momhash_t h) {
    switch (h) {
  #define MOM_PREDEFINED_NAMED(Nam,Id,Hash) case Hash: return mom_named__##Nam;
  #include "predef-monimelt.h"
    default:
    return NULL;
     }
  }

but my items.c includes the predef-monimelt.h file twice (to create the predefined items at initialization, and to define their variables):

 void mom_create_predefined_items (void) {
   int nbnamed = 0;
 #define MOM_PREDEFINED_NAMED(Nam,Id,H) do { \
   mom_named__##Nam = mom_make_item_of_identcstr(#Id); \
   mom_named__##Nam->i_space = momspa_predefined; \
   mom_register_item_named_cstr (mom_named__##Nam, #Nam); \
   nbnamed ++; \
 } while(0);
 #include "predef-monimelt.h"
 } // end of mom_create_predefined_items

 // declare the predefined
 #define MOM_PREDEFINED_NAMED(Nam,Id,H) momitem_t* mom_named__##Nam;
 #include "predef-monimelt.h"

FWIW, the MELT monitor is GPLv3+ licensed software

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547