Hello, and welcome.
In this lecture, I want to talk about the include statement.
And so again, from our first challenge our first exposure to
c code and c program, you should have noticed this include statement.
It's on line eight here in this example, and this is our example
challenge project basic c program.
You see it's include.
And now because we've learned about the preprocessor, we know
that it's a preprocessor directive because we have that pound symbol.
Anything that starts with the pound symbol before any other
spaces is a preprocessor command, a preprocessor directive.
And so we know line eight include special keyword with the pound
symbol is a pre-processed directive.
I now want to explain to you what exactly this is doing?
What does this mean?
Again, this is a basic concept in c that we need to talk about very early
on because every c program has it.
It's very similar if you're familiar with java with the import statement.
But we're going to describe this in detail now in some slides but you'll
see this in all of your c programs.
So the include statement is a preprocessor directive because of
that pound symbol we know that.
And we call it a statement as as opposed to a directive for no real
reason just because it's common to use that term in programming language,
but it is a directive technically.
And again, the example in our program was including this
something in these angle brackets.
So we see this include keyword and then we see this stdio.h.
And so this is an example.
This is the one that we used in our first challenge.
It is not strictly part of the executable program.
However, the program won't work without it.
We know it's a pre-processed directive.
It doesn't really do much as far as how the program works.
It's just telling the compiler before it runs, hey, we were doing
something here with this include.
And so if you don't have it there, the compiler is going
to complain and throw errors.
So you need it.
The pound symbol indicates that it's a pre-processing directive.
We talked about that, which is an instruction your compiler
to do something before compiling the source code.
There are many many pre-processing directives, and we gave some examples
in our pre-processing lecture.
We talked about constants, defined statements.
We talked about if and else and so forth different the conditional
pre-processing directives.
But this is one of the most really commonly used ones, the include.
And so again, these directives can be anywhere in your source code
but they're usually at the top.
In the include specifically, this include preprocessor directive is
very similar to the import statement in java if you're familiar with that.
But what does that mean?
So in the above example here with the include pound symbol stdio.h,
this is telling your compiler to include some of your program
contents of the file with that name.
So there's a file name that exists somewhere called stdio.h.
And what the directive is saying is my current program that contains the
directive needs some of the contents inside this file in order to compile.
So specifically the include directive is going to have
file names that end in .h.
These are called header files.
They're called header files because they're usually included
at the top of the source file.
It's one of the first things you do is you include all your header files.
And again, this preprocessor directive is saying I want some
of the contents inside that file.
It's not a c file.
We know that files that end in a .c are c source files.
So the .h has to be something else, and it is.
It's a header file.
And it has that .h extension.
So let's talk about what a header file is.
Header files define information about some of the functions
that are provided by that file.
And I'll give you an example some of the content in a header file.
But first off, they're defining some information about some source code.
And specifically for our example with that included stdio.h, this is
including a c standard library header.
So remember, I told you about libraries.
Libraries are just additional functionality that's already
written for you and c that you can include in your program so
you don't have to rewrite it.
So again, examples of libraries are reading and writing to the console,
displaying output, reading and writing files, all sorts of different
things doing networking code, many many different libraries out there.
But stdio.h is a c standard library.
What that means is it comes with the c programming language and it
provides functionality for displaying output among many many other things.
So stdio.h, it's a header file.
It defines some functionality that the compiler needs to understand
before it can actually use it.
So specifically stdio.h contains information about printf.
And so we're going to talk about printf in the next lecture
which helps us display output, but printf is a function that's
in the stdio.h header file.
And it also contains many other functions.
Stdio is short for standard input output.
So this library is describing it contains functionality for
dealing with input and output.
And so this is the concept of using a meaningful name.
We don't want to call that library Jason or Jason .h
because that's not meaningful.
We use stdio.h because it's saying standard io.
Standard input and output, and we know that, that library contains all sorts
of functionality for import alpha.
Specifically functionality that we want to use in our challenge
program, our example first program is we want to use this function
called printf, which allows us display output to the console.
And so by including this stdio.h in our source code, we're saying in order
to use this functionality of this printf, we need to include this file.
So header fires are going to specify information that the compiler uses
to integrate and utilize any reuse code, any functions within a program.
So you're going to either use many many different standard library
header files and there's many out there, stdio.h is just one.
There's again header files that define functionality or function prototypes
for networking code, socket code.
Again, we're going to go through a lot of them, strings for complete
words and things like that.
But in addition to the c standard library in the different libraries
that come with the c programming language, you can also create your
own header files, and you will be creating your own header files because
header files allow for software reuse.
They allow you to reuse functionality written in c.
So somewhere the designers of c wrote all this c code that reads and writes
data and handles input and output.
They don't want this new c programmer to have to write their own.
So they're going to write it for you, and they're
going to say you can use it.
And the way that you use it is you include the header file.
And so you as your own developer, you may write some code in a big, big
large program, thousands of files that you want to be used in other files.
And so you'll write your own header files.
And the header files, again, are going to define function
prototypes, different constants, all sorts of different things
that are going to help another file that can use your program,
it will help compile your program.
So it's a software reuse technique essentially.
So we're going to include this standard io.header file in a
program whenever we need to use the printf function from the
standard library, it's required.
If you didn't include it, it will not compile.
So whenever you're using certain functionality, you
have to use the include.
The syntax as far as header files go, they are case-sensitive.
Case-sensitivity means uppercase or lowercase.
It matters whether you do one or the other.
So you should always write them in lowercase.
Your header files should be in lower case because otherwise
certain systems may interpret them have a different meaning.
There's two ways to include header files in a program.
You can use angle brackets such as pound include
Jason.h and angle brackets.
And what this usually means is it's telling the preprocessor to look for a
file in some kind of system directory.
It's usually a standard library that comes with the c programming language.
You can also use double quotes.
Double quotes usually mean it means it's a user-defined header file.
It's one that you created as a developer that didn't come with
the c programming language but you want to have that header there
because you want to reuse some code.
So it's also going to say look in the current directory.
Every c compiler that conforms to the C11 standard will
have a set of standard header files supplied with it.
These are just header files that represent libraries.
C functionality that comes with the language.
When you do use the include directive, it's common practice to use the other
preprocessor directives if not to find and define, so that you don't
include the file more than once.
This is for efficiency.
So if your program is using printf, for example, displaying output to the
screen, you want to tell your program, hey, I need to include this additional
functionality in the standard library.
You don't want to tell it to include it more than once because
the preprocessor will do many things that it doesn't need to do.
So typically you'll use if not defined.
So you'll see some of this in some of the examples that I provide where
I wrap the includes in these if not defines and define statements.
Here's an example of a header file in its contents.
You may have some header file, has some comments in it, it
could have other things in it.
It mainly has function prototypes in it, but it can have
constants, it can have typedefs.
These are some concepts that we don't understand yet.
But I'm just trying to illustrate trying to help you understand that
it contains many different things.
But again for our stdio.h, there's something in there called printf
that defines the printf function so that when we link we can find
the implementation code for that.
So here are some function prototypes.
Function prototypes are just names of functions.
We know what a function is hopefully by now, it's just a
way of grouping code together.
We'll talk about functions in much more detail later.
So a header file can include many, many different things.
It can include defined directives, constants, structure declarations.
This is again a way to group different variables, group different data.
Typedef statements have to do with data types.
We'll talk more about that.
But function prototypes are really the important one.
They're telling you how functions are defined, so the compiler can
generate the code it needs to.
Executable code goes in a source code file.
Source code file ends in the .c extension.
Executable code normally doesn't go in a header file.
There are some exceptions.
When you want to do things inline which is again an efficiency thing
when you can put source code into header files,
but typically a .h file, a header file is not going to contain implementation code.
It's going to contain lines of code for just definitions.
All right, there's not going to be any logic in there.
It's just going to be definitions.
This function exists.
That function exists.
These constants exist.
These different structures exist.
They don't say how this code is doing what it's doing.
It's just telling you that it's there.
It's needed by the compiler to generate the linking.
Okay, so you're going to create your own header files.
You're going to use system header files for additional functionality.
It's a concept that you need to understand before you move on
with writing c programs because you're going to see this include
directive in all programs.
And it's just telling the program again that we're including
an additional code somewhere.
We're including additional definition code so that we can now invoke
these different functions in this different code in our c program.
So I hope that makes sense.
Again, it's not a real complicated concept.
We're including additional code.
It's pretty intuitive, but we'll go through it we'll
provide more examples as we go.
Thank you.