C Programming Tutorial - 10 - Creating a Header File

Sharing buttons:

alright guys welcome back and in this

video I'm going to be talking to you

guys about these lines at the very top

of your program and they're technically

called preprocessor directives now you

know how I told you guys like in the

first video that the reason that we

download this piece of software is not

only so we can type code in it but also

one of the jobs of this piece of

software was basically the take code

that we can understand of course we can

read this this is the word include main

and it pretty much translates it into

ones and zeroes or in other words code

the computer can understand now this

process the technical term for it is

compiling your program so what these

lines are and why I even mentioned that

is because before your program compiles

what it does is it includes or it pretty

much runs your pre-press or processor

directives kind of a tongue twister but

basically it's going to grab the entire

file wherever we tell it to so it's

going to grab stdio.h and stdlib.h and

it's going to substitute them in place

right here now after all your

preprocessor directives are finished

completing then it's going to go ahead

and compile your program and also arm

like this one right here this stdio

stands for standard input/output and

that's what allows us to use that printf

because the output stuff on the screen

so again what this include does is

basically include another file with well

whatever we're on we want to put in it

and I'll actually show you guys how to

make your own file in just a second now

another preprocessor directive you can

use other than include is actually one

called define I'll show you guys how to

use that right now pretty much type in

the I call it hash tag because I'm

stupid Twitter now but the pound sign

hit define and what a constant is is

basically it's kind of like a variable

but its value can never change so

whenever you have a variable like tuna


can set it equal to 21 and then later on

you can set it equal to 32 well with a

constant you can't change its value set

it once in your program and it stays

that way forever so if you want to just

make a stupid constant just to show you

guys an example called my name and also

whenever you're making constants make

sure that you make them in all uppercase

letters and this is just so you can

easily distinguish a constant from

variables in your program it's a I don't

know it's just so like something all

programmers do so of course

type the name whatever you want to name

your constant and then after it it's

value so I'll just say my name is tuna


and another thing I want to point out is

that you don't need a semicolon at the

end of any preprocessor directive so now

basically what's going to happen is

anytime we have my name and our program

might as well use it and what are we

going to put something stupid like me

name be and we'll just write my name

right there so basically what this

preprocessor directive is going to do is

it's going to search your program right

here and it's going to look for any

instances of the word my name and

replace them with a string tuna mcbutter

so now if we run this it says 'my name

be tuna mcbutter and of course it did

all of that before your program started

compiling so now let me go ahead and

actually show you guys how to use these

in a useful kind of way so clear this

out and clear this out as well and I

want to show you guys how to include

your own custom made header it will

actually use one of those constants

because why the heck not so the first

thing we need to do if we want to

include another file is well create a

new file so if you go to file new empty

file and it says do you want to edit yes

and now you pretty much just give it a

name I'm going to name mine


info and you also want to put dot H it's

just a header file and it's just some

I'll explain you guys about the

different types of files later on but

whenever we're including this give it a

dot H extension it means something

special to see so hit save and then it

says ok pretty much do you want to add

it yup and another thing I want to

mention is that whenever we named it dot

H what it means to see is a header file

so it creates a new folder called

headers and it organizes pretty much all

your header files for you by default

which is pretty sweet so now we have two

files in our program this first one of

course is the one may not see the main

one we've been working on the whole time

and we also have this new Bucky's info

orcs III Bucky's info dot H now also

take note of these other header files we

include we're also H so you know in case

you didn't notice that now in this one

remember what's going to happen is any

code that's in here it's pretty much

going to be copied and substituted right

whenever we write our preprocessor

directive so we can um write something

like let's just go ahead and define a

couple constants so define my name and

uh put my real name this time no more

that to na mcbutter whatever had a heck

is name is and I'll just go ahead and

define arm age 28 so now and we actually

don't need to save these because

whenever we build and run it

automatically saves all your files for

you so pretty cool you can do it the

lazy way so now we have a little bit of

code in this Bucky's info H and let's go

ahead and include it in our main program

so copy this do it the lazy way and also

delete your entire stdlib.h code right

now and as you can see this header and

this header are brought in using the

brackets and whenever you use brackets

it says it pretty much means search the

default place where headers are but we

can use double quotes and double quotes


search in the same directory or the same

folder is our code and it's just a

little bit faster way to bring it in

don't really worry about it right now

and of course type the name of your file

so again it's going to include this one

this one and then it's pretty much going

to copy this and paste it right in here

that's like pretty much what it does

actually I should probably use that

analogy earlier on and then it runs your

program so now that we have this info

brought in let's make a cool program

with it I'll show you guys how to make a

program that calculates the age of girls

I'm allowed today so I'm like alright am

I allowed to date a girl who's like 24

if I'm 28 well I don't know what about a

girl who's 17 probably not so you know

the formula for it is this go ahead and

make a variable called int girl's age

and the formula for this is actually um

and I'm going to do some math and I know

I didn't talk to you about the

mathematical operators of C yet but you

guys will understand this you take the

age and this is my age which is 28 and

you divide it by 2 of course to divide

is just the forward slash and I'll talk

like I said talk about that later on so

now of course this is 14 for me and you

take that and add 7 and that's the age

of girls anyone is allowed state so go

ahead and think of your age right now

divided by 2 and add 7 don't they any

girls younger than that so this should

equal 21 I believe so now let's just go

ahead and add a print statement don't

need to do anything else so I'll just

put something like um s which would be

my name like Bucky Roberts can date

girls who are mmm actually girls do this

percent T or older

and of course now we need to give it

those uh pretty much like pieces of

information that asks for it and this is

what I named it H or the first one is my

name and the second one is girls age so

let's see if this works all right

so Bucky can date girls 21 or older all

right I am finally I got to go find me

some 21 year old hottie now if anyone is

listening and giving a call alright so

I'll talk you guys through this one more

time real quick just so it sticks in

your brain and you're pretty much a pro

at it what we can do is we can write our

own custom header file and any code we

put in here as our program runs it

pretty much grabs this file copies all

the crap in it and paste it right here

and then your program compiles in in our

program what we did is we took my age

divided it by 2 and add 7 to that value

and it gave us the girls age that you're

allowed to date and I got 21 so I'm

going to pause this video go find some

21 year old hottie and I'll see you