2

Why should we not go for including multiple .c files into one .c file in case we have declarations in those multiple .c files ?

For instance I have variables x,y,z declared in a.c , b.c and d.c and I include these files in one source file rd.c , so why is this a bad programming practice .

Rather if I go for including multiple header files in one .c file , it is a good practice , what is the basic difference in including multiple .c files into one source file and including multiple .h files into one .c file ?

Radha Gogia
  • 765
  • 1
  • 11
  • 22
  • See the preprocessed code... – Sourav Ghosh Mar 07 '17 at 19:39
  • 6
    All names are just conventions. The point of conventions is to reduce confusion and aid understanding. – Kerrek SB Mar 07 '17 at 19:39
  • 1
    Did you do a search? e.g. [here](http://stackoverflow.com/questions/232693/including-one-c-source-file-in-another), [here](http://stackoverflow.com/questions/31002266/why-we-should-not-include-source-files-in-c), and [google](http://google.com) – kaylum Mar 07 '17 at 19:40
  • 1
    _Principle of Least Surprise_ (a.k.a. _Principle of Least Astonishment_). That's a phrase worth Googling. You can #include any file you like, but other programmers expect files named _foobar_.h to be #included, and they expect files named _foobar_.c not to be #included. – Solomon Slow Mar 07 '17 at 19:41
  • Possible duplicate of [Why have header files and .cpp files in C++?](http://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c) – adentinger Mar 07 '17 at 19:42

2 Answers2

3

Yes, you can include an .y file into a .c file. And you can simply have only one file. That's possible and no infringement of the standard.

The reason for having two files per "module" is, that you can distinguish between public parts in the .h file and private parts in the .c file. This allows the developer to restrict publications to what is needed. All other parts of the source code is kept private. This is called information hiding and a important part of software architecture. You can google for that.

So, it is likely that you have declarations in the .c file. But this declarations are private. The shall not be read by others.

Beside this, the .c file can be compiled and deployed in compiled form. Therefor you do not have any .c file to include.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
2

.c files include function implementations, not just declarations. While that may seem innocuous, if you included them, it can be extremely difficult to find if you accidentally included an implementation twice. Including a declaration twice is fine, as long as they're the same. But including an implementation twice is a compiler error that's hard to find.

Moreover, most compilation is done with a build file that has a list of source files somewhere. It's much easier to navigate a central list of source files in one place, than in the include directives from a series of files.

All this is to say that to include .h files, rather than .c files, decreases the likelihood of errors, and makes debugging easier.

SarcasticSully
  • 442
  • 4
  • 14
  • You forgot to mention compilation speed. – Jabberwocky Mar 07 '17 at 19:54
  • @MichaelWalz I didn't know that. Is it because the copy-and-paste would take longer than the linking at the end of compilation? – SarcasticSully Mar 07 '17 at 19:56
  • This answers explains the advantages of having "modules". It does not explain, why you need an .h and a .c file per module. – Amin Negm-Awad Mar 07 '17 at 19:57
  • the biggest disadvantage in including .c files is that you can declare `main` twice. – Roy Avidan Mar 07 '17 at 20:18
  • @SarcasticSully Imagine you have 100 .c file. You make a modification of one .c file. If you include 100 .c files all 100 files need to be compiled. Otherwise one .c file must be compiled and then the 100 object files need to be linked which is faster than recompiling everything. You get even more benefits if the linker can do incremental linking. – Jabberwocky Mar 07 '17 at 21:02
  • @MichaelWalz , can you please explain your last statement : "Otherwise one .c file must be compiled and then the 100 object files need to be linked which is faster than recompiling everything. You get even more benefits if the linker can do incremental linking" , If I haven't compiled my 100 .c files , then how is the object file made for them and how can I link those object files with my .c file ? – Radha Gogia Mar 08 '17 at 09:34
  • @stackuser There are multiple steps to compilation, each of which creates an intermediate file. There's preprocessing, compiling, assembling, and linking. If you include .c files, you'd always have only source code, and have to redo all those steps. If you have object files- the intermediate after assembling- there's only one file to get to the object file, and you only have to link everything. – SarcasticSully Mar 08 '17 at 10:01