C Programming Tutorial - 10 - Creating a Header File

thenewboston
4 Aug 201409:51

Summary

TLDRThis video tutorial introduces the concept of preprocessor directives in programming, focusing on 'include' and 'define'. The presenter explains how 'include' integrates external files like 'stdio.h' and 'stdlib.h' into the code, while 'define' creates constants with unchangeable values. The tutorial demonstrates creating a custom header file and using constants to calculate the age of girls one is allowed to date based on a humorous formula. The script concludes with a practical example, enhancing understanding of these fundamental programming concepts.

Takeaways

  • πŸ“ The video discusses the concept of preprocessor directives in programming, which are lines at the top of a program that perform specific tasks before the program compiles.
  • πŸ” The '#include' directive is used to include the contents of another file, such as 'stdio.h' or 'stdlib.h', into the program, facilitating the use of standard input/output functions like 'printf'.
  • πŸ› οΈ The process of converting code into a format that a computer can understand is called 'compiling'.
  • πŸ“š The '#include' directive can be used with angle brackets for searching in default locations or double quotes for searching in the same directory as the source file.
  • πŸ†• The video introduces how to create a custom header file, which is a new file with a '.h' extension that can be included in the main program for reusability of code.
  • πŸ”‘ The '#define' directive is used to define constants, which are like variables but with values that cannot be changed once set.
  • 🎨 Constants are typically written in all uppercase letters to distinguish them from variables in the code.
  • πŸ”„ Preprocessor directives do not require a semicolon at the end, unlike regular C statements.
  • πŸ‘¨β€πŸ’» The video demonstrates how to use a custom header file and constants in a program to calculate the age of girls the programmer is allowed to date, based on a simple formula.
  • πŸ˜‰ The presenter uses humor to engage the audience, suggesting they go find someone to date after learning the concept of constants and custom header files.
  • πŸ” The video emphasizes the importance of understanding preprocessor directives as a fundamental part of programming in C.

Q & A

  • What are preprocessor directives used for in a program?

    -Preprocessor directives are used to give instructions to the preprocessor before the actual compilation of the program. They include files, define constants, and can perform other tasks before the code is translated into machine code.

  • What is the purpose of the '#include' directive?

    -The '#include' directive is used to include the contents of one file into another. It allows for the use of standard libraries like stdio.h and stdlib.h, and can also include custom header files created by the programmer.

  • What does the '<stdio.h>' header file provide?

    -'<stdio.h>' is a standard input/output header file that provides functions for input and output operations, such as printf for printing to the screen.

  • What is the difference between a variable and a constant in programming?

    -A variable is a named storage location that can hold a value which can be changed throughout the program. A constant, on the other hand, is a value that cannot be changed once it is set.

  • Why should constants be written in all uppercase letters?

    -Constants are written in all uppercase letters to distinguish them from variables. This is a convention that helps programmers quickly identify constants in the code.

  • What is the purpose of the '#define' directive?

    -'#define' is a preprocessor directive used to define constants. It replaces occurrences of the constant's name with its value throughout the program before compilation.

  • Why is it important to understand the process of compiling a program?

    -Understanding the compiling process is important because it clarifies how code is transformed from human-readable form into machine code. This knowledge helps in debugging and optimizing code performance.

  • What is the significance of creating a custom header file in a program?

    -Creating a custom header file allows programmers to organize and reuse code effectively. It promotes modularity and can include constants, macro definitions, and function prototypes.

  • How does the script demonstrate the use of a custom header file?

    -The script demonstrates by creating a new file named 'bies_info.h', defining constants within it, and then including this file in the main program using the '#include' directive.

  • What is the formula used in the script to calculate the age of girls someone is allowed to date?

    -The formula used in the script is to take the person's age, divide it by two, and then add seven to find the minimum age of girls they are allowed to date.

  • What is the practical example given in the script to utilize the custom header file and constants?

    -The practical example is a program that calculates the age of girls the programmer, named Bucky, is allowed to date based on his age using the formula provided and constants defined in the custom header file.

Outlines

00:00

πŸ“ Understanding Preprocessor Directives

The first paragraph introduces the concept of preprocessor directives in programming. It explains that these directives are lines at the top of a program that perform tasks before the program compiles. The video discusses the 'include' directive, which is used to incorporate other files into the program, such as 'stdio.h' and 'stdlib.h', and how it translates to machine code. It also touches on the '#define' directive, which allows the creation of constantsβ€”variables whose values cannot change once set. The paragraph emphasizes the importance of distinguishing constants from variables by using uppercase letters and concludes with a demonstration of creating a custom header file and using a constant within it.

05:01

πŸ”§ Creating and Utilizing Custom Header Files

The second paragraph delves into the process of creating a custom header file, named 'bies.info', and demonstrates how to include it in the main program. It explains the difference between using angle brackets and double quotes for including files, with the former searching default locations and the latter searching in the same directory. The paragraph also illustrates defining constants within the header file and using them in the main program. It provides a practical example of calculating the maximum age of girls one is allowed to date based on a formula involving the user's age. The summary includes a light-hearted conclusion about the presenter's intention to find someone who fits the calculated age criteria, emphasizing the practical application of the concepts discussed.

Mindmap

Keywords

πŸ’‘Preprocessor Directives

Preprocessor directives are special instructions that are processed before the compilation of a program. In the video, they are used to include header files and define constants, which are essential for setting up the program's environment. An example from the script is '#include', which tells the compiler to include the contents of a specified file, such as 'stdio.h' for standard input and output functions.

πŸ’‘Compiling

Compiling is the process of translating code written in a high-level language into machine code that a computer can execute. The video explains that before the actual compilation, the preprocessor directives are processed. This process is crucial as it sets up the necessary libraries and constants that the program will use.

πŸ’‘Include

The 'include' directive is used to insert the content of one file into another during the preprocessing stage. In the context of the video, '#include <stdio.h>' is used to include the standard input and output library, allowing the use of functions like 'printf' for displaying output on the screen.

πŸ’‘Standard Input Output (stdio.h)

Stdio.h is a standard library in C programming that provides functions for standard input and output operations. In the video, it is included using the 'include' directive, which allows the programmer to use functions like 'printf' for outputting text to the console.

πŸ’‘Define

The '#define' preprocessor directive is used to define constants whose values cannot change throughout the program. In the video, the presenter uses '#define' to create a constant called 'my_name' and assigns it the value 'Tuna McButter', demonstrating how constants are declared and used in the program.

πŸ’‘Constants

Constants are values that remain unchanged throughout the execution of a program. They are defined using '#define' and are typically written in uppercase to distinguish them from variables. In the video, the concept is illustrated by defining 'my_name' and 'age' as constants.

πŸ’‘Header Files

Header files, usually with a '.h' extension, contain function declarations and macro definitions that can be included in multiple source files. The video demonstrates creating a custom header file named 'bies_info.h' to store constants and include it in the main program for easy access and reuse.

πŸ’‘Custom Header File

A custom header file is a user-created file that includes specific declarations and definitions for use in a program. In the script, the presenter creates a header file named 'bies_info.h' to store personal constants like 'my_name' and 'age', which can then be included in the main program for convenience.

πŸ’‘Macros

Macros are a way to define a piece of code that can be reused throughout a program. In the video, '#define' is used to create macros for constants. The script demonstrates how macros can simplify the code by replacing instances of 'my_name' with the string 'Tuna McButter' before compilation.

πŸ’‘Compilation Process

The compilation process involves converting the source code into an executable format. The video explains that the compilation process begins after the preprocessor directives have been executed, which includes including files and defining constants that are then used in the program.

πŸ’‘Age Calculation

The script includes a humorous example of calculating the age of girls the presenter is allowed to date based on his own age. This calculation demonstrates a practical application of the concepts introduced in the video, such as using constants and performing arithmetic operations in C.

Highlights

Introduction to preprocessor directives in programming.

Explanation of the 'include' directive for incorporating standard libraries.

The process of compiling a program and the role of preprocessor directives.

Demonstration of creating a custom header file for code organization.

Use of 'stdio.h' for standard input and output operations.

Introduction to the 'Define' preprocessor directive for creating constants.

Difference between variables and constants in programming.

The convention of writing constants in uppercase letters.

How the 'Define' directive replaces occurrences of a constant with its value.

Creating a new file for custom header information.

Explanation of file extensions and their significance in programming.

Inclusion of custom header files using angle brackets and double quotes.

Difference between searching default places and the same directory for header files.

Creating a program to calculate the age of girls one is allowed to date based on a formula.

Use of mathematical operators in C programming for calculations.

Incorporate custom constants into the main program for practical use.

Final program output displaying the calculated dating age limit.

Humorous conclusion encouraging viewers to find someone within the calculated age range.

Transcripts

play00:00

all right guys welcome back and in this

play00:02

video I'm going to be talking to you

play00:03

guys about these lines at the very top

play00:06

of your program and they're technically

play00:08

called preprocessor

play00:11

directives now you know how I told you

play00:13

guys like in the first video that the

play00:15

reason that we download this piece of

play00:17

software is not only so we can type code

play00:19

in it but also one of the jobs of this

play00:22

piece of software was basically a take

play00:25

code that we can understand of course we

play00:28

can read this this is the word include

play00:30

domain and it pretty much translates it

play00:32

into ones and zeros or in other words

play00:35

code the computer can understand now

play00:38

this process the technical term for it

play00:40

is compiling your program so what these

play00:45

lines are and why I even mentioned that

play00:47

is because before your program compiles

play00:51

what it does is it

play00:53

includes or pretty much runs your

play00:55

pre-processor processor directives kind

play00:58

of a tongue twister but basically it's

play01:01

going to grab the entire file wherever

play01:03

you tell it to so it's going to grab

play01:06

stdio.h and stdlib.h and it's going to

play01:09

substitute them in place right here now

play01:14

after all your pre-processor directives

play01:16

are finished completing then it's going

play01:19

to go ahead and compile your program and

play01:22

also um like this one right here this

play01:26

stdi iio stands for standard input

play01:28

output and and that's what allows us to

play01:31

use that print F because it outputs

play01:33

stuff on the screen so again what this

play01:36

include does is basically include

play01:39

another file with well whatever um we

play01:42

want to put in it and I'll actually show

play01:44

you guys how to make your own file in

play01:46

just a second now another pre-processor

play01:49

directive you can use other than include

play01:51

is actually one called Define and I'll

play01:54

show you guys how to use that right now

play01:56

pretty much type in the I call it

play01:59

hashtag cuz I'm stupid Twitter now but

play02:02

the pound sign hit Define and what a

play02:05

constant is is it's basically it's kind

play02:07

of like a variable but its value can

play02:09

never change so whenever you have a

play02:11

variable like tuna you can set it equal

play02:14

to 21 and then later on you can set it

play02:16

equal to 32 well with a constant you

play02:20

can't change its value you set it once

play02:21

in your program and it stays that way

play02:23

forever so if you want to um I don't

play02:26

know just make a stupid constant just to

play02:29

show you guys an examp example called my

play02:30

name and also whenever you're making

play02:33

constants make sure that you make them

play02:36

in all uppercase letters and this is

play02:38

just so you can easily distinguish a

play02:41

constant from variables in your program

play02:43

it's uh I don't know it's just uh like

play02:46

something all programmers do so of

play02:48

course type the name whatever you want

play02:50

to name your constant and then after it

play02:52

it's values so I'll just say my name is

play02:55

tuna

play02:56

mcbutter and another thing I want to

play02:58

point out is that you don't need a

play03:00

semicolon at the end of any

play03:01

pre-processor

play03:03

directive so now basically what's going

play03:05

to happen is anytime we have my name in

play03:08

our program might as well use

play03:12

it and what are we going to put

play03:14

something stupid like me name B and

play03:18

we'll just

play03:22

write my name right there so basically

play03:28

what this pre-processed is going to do

play03:30

is it's going to search your program

play03:32

right here and it's going to look for

play03:34

any instances of the word my name and

play03:36

replace them with the string tuna

play03:39

mcbutter so now if we run this it says

play03:42

me name be tuna mcbutter and of course

play03:46

it did all of that before your program

play03:48

started compiling so now let me go ahead

play03:50

and actually show you guys how to use

play03:53

these in a useful kind of way so clear

play03:55

this out and clear this out as

play03:58

well and I want to show you guys how to

play04:01

include your own customade header and we

play04:05

actually use one of those constants

play04:06

because why the heck not so the first

play04:09

thing we need to do if we want to

play04:11

include another file is well create a

play04:13

new file so if you go to file new empty

play04:17

file and it says do you want to edit yes

play04:21

and now you pretty much just uh give it

play04:23

a name I'm going to name mine

play04:25

bies info and you also want to put H

play04:30

it's just a header file and it's just um

play04:32

I'll explain you guys about the

play04:34

different types of files later on but

play04:36

whenever we're including this give a a h

play04:38

extension it means something special to

play04:40

see so hit save and then it says okay

play04:43

pretty much do you want to add it yep

play04:46

and another thing I want to mention is

play04:48

that whenever we named it h what it

play04:52

means to see is a header files so it

play04:54

creates a new folder called headers and

play04:57

it organizes pretty much all your header

play04:59

files for you by default which is pretty

play05:00

sweet so now we have two files in our

play05:04

program this first one of course is the

play05:06

one main.c the main one we've been

play05:08

working on the whole time and we also

play05:10

have this new bies dotinfo or excuse me

play05:13

bues

play05:14

info. now also take note of these other

play05:17

header files we include were also H so

play05:20

you know in case you didn't notice that

play05:22

now in this one remember what's going to

play05:25

happen is any code that's in here is

play05:28

pretty much be copied and substituted

play05:32

right whenever we write our

play05:33

pre-processed directive so we can um

play05:36

write something like let's just go ahead

play05:38

and Define a couple constants so

play05:40

Define my name and I'll put my uh real

play05:43

name this time no more that tunic butter

play05:47

whatever he heck his name

play05:49

is and I'll just go ahead and Define um

play05:53

age 28 so now and we actually don't need

play05:58

to save these because whenever we build

play06:00

and run it automatically saves all your

play06:01

files for you so pretty cool you can do

play06:04

it the lazy way so now we have a little

play06:06

bit of code in this bues info. and let's

play06:10

go ahead and include it in our main

play06:12

program so copy this do it the lazy

play06:16

way and

play06:18

also delete your entire STD lib piece of

play06:21

code right now and as you can see this

play06:26

header and this header are brought in

play06:28

using the brackets and whenever you use

play06:30

brackets it says it pretty much means uh

play06:33

search the default place where headers

play06:35

are but we can use double quotes and

play06:38

double quotes means search in the same

play06:42

um directory or the same folder is our

play06:44

code and it's just a little bit faster

play06:46

way to bring it in um don't really worry

play06:49

about it right

play06:50

now and of course type the name of your

play06:53

file so again it's going to include this

play06:55

one this one and then it's pretty much

play06:57

going to copy this and paste it right in

play07:00

here that's like pretty much what it

play07:01

does actually I should probably used

play07:03

that analogy earlier on and then it runs

play07:06

your program so now that we have this

play07:08

info brought in let's make a cool

play07:11

program with it I'll show you guys how

play07:12

to make a program that calculates the

play07:15

age of girls I'm allowed to date so I'm

play07:17

like all right am I allowed to date a

play07:20

girl who's like 24 if I'm 28 well I

play07:24

don't know what about a girl who's 17

play07:26

probably not so you know the formula for

play07:28

it is this

play07:30

go ahead and make a variable called int

play07:32

girls age and the formula for this is

play07:37

actually um and I'm going to do some

play07:39

math and I know I didn't talk to you

play07:40

about the um mathematical operators of C

play07:43

yet but you guys will understand this

play07:45

you take the age and this is my age

play07:47

which is 28 and you divide it by two of

play07:51

course to divide is just uh forward

play07:53

slash and I'll talk like I said talk

play07:54

about that later on so now of course

play07:58

this is 14 for me me and you take that

play08:00

and add seven and that's the age of

play08:03

girls anyone is allowed to date so go

play08:06

ahead and think of your age right now

play08:08

divide it by two and add seven don't

play08:10

they any girls younger than that so this

play08:14

should equal 21 I believe

play08:16

so now let's just go ahead and add a

play08:18

print statement don't need to do

play08:20

anything

play08:22

else so I'll just put something like um

play08:26

s which would be my name like Bucky

play08:28

Roberts

play08:29

can date girls who

play08:33

are um actually girls do this percent or

play08:40

older and of course now we need to give

play08:43

it those uh pretty much uh like pieces

play08:46

of information it asked for and this is

play08:49

what I name it age or the first one is

play08:52

my

play08:55

name and the second one is girls age

play09:01

so let's see if this

play09:03

works all right so Bucky can date girls

play09:06

21 or older all right I am fine with

play09:10

that got to go find me some 21y old

play09:12

hottie now if anyone's listening give me

play09:14

a call all right so I'll talk you guys

play09:16

through this one more time real quick

play09:18

just so it sticks in your brain and

play09:19

you're pretty much a pro at it what we

play09:21

can do is we can write our own custom

play09:24

header file and any code we put in here

play09:28

as our program runs it pretty much grabs

play09:30

this file copies all the crap in it and

play09:32

pastes it right here and then your

play09:34

program compiles and in our program what

play09:37

we did is we took my age divided it by

play09:39

two and add seven to that value and it

play09:42

gave us the girl's age that you're

play09:44

allowed to date and I got 21 so I'm

play09:47

going to pause this video go find some

play09:48

21y old hottie and I'll see you later

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C ProgrammingPreprocessorDirectivesHeader FilesCoding TutorialInclude StatementsConstantsCustom HeadersCompile ProcessProgramming Tips