1

I am working on a C project and I like to keep myself organised. Thus, I have got multiple header files, files with functions for that header files and a main file.

|- Header Files
|   |- hash_table.h
|   |- linked_list.h
|   |- dictionary.h
|- Source Files
|   |- main.c
|   |- hash_functions.c
|   |- list_functions.c
|   |- dictionary_functions.c

Will it be any problem if I include libraries such as #include <stdio.h> in each of that function file? Will it affect by any means the efficiency of my program?

Dragos Rizescu
  • 3,380
  • 5
  • 31
  • 42
  • 1
    Only your compilation time, not your program's execution time. See also http://stackoverflow.com/questions/2679373/include-headers-in-header-file . –  Apr 07 '14 at 12:18

5 Answers5

1

All modern headers use header guards. This means that the header checks if it has been included before and then skips it. Also the compiler is smart enough to figure out which functions defined in a header you actually use and only include those in object code.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
1

No, the include file is a description of the library. The library itself is a separate binary file that is linked in the final stages of program assembly

hdante
  • 7,685
  • 3
  • 31
  • 36
1

No problems. Standard header files are written that way. See: http://en.wikipedia.org/wiki/Include_guard

1

No, there would not be any problem if you include same header file in multiple files.

When any header file is written it is written to avoid multiple inclusion.

To avoid multiple inclusion, macro pre-processors are often used. One such mostly used way is given below.

#ifndef SOME_SYMBOL
#define SOME_SYMBOL

// actual code goes here.

#endif

For example see the code of stdio.h file for Linux at : https://github.com/torvalds/linux/blob/master/arch/powerpc/boot/stdio.h

Dhananjay
  • 340
  • 1
  • 2
  • 5
  • As far as efficacy is concerned, the macro pre-processing will take longer time (not significant though) as it'll need to perform those checks everytime header is included. It does not have any impact on run time efficacy. – Dhananjay Apr 07 '14 at 12:32
1

Mutliple file Inclusion will not effect you untill you have a INCLUSION GAURD.So by thi sYou can also do one easy thing, That is by having Single header file (suppose includes.h) which will have all include files So that you can eliminate the Order of Inclusion problem with the cost of compilation time.

Muthe
  • 23
  • 3