2

I have started to learn C recently. I am using Eclipse and I want to create a program where each function is in a different file. My question is the following:

This in my main.c file:

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int main(void) {
    function();
    system("pause");
    return EXIT_SUCCESS;
}

My header.h:

#ifndef HEADER_H_
#define HEADER_H_

void function (void);

#endif /* HEADER_H_ */

And my function.c:

#include <stdio.h> //Necessary??

void function (void){
    printf("Hello\n");
}

Is it necessary to write #include< stdio.h>, #include< stdlib.h>, etc. in every function? The program works without #include in function.c but I get a warning.

Thanks in advance!

suifuto
  • 23
  • 1
  • 3
  • You can not add them and just compile and see where they are missing and add them where they should be – O.Rares May 21 '17 at 12:50
  • Why do you want to have each function in a different file? My feeling is that it is a very bad habit (but you could do that if you really wanted to). – Basile Starynkevitch May 21 '17 at 14:16
  • @BasileStarynkevitch Maybe I am wrong because I am new in this. But I think that having one or just a few functions per source file the program looks tidier and also it is faster to find the part that you want to edit (less time scrolling...). Thanks a lot for your previous answer, very complete – suifuto May 21 '17 at 16:08
  • Look at existing practice (free software written in C). You'll find many files of many thousand lines containing many dozen of functions. But this is a matter of habits. You could write a program with thousand of short files of a few dozen lines each (but it will compile slower) and only one function definition per file. – Basile Starynkevitch May 21 '17 at 16:38
  • BTW, Eclipse is rumored to be not very good for writing C code. Did you consider switching to a more powerful source code editor (e.g. some recent `emacs` or `vim` with suitable extensions) ? – Basile Starynkevitch May 21 '17 at 16:40
  • @BasileStarynkevitch I will follow your advice and I will read some existing code. I think it is a good idea for learning how to organise a programme. About Eclipse, it seems to work ok for what I am doing now. As I am a beginner I don't think it is so important to use the most powerful or the most efficient code editor. But thanks a lot for your advices – suifuto May 21 '17 at 16:56

2 Answers2

4

You only include the appropriate headers for those functions that are declared in that header that you are using.

In your example, main.c - You do not need to include #include <stdio.h>. However in function.c you do need to include it (stdio.h). This is because you are using the function printf in the code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
4

Read more about the C preprocessor (and also here). You often (and conventionally, so it is more a good habit than a requirement) would add a #include <stdio.h> preprocessor include directive near the start of your translation unit (or near the start of your header file), i.e. your *.c source file. Of course you need to include <stdio.h> only in translation units using some standard I/O function (or name of a type like FILE, or variable like stdout). So you can avoid <stdio.h> in modules unrelated to I/O, such as code doing only computations.

You certainly don't need to define one function per source file. It is common (and I even recommend it) to define several functions in one source file. In particular, you could declare static some internal function and call it from another one (in the same translation unit).

You might want, with GCC, to have a single pre-compiled header (which practically has to include other ones), and you could decide to put all standard headers inclusions inside your header.h. Read about include guards.

You could want to define (not only declare) some short static inline functions in your header.h, hoping that the compiler would inline most calls to them.

Your function.c practically needs to #include <stdio.h> because it is using printf. If you miss that include you need to properly declare printf.

BTW, if you compile with GCC, don't forget to enable all warnings & debug info, using gcc -Wall -Wextra -g. And try once to get the preprocessed form using gcc -C -E function.c > function.i then look with a pager (or an editor) into the generated function.i. See also this.

Notice that Eclipse is not a compiler but an IDE, that is a glorified source code editor able to run other programs (like compilers, debuggers, etc...). Your compiler -started by Eclipse- is likely to be GCC (or Clang/LLVM).

Read also the Modern C book.

NB. Programming in C requires to define and follow many conventions, which could vary from one project to the next. But you'll better explicit them. I recommend to study the source code of some small or medium sized free software project (e.g. on github).

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