Couldn't stdio functions and variables be defined in header files without having to use .c files.
If not, what are .c files used for?
Couldn't stdio functions and variables be defined in header files without having to use .c files.
If not, what are .c files used for?
The functions defined in the header file have to be implemented. The .c
file contains the implementation, though these have already been compiled into a static or shared library that your compiler can use.
The header file should contain a minimal description of the function to save time when compiling. If it included the entire source it'd force the compiler to rebuild it each and every time you compile which is really wasteful since that source never changes.
In effect, the header file serves as a cheat sheet on how to interact with the already compiled library.
The reason the .c
files are provided is primarily for debugging, so your debugger can step through in your debug build and show you source instead of raw machine code. In rare cases you may want to look at the implementation of a particular function in order to better understand it, or in even more rare cases, identify a bug. They're not actually used to compile your program.
In your code you should only ever reference the header file version, the .h
via an #include
directive.
stdio.h
is a standard header, required to be provided by every conforming hosted C implementation. It declares, but does not define, a number of entities, mostly library functions like putchar
and scanf
.
stdio.c
, if it exists, is likely to be a C source file that defines the functions declared in stdio.h
. There is no requirement that an implementation must make it available. It might not even exist; for example the implementations of the functions declared in stdio.h
might appear in multiple *.c
files.
The declaration of putchar
is:
int putchar(int c);
and that's all the compiler needs to know when it sees a call to putchar
in your program. The code that implements putchar
is typically provided as machine code, and the linker's job is to resolve your putchar()
call so it ends up invoking that code. putchar()
might not even be written in C (though it probably is).
An executable program can be built from multiple *.c
source files. One and only one copy of the code that implements putchar
is needed for an entire program. If the implementation of putchar
were in the header file, then it would be included in each separately compiled source file, creating conflicts and, at best, wasting space. The code that implements putchar()
(and all the other functions in the library) only needs to be compiled once.
The .c
files has specific function for any aim. For example stdio.c
files has standart input-output functions to use within C program. In stdio.h
header files has function prototypes for all stdio.c
functions, all defines, all macros etc. When you #include <stdio.h
> in your main code.c
file your main code assumes there is a " int printf(const char *format, ...)
" function. Returns int
value and you can pass argument ..... etc. When you call printf()
function actually you use stdio.c
files..
There are languages where if you want to make use of something someone else has written, you say something like
import module
and that takes care of everything.
C is not one of those languages.
You could put "library" source code in a file, and then use #include
to pull it in wherever you needed it. But this wouldn't work at all, for two reasons:
If you used #include
to pull it in from two different source files, and then linked the two resulting object files together, everything in the "library" would be defined twice.
You might not want to deliver your "library" code as source; you might prefer to deliver it in compiled, object form.