Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to write #include< stdio.h> in every function in a separate file? C

Tags:

c

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!

like image 959
suifuto Avatar asked Sep 15 '25 19:09

suifuto


1 Answers

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).

like image 185
Basile Starynkevitch Avatar answered Sep 18 '25 10:09

Basile Starynkevitch