- Preprocessor sounds like a scary word.
In fact it's a very helpful one.
What C does, it enables programmers
to create or work on projects
that are very large in scope
distributed among multiple location and multiple people.
The way this happens is using the command called #include
which allows linking multiple files across systems,
locations, and to multiple individuals.
We will see shortly how this works in a C program.
#incluse "file" the second one with the double quotes,
works in a similar way, so why do we need the two?
The first one, with the brackets,
those are files that are already part
of the standard libraries which means
just like you go to a library and borrow a book
you don't already have, your program can link to a file
from the library and use it.
In fact, multiple people can link to the same file.
The "file" in double quotes, is a file that you created.
Look at it as a custom document or a custom book
that you place in your own library
for anyone else to use at will.
One file can be included by multiple projects.
And finally there is a #define command
that allows you to substitute a number or a value
that doesn't make much sense by itself,
with another one that does.
For example, buffer size,
rather than referring to the number 1024
throughout your code, we can simply rename 1024
as BUFFER_SIZE.
And from that point and on, this is how it would be known
to you throughout the code.
The compiler, at run time, will replace buffer size 1024
and the program will run as intended.
Let's see how this works in an actual program.
You notice already that to include commands
in every single example you've seen so far
but we never talked too much about it.
Notice that the first two ones, the stdio,
or standard input output,
and stdlib, which is the standard library
are two programs, or libraries, that are included
with every single default C project.
The third one is more interesting.
The third one is the one that we created for this project
in order to illustrate how the linking works.
Let's run this code and notice something.
Obviously the code works because somebody told it
to print out the words "It works".
Let's see where in the code "It works" would come from.
If you look throughout those lines,
there's nothing here that says print if "It works"
or any other printing command.
We must infer therefore that it must reside somewhere else.
In fact, if you look at line 14, #include "mylibrary.h"
that's the clue the compiler looks for
to find a resolution for commands
otherwise not explicitly programmed.
Let's look at mylibrary.h.
mylibrary.h if you look at line 11 and 12
has the finishings of two functions.
In fact, function1 is used in line 20
or called as we say,
but it's clearly part of mylibrary.h.
Still in mylibrary.h we don't see any reference
to the words"It works".
Therefore we infer they might have to come
from somewhere else.
In fact, there's a mylibrary.c.
mylibrary.c is the file that actually
has all the functions we need.
We will talk about how this works in greater detail
in further, down the road.
However, all we need to know right now
to summarize this,
if you want to extend the functionality
of your main programs beyond what it has
already built in and what C provides you,
you can simply include a library.h
which then requires that you have a pair of files
library.h and library.c, .c has the actual code,
.h has some linking code,
which we'll explain later on.