C Programming Tutorial for Beginners

C Programming Tutorial for Beginners

freeCodeCamp.org

0:00 Hey, welcome to Giraffe Academy My name is Mike in this course

0:02 I'm gonna be teaching you guys everything you need to know

0:05 to get started in the C programming language C is an awesome

0:09 programming language and it's actually one of the oldest programming languages

0:12 around in fact A lot of modern programming languages are based

0:16 off of C so it's a really good idea if you want

0:18 to get into C or even if you're trying to get

0:21 into something like C++ to learn the basics and in this course,

0:24 we're gonna cover everything you need to know I'm

0:26 gonna talk to you guys about installing a text editor

0:29 and using the C compiler and we're gonna write some

0:32 basic code I'll talk to you guys about you know,

0:34 what is a program and how the program's work

0:36 and how does C Read the instructions that you

0:38 give it and we're gonna get into some more

0:40 advanced stuff We're gonna look at things like if statements

0:43 and loops we're gonna create different variables We're gonna

0:46 talk about the different types of data that you can

0:48 use and C then we're gonna get more advanced

0:50 We're gonna talk about things like structures and the functions.

0:52 We're going to talk about pointers And basically I'm just gonna give you a full

0:57 overview of all of the core concepts in C So by the end of this course,

1:02 you'll have a really good understanding

1:04 and a really good foundation Which you can build

1:06 on you can kind of go forward and learn some more I'm really excited to be

1:10 bringing you guys this basic course on C I'm really excited if you guys to dive

1:14 in and start using these tutorials So feel

1:16 free to click around through all the videos

1:18 and hopefully you'll learn something awesome about C

1:24 In this tutorial I'm gonna talk to you

1:26 guys about getting everything set up to start

1:29 programming in C now in order to program

1:31 in C We're actually going to need two things The first thing we're gonna need is

1:36 an environment where we can write our C

1:38 programs Now there's a bunch of these different environments.

1:41 Essentially.

1:41 All you need is just a text editor so anything that can you know,

1:45 allow you to Write text and then save that text in a specific

1:49 file format is gonna work although when we're working with C

1:53 a lot of times it can be useful to use a special

1:57 environment called an IDE an IDE stands for integrated Development environment.

2:02 Basically, this is a special text editor which makes it a lot easier for us

2:06 to write our C programs And so I'm gonna show you guys how to install an IDE

2:12 called code blocks the second thing We need to write our C programs is going

2:16 to be something called a C compiler now C is a programming language It means we

2:21 can basically write out You know instructions

2:23 that we want to give to the computer but eventually for the computer to be able

2:27 to execute those instructions they have to be

2:30 Compiled which basically means like translated or transformed

2:33 into a language that the computer can understand

2:36 So we're gonna basically download and install

2:38 a special program that will do that for us.

2:41 So let's get started I am going to come over to my web

2:44 browser and I'm just gonna go up to the Google search bar.

2:48 And I'm gonna search for a program called code block.

2:51 So just type in Code blocks C.

2:54 And this link should come up.

2:56 It's code blocks org now This is

2:59 an integrated development environment It's basically a text

3:02 editor that will make it really easy for us to write our C programs.

3:06 So I'm gonna come down here into downloads

3:09 and There's a bunch of options here one says download

3:12 the binary or at least download the source code

3:14 We want to click on download the binary release.

3:16 This is going to be the easiest option So if you're on Windows or Linux or Mac,

3:21 you can install it from this page So depending

3:24 on the operating system that you're on you want to click that.

3:27 I'm on Windows So I'm gonna come down here to the windows option.

3:30 You'll see there's little options for us to download

3:33 this stuff So there's a bunch of options here.

3:36 What we wanted download is going to be this one right here.

3:39 It says code blocks And then the version number

3:43 and then it says min GW- set up Basically what

3:47 this is is it's gonna allow us to install code

3:50 blocks So we're gonna be able to install that IDE program,

3:53 and we're also going to be able to install AC Compiler.

3:57 So remember I said we needed those two programs

3:59 We needed those two things and this is actually going

4:02 to give us both of them on Windows So

4:05 I'm gonna come over here and click on the link

4:07 to download this from sourceforge.net And you can see

4:10 it's gone ahead and downloaded that So now let's

4:12 head over to our downloads folder and I'm just

4:15 gonna double click on this setup program so this should

4:17 open up a window where we can set up

4:19 the program and Let's just click through this So

4:21 I'm just gonna click Next I'm gonna agree the license

4:23 and basically just leave all the options as default

4:25 and you should be good to go alright when

4:28 that's done installing it's gonna ask us to run

4:30 code blocks so we can just go ahead and do

4:32 that and It's giving me this little window here.

4:36 It says compilers auto detection So you can just click on the one up here.

4:40 That's highlighted It says GNU GCC compiler

4:42 and I'm just gonna click set as default

4:45 and click OK and there you go We now have code blocks installed so

4:51 in the next few tutorials We're gonna be learning about setting up code blocks

4:54 writing our first C programs and really just getting started with C But for now,

4:58 you have everything you need to get started

5:04 In this tutorial I'm gonna talk to you

5:07 guys about getting everything set up to start programming in C on OS X So

5:11 if you're using a Mac Then this video

5:13 will basically show you how to get everything

5:15 set up in order to start working with C We're actually going to need two things.

5:19 The first thing we're gonna need is a text

5:21 editor basically We're going to use the text editor

5:24 to write our C programs in and you can use

5:27 any text editor that you want to write C programs

5:29 but there's actually a special type of text editor which

5:32 is called an IDE and it stands for Integrated development

5:35 environment and it's basically a special environment where we can

5:38 go to Write and run and manage our C programs

5:43 So that's the first thing the second thing we're

5:45 gonna need is what's called a C compiler now C

5:48 is a programming language So essentially what we're doing when

5:51 we're using it is we're writing instructions for the computer

5:54 but in C We're writing instructions that us humans can

5:57 understand and work with but in order for the computer

6:00 to be able to carry them out they have

6:02 to be compiled or Translated or transformed into a language

6:06 that the computer can understand and that's what the C

6:09 compiler is gonna do It's gonna take our C

6:12 programs and essentially just translate them to a language

6:15 that the computer can understand So the first thing I'm gonna

6:18 do is show you guys how to install that compiler

6:20 and then we'll get a text editor So what you want to do on your Mac is go over

6:24 to the search bar and you're just gonna type in terminal

6:28 and Click enter and a window that looks like

6:30 this should pop up now The terminal is basically

6:33 a program that allows us to interact with the computer

6:37 using text commands So we're actually gonna need to use

6:39 the terminal in order to do what we want

6:41 to do so the first thing we want to do is

6:44 check to see if you might already have AC compiler

6:47 installed so in certain circumstances You might already have one.

6:50 So you just want to type in C C- V and click enter and You'll see over here.

6:57 I have AC compiler installed.

6:58 So I'm getting all of this information.

7:00 You can see over here It says like si Lang and it's giving me you know,

7:04 a bunch of information If you have that, then you have your C

7:08 compiler installed and you're ready to go But if you don't have

7:12 that I'm going to show you guys how to get it So

7:14 all you want to do is just type in Xcode select and then

7:19 the space-- install and Click enter and you'll see I'm getting an error

7:25 here because I already have these command line tools installed But if

7:29 you don't already have them installed and obviously then you wouldn't have

7:32 that C compiler Then this is gonna go off and install everything.

7:36 You need to get that C compiler working so once you run that Xcode select

7:41 command just type in CC- V again

7:44 and You should be getting this version number Alright,

7:48 so now that we have our C compiler installed What we want to do

7:51 is download an IDE So we want to download a special text editor that we

7:55 can use to write our C programs in so I'm gonna go over here

7:58 To my web browser and I'm up here at a website called code blocks dot org,

8:03 and this is an IDE called code blocks It's one

8:06 of the most popular free IDE s for programming and C.

8:09 So we're just gonna come down here to this downloads link and There

8:14 should be a few options here download

8:16 the binary released download the source code.

8:18 We're gonna click download the binary release and You'll see down here We have

8:23 a bunch of options for windows linux and mac click the mac option and if

8:27 we come down here You'll see that here is what we need to install

8:30 code blocks on the Mac So if you go over to the right side here,

8:34 there's a link to SourceForge where we can download it So I just

8:38 clicked that link and it should

8:39 start downloading Automatically when that finishes downloading,

8:42 let's head over to our downloads folder and you'll

8:44 see here we have our zip file so I'm just

8:47 gonna double click on this and They should give

8:50 us code blocks now All you want to do is

8:53 just drag and drop code blocks into your Applications

8:55 folder and you're ready to go So you have code

8:58 blocks installed and ready to start following along with this course

9:01 and writing some awesome C programs In this tutorial,

9:08 I'm gonna talk to you guys about getting

9:10 our first C file setup So we're gonna load up

9:13 a C file and we're gonna be able to get

9:15 everything working and test out our program so I'm gonna

9:18 open up my codeblocks program and this is

9:20 the IDE that I'm gonna be using for the rest

9:23 of this course So I'm just gonna double click on it

9:26 and it should start opening once we have codeblocks open.

9:28 You'll see over here There's a bunch of different options like create a new

9:31 project open an existing project So we're actually going to want to create a new

9:35 project So whenever we're gonna start writing

9:38 some C files We're gonna want to create

9:40 a new project in code blocks so you can either click this button right here,

9:44 or you can go up to file new and projects so either way it's gonna bring you

9:50 to this window over here and There's gonna be

9:53 a bunch of options here basically Just these are

9:55 different types of like C projects that we

9:58 could create what we're gonna be doing is creating

10:01 a console application This is like there's a basic

10:03 C project that you can run on your computer.

10:05 That's exactly what we need So click on console application and then click

10:08 go and I'm just gonna click through this wizard and you'll see over here.

10:12 It says C++ or C now C and C++

10:16 are both Different programming languages for this course,

10:19 we're gonna be working with C So just highlight C and click Next

10:22 and we're gonna give this a title I'm just gonna call mine draft

10:25 and we're gonna have to put this project Inside of a folder so

10:29 I'm gonna open this up and I'm just gonna put this on my desktop.

10:32 So let's click OK and Click Next and you can leave all

10:36 of these options as the default and we'll click finish so now what

10:40 we should have is our first C project in code blocks now over

10:44 here inside this little File Explorer you'll notice that Draf is right here.

10:49 So that was the name of the project that I just created down.

10:52 Here.

10:52 We have this source folder So I'm going to click down and you'll

10:56 see we have this file here called main dot C And this is

10:59 a file that got automatically created for us by code blocks I'm

11:03 just gonna right click and open it and you'll see over here.

11:06 We have some default code so up here There's

11:09 these little lines of code that say include down here.

11:11 This says int main so this is the Program

11:14 that we're given by default when we created

11:15 our C project and this is sort of like

11:17 the simplest C program that you can Right,

11:19 essentially what this program does is it prints out hello world onto

11:24 the screen So in order to run this program and test it

11:27 just to make sure that everything's working I'm gonna come up here

11:30 and you'll see that there's this Green play button over here and it

11:33 just says run when I hover over it when I click

11:36 this button This file that we have open right here main see is

11:41 gonna get run So we're essentially telling code blocks to run

11:44 this file and execute the program So when I click the Run button,

11:47 we're getting this message.

11:48 It says the project hasn't been built yet do you want to build it like yes,

11:52 and Now over here this little window pops up Basically,

11:56 this is the command prompt.

11:57 So whenever I run a C program this little window is gonna pop up So if I

12:03 was to exit out of this and run

12:04 this program again You'll see this window pops up again.

12:08 And it basically just executes the program

12:10 and in the case of this program All we're doing

12:13 is we're printing out Hello world onto the screen

12:16 and that's actually what this instruction does over here.

12:19 When we open up this command prompt.

12:21 It's essentially just Executing the program and it's

12:24 printing out whatever we told it to print out

12:26 So it's printing out hello world over here

12:28 and printing something out onto the screen is really easy

12:30 And that's one instruction that we can give

12:32 to the computer But as we go through this course

12:35 we're going to learn all sorts of instructions that we

12:37 can use but the purpose of this tutorial was

12:39 just to get our C project set up and to test our main dot C file So

12:45 as long as everything works here and you're good

12:47 to go and now we can start really learning how

12:49 to program in C In this tutorial I'm going to talk to you guys about the basics

12:57 of writing a program in C So if you've

13:00 been following along with the course up to this point,

13:02 we've installed a text editor We've installed a C

13:05 compiler and we set up our first C program.

13:08 And our first C project Inside of code blocks.

13:11 So now essentially what we have is this file over here.

13:15 This is that main dot C file I just kind of want to point

13:19 out a couple things that are in here first up here We have

13:22 these little instructions which are just called include and I'm not gonna get

13:26 too much into what those do in this tutorial Later on in the course.

13:30 We're gonna talk about what these are

13:32 actually doing and the specifics of you know, How they're helping our program,

13:35 but for now just know that in order for us

13:38 to use our program We're gonna need to have

13:40 these guys up there next thing we have is

13:43 this little block of code called main and It just has

13:46 this name main you can see there's an open

13:48 and closed parenthesis And this is actually what's called a method

13:51 and don't worry too much about what methods are we're

13:54 gonna again we're gonna talk more about those in future,

13:57 but essentially a method is just like a container where

14:00 we can put some of our code and This method main

14:04 is very important because this is the method that's going

14:07 to get executed when we run our program so in C we

14:11 can actually write out a bunch of instructions for the computer

14:14 and then we can run our program so we can

14:17 tell the computer to Execute those programs and whenever the computer

14:21 executes our C program It's gonna look inside of this main

14:25 method in other words that's gonna look inside of all

14:28 the code inside of these open and close curly brackets

14:30 and it's gonna Execute that code so I have this instruction

14:34 here It's called printf and basically this is just printing out

14:37 some text onto the screen when we run our program

14:40 It's gonna come into this main method and it's gonna execute

14:44 all the instructions inside of here namely this printf method

14:47 So if I was to run my program then this is

14:50 gonna work now Let's talk about actually running a program

14:53 whenever we want to run a program that we write

14:56 in C We actually have to do two things The first

14:58 thing we have to do is what's called building a program

15:01 or compiling a program And basically what that does is

15:05 it takes all of this C code It translates it

15:08 into a language that the computer is going to be able

15:11 to understand and execute So it's essentially taking this C program

15:15 code Translating it into computer code and then the computer

15:18 can execute it and run our program So the first thing

15:21 we have to do is always build our C file

15:23 The second thing we have to do is run the file

15:26 that gets built so once we've built this program we've

15:30 compiled it down into code the computer can understand we have

15:33 to run it basically tell the computer to execute that code

15:36 and in code blocks It's actually really easy to do this.

15:39 So we can come over here and this little gear icon will allow

15:42 us to build our program and then this play button will allow us

15:46 to run our program but a lot of times when you're Programming you're

15:50 gonna want to build and run your code at the same time, right?

15:54 you're just gonna want to build it and run

15:55 it just to see what Happened, you know?

15:57 see how your program did So we have this option

15:59 over here called build and run and this will both build

16:02 your program and run it at the same time So generally

16:05 as you're going through this course you're gonna want to be

16:07 using this option That's build and run options when I

16:09 click this it'll essentially build my program run it and then

16:13 it'll show up here on this little window in this little

16:16 window is basically a Console window you can call it.

16:19 So a lot of people would refer to this as the console and It'll

16:23 just like output any Information that we tell it to so if we

16:27 write a C program that doesn't output

16:28 information that doesn't print anything to the screen

16:30 Then we wouldn't actually see this but since we're using this printf command,

16:35 that window is gonna pop up So let's

16:37 talk about how these programs actually get executed essentially

16:40 when we write a program We have a set

16:43 of instructions So when I'm writing a C program,

16:46 I'm basically writing out a set of instructions imagine you

16:49 were writing like a recipe or something Right a recipe is

16:53 essentially just a set of instructions that when executed correctly

16:56 will result in You know cooking or baking something delicious, right?

17:01 And that's basically what these C programs are It's

17:04 a set of instructions that when executed correctly will

17:07 result in the computer doing something for us and one

17:10 of the cool things about computers is that we

17:12 can program them to do anything and C is

17:15 one of the best languages to do that with so

17:17 let's look at this instruction over here It's called

17:20 print F and this prints something out onto the screen.

17:22 We will notice over here at the end.

17:25 We have this Semicolon and this semicolon

17:28 is actually really important So whenever we write

17:30 an instruction in C We always want to end it off with a semicolon That tells C

17:35 that we're done with that instruction and we want to move on to the next one

17:38 so I could actually copy this instruction and I

17:41 could paste it down here and Now we're

17:44 basically going to be executing this instruction twice

17:47 so if I was to run and build my program you'll see that we're not only

17:51 printing it out once but we're also printing it

17:53 out twice So when we run our program

17:56 the program comes down into this main method

17:58 and it looks at all the instructions inside

18:01 of it So it starts with this first instruction.

18:03 It executes it, right?

18:05 So it prints out hello world onto the screen and then

18:07 it also prints out a new line So this backslash

18:10 n is like a newline character and that'll move the text

18:13 onto a new line When it's done with this instruction,

18:16 it moves on to the next instruction and it'll

18:18 execute that instruction So as we go forward and we

18:21 learn how to write in C We're gonna learn

18:24 more and more complex instructions And we'll be able

18:27 to use all those instructions in conjunction with each

18:30 other to specify some seriously complex programs but for now

18:33 This is really the only instruction that we know

18:35 which is printf so why don't we use this printf?

18:38 Instruction in order to draw out a little

18:41 shape onto the screen So I'm going to show

18:43 you guys how we can draw out like a little triangle onto the screen I'm gonna

18:47 take this printf and I'm actually just gonna

18:49 get rid of hello world and I'm gonna

18:51 copy this a couple times so I'm just gonna copy it and we'll paste it down here,

18:56 maybe four times and What I can do is

18:59 I can specify Instructions to the computer that will

19:02 tell it to draw out a particular shape So down here I could make like a forward

19:06 slash and over here We'll make another one

19:08 and I'll make another one and I'll make another

19:11 one and you can see here I've drawn

19:13 like a little diagonal line with these forward slashes.

19:16 Now, I'm gonna draw vertical bars going all the way down

19:19 So I'm gonna put one here I'm gonna put one here.

19:22 I'll put one here and finally, why don't we draw some underscores and then

19:28 we'll put one so I'm basically drawing

19:30 out a little triangle shape and these Backslash

19:33 ends are just printing out a new line.

19:35 So anytime I print this it's gonna move it on to a new line So it'll print

19:40 this guy out now on the line down the line below So if I was to save

19:44 my program and execute it now the computer is

19:46 going to go through it's going to execute each

19:48 one Of these instructions in order and we'll be

19:50 able to print out the shape onto the screen.

19:52 So let's do that You see over here.

19:54 We get our shape and One thing I really want to stress.

19:58 Is that order matters?

19:59 So if I took this instruction and I

20:02 placed it up here as the first instruction now

20:06 This is gonna get printed out first So when

20:08 I run my program you'll see that we're printing

20:10 out this kind of Funky looking shape and that's

20:13 because we changed up the order of the instructions

20:15 So the computer changed the order in which

20:18 it executed those instructions and you'll notice here again,

20:21 we're using this Semicolon after each line of code.

20:24 So that's kind of the basics of writing

20:26 a program That's essentially all you need to know

20:28 when we're writing a program we're specifying a list

20:30 of instructions that we want the computer to carry

20:33 out and Depending on which instructions we give

20:36 the computer and which order we give them and how

20:39 complex the instructions are That'll determine what the program

20:42 does and as we go through this course,

20:45 we're gonna learn more and more complex instructions

20:47 we're also going to learn how we can take

20:49 simple instructions like printf and Do different things

20:52 with them to make our programs a lot more powerful?

20:59 In this tutorial I want to talk to you

21:01 guys about variables in C Now when we're using

21:04 C programming language a lot of times we're gonna

21:07 be dealing with data so there's gonna be different data

21:10 values or different information that we're gonna want

21:12 to keep track of and use inside of our programs

21:15 and Sometimes it can be difficult to maintain and keep

21:19 track of all that data and that's where variables come

21:22 in a variable is Essentially a container where we

21:26 can store different pieces of information so different data values

21:30 we could sort of things like numbers or texts

21:32 or characters and These variables make it a lot easier

21:36 for us to keep track of and manage all

21:38 the data in our program So I'm gonna show you

21:41 guys basically how variables can be useful and how we

21:43 can use them in our C programs But down here.

21:46 I have a very basic program.

21:48 I'm basically just Printing out a little story.

21:50 It says there once was a man named George He was 70 years old.

21:55 He really liked the name George but did not like being 70.

21:59 So this is a simple program We're just printing out a bunch

22:02 of different lines of text and then when I run this program You'll see

22:06 we print out the actual story so over here we have our story

22:10 and this is great But let's say that I'm reading through my story.

22:14 I'm reading through my program and I'm thinking to myself hmmm I think

22:17 I want to change the character's name

22:19 So instead of calling the character George,

22:21 why don't we call him like John in order to make that change?

22:25 I'm gonna have to look through my entire program

22:27 and find every place where we mention the character's

22:29 name So right here is one change it

22:31 to John remember keep looking and here's another one.

22:34 So we'll change this to John, right?

22:36 So I had to manually go through and change every instance

22:39 of the character's name To John and let's say that okay.

22:43 That sounds pretty good But maybe I'm thinking I

22:46 want to make the character a little bit younger.

22:48 So instead of 70 Why don't we make him 35?

22:51 so now again I'm gonna have to look through my entire program find every place

22:56 where we mentioned the character's age and change

22:58 it to 35 So there and there there

23:01 you go I changed the program I updated and modified it now If I was

23:05 to run this program then all that information

23:08 will be updated and we'll have new story.

23:10 Here's the problem though I'm dealing with a very short story.

23:13 I mean, this is four lines We only mentioned the character's age

23:16 and name twice But imagine if I was writing out a huge C

23:20 program that had hundreds of lines in this story So story had

23:24 hundreds of lines and maybe we mentioned the character's name hundreds of times.

23:28 They're mentioned their age hundreds of times Well if

23:30 I wanted to change the characters age your name?

23:32 I would have to go through and manually change it in every single

23:36 location that would take forever and it would be a huge track Basically,

23:39 that's not a good way for us to manage

23:42 and maintain the data and our programs All right.

23:45 We basically have two pieces of data

23:47 that we're working with Consistently in this program

23:49 the characters age and the character's name What

23:52 I want to show you guys now is how we can use variables to better keep track

23:57 of and manage these pieces of information So

24:00 remember a variable is basically just a container

24:02 where we can store some data so I want to create two variables which will store

24:08 the character's name and The character's age and I'll

24:10 show you guys how this can make our program a lot easier to Manage up here

24:15 above these print statements I'm just going

24:18 to make a new line and I'm gonna come over here and I want to create a variable

24:22 Now whenever we create a variable in C,

24:24 we have to give C a couple pieces of information

24:28 So we have to basically tell C a couple different things

24:30 The first thing we have to tell C is what

24:33 type of information we want this variable to store now I'm

24:37 gonna get into the different data types that we can

24:39 represent and see more in the next tutorial but for now

24:42 just know that there's a bunch of different types of data

24:44 that we can store and C we can store things like

24:47 Numbers characters we can store text we can store decimal

24:50 numbers all sorts of stuff like that in our case I'm

24:54 going to store the character's name and the character's name is

24:58 basically just a collection of characters So in order to create

25:01 this variable The first thing I have to do is

25:04 tell C that I want to create a variable that's gonna

25:06 store characters So to do that, I'm just gonna type

25:09 out char just like that and that'll basically tell C Hey,

25:12 we're gonna store characters inside of this variable After we type out char I

25:17 want to type out the name of the variable that I want to create Remember

25:21 of variables that contain and it's a good

25:23 idea to give these containers Descriptive names

25:26 and those names will basically let us know

25:28 what that variable is storing inside of it.

25:31 So I'm gonna call this Character name and now what I want to do is I basically

25:37 want to store multiple characters so over here

25:41 I'm telling to see that I want to store

25:42 a character But in C we can also store instead of just storing like one single

25:47 character we can store a bunch of characters

25:49 and that would be kind of like you know

25:51 a name so there's like four characters in here

25:54 in order to store a bunch of characters

25:56 inside of this variable after we Type the character

25:59 Abel's name We're gonna have to make an open

26:02 and closed square bracket And that's gonna tell C that we want to store a bunch

26:07 of characters inside this variable So I'm gonna type

26:09 char a character name open a close square brackets.

26:12 I'm gonna set this equal to something So I'm essentially assigning

26:15 a value to this variable and I'm just gonna make an open

26:19 and closed quotation marks and inside of here We're gonna type

26:22 out the value that we want to store So in my case,

26:24 I'm just gonna store the character's name,

26:26 which is John and then as always in C We're gonna have

26:29 to end this off with a semicolon All right so essentially what I

26:33 did here was I Created a variable and I told C what

26:36 I wanted to store in the variable I wanted to store a character.

26:40 I gave this variable a name.

26:42 It was character name and I used these open and close square

26:46 brackets to sell C that I wanted to store a bunch of characters.

26:49 Not just one Now what we can do is we can

26:52 make another variable to store the character's age So in this case,

26:56 we're gonna want to do something similar,

26:57 but we're gonna want to store a number There's a bunch of different ways

27:01 that we can store numbers and see but for our purposes we're storing a name

27:04 so we're gonna use something called an integer and an integer is basically just

27:08 a whole number so I'm just gonna type

27:10 out int and That basically stands for integer.

27:12 And now we're gonna give this a name so I'm gonna call this Character

27:16 age and I'm gonna set this equal to a number So in our story

27:21 the character is 35 So I'm just gonna type out 35 and now we

27:25 can again end this off with a semicolon So I have two variables here.

27:29 This one's storing a collection of characters.

27:31 This one's storing an integer Now the question becomes

27:34 how can we use the inside of our program?

27:37 well basically What we can do is we can print them out

27:41 alongside of this text and we can actually use this print F

27:45 Instruction in order to do that so I'm gonna show you guys

27:48 how to do this and then we're gonna talk about it So basically,

27:50 I'm gonna go over here where we have the character's

27:53 name I'm gonna erase this and I'm gonna replace it

27:56 with a% and an S and now over here I'm gonna

28:00 make a comma and I'm just gonna type in the variable name.

28:04 So I'm gonna type in character name and basically what's happening

28:08 is when we use this percent s We're telling see that inside

28:13 of this string of text here We're gonna want to insert a another

28:19 String and a string is basically just a collection of characters so I

28:22 could call this up here a string variable So when I say%

28:27 s this is basically a placeholder and I'm telling C I'm gonna

28:30 insert a string into here and Over here I use this comma

28:34 and then I type out the string that I want to include so

28:38 in our case It's character name and so basically over here It's

28:42 gonna say there once was a man named% s and it's gonna replace%

28:48 s with whatever we specify over here So it's gonna replace% s

28:52 with the value that's stored inside that character name variable so I'm gonna

28:56 save this and we'll run our program and Now you'll see that it's

29:00 still printing out there once was a man named John Even though

29:04 we didn't actually type out John over here We just typed% ass

29:08 in the character name so I can do this in this other spot too.

29:12 So down here We're also using the character's name so I can

29:15 type% s and over here we can type Character name and again.

29:20 This is going to act as a placeholder For this string

29:24 of characters that we specify over here Let's do the same

29:28 thing with the age so over here we have the age

29:31 is 35 I'm going to do something similar and I'm gonna

29:33 say percent but now because I have a number instead

29:37 of using percent s I'm gonna use Percent D and that basically

29:41 means that we're gonna insert an integer number into here So

29:45 I'm saying he was percent D years old and over here.

29:48 I'm gonna make, and I'm just gonna type out

29:50 the name of the variable that I want to be placed

29:53 inside of here so I'm just gonna be Character age

29:57 and we're going to do this one more time down here.

30:00 I'm going to type percent D and Character age so essentially

30:05 what I'm doing is I'm printing out the value That's inside

30:09 of character age where I specify this placeholders so Let's go

30:13 ahead and run our program and now you'll see it's gonna look

30:16 exactly the same as it did before it's the same story

30:19 the same name and the same age Except I didn't have

30:23 to actually type out the character's age and the character's name inside

30:26 the story I just referred to the variable and what's cool about?

30:30 Variables is I could come up here and let's say

30:33 I wanted to modify the character's age all I would

30:35 have to do is modify it in this one spot

30:38 so we could change the characters a name to like Tom

30:40 and We could also change their age to like 67

30:43 And now when I run my program all of that's

30:46 gonna get updated in the story so now it's using

30:49 the name Tom and the age of 67 in our case.

30:52 We only mention the character's name and the character's

30:55 age a couple times But you can imagine if

30:57 I was mentioning this variable like ten or twenty

31:00 or thirty times Having to change it in each

31:03 one of those individual spots would be extremely

31:05 difficult So variables are great because we can store

31:07 the value one time and then if we want

31:10 to modify it We just modify it in one place.

31:12 So another cool thing that we can do with these variables is

31:15 we can actually modify them So let's say that halfway through the story.

31:19 I wanted to change the character's age.

31:21 I could actually come down here I can make

31:23 a new line and right above these print statements.

31:25 I could give this variable a new value so I could say character age and I can

31:30 just set it equal to something else so we can set it equal to like 30 and Again,

31:34 we want to include that semicolon.

31:36 So now halfway through the story.

31:37 The characters age is actually gonna change So you'll

31:40 see it says there once was a man named Tom,

31:43 he was 67 and then down here The age is being a 30 so we actually

31:47 modified the value that was stored inside

31:50 of the variable Halfway through printing out our story

31:53 and that's really why variables are useful So

31:56 that's kind of a basic overview and introduction

31:58 into what variables are why they're useful how

32:00 we can use them And over here again, we're storing a collection of characters,

32:05 which is called a string and we're also storing this integer But there's

32:09 some other data types that we can also represent So in addition to representing

32:12 like a string or a number we can do some other stuff as well

32:16 And I'm going to talk about that in the next video But for now,

32:19 this has just been a basic overview and introduction into variables

32:23 and see In this tutorial I want to talk to you guys about

32:30 data types in C and in the C programming language We're gonna

32:33 be dealing with a lot of data So generally when you're writing programs,

32:36 you're gonna be dealing with all sorts of information

32:39 and in this tutorial I just want to talk to you guys

32:42 about what types of information we can represent and use

32:45 Inside of our C program and these are called data types.

32:48 All right So what are the different types

32:50 of data that we can work with in our program?

32:53 So I'm gonna show you guys all those different data types right

32:56 now So a lot of times in C when we're working with data,

32:59 we're gonna be storing that data inside of variables so

33:03 I'm just gonna show you guys a bunch of different

33:05 variables that we can create and all the different types

33:07 of data that we Can store inside of those variables?

33:10 so remember whenever we create a variable and C

33:13 we have to tell C a couple pieces of information

33:15 and the first piece of information we need to tell C

33:18 is what type of Data that we want to store inside

33:22 of that container inside of that variable so let's talk

33:25 about the different types of data One of the most basic

33:28 types of data and probably one of the types of data

33:30 you're going to be using the most Gonna be numbers.

33:33 There's actually two very important types of numbers

33:36 that we can represent in C So

33:39 the first type is called an integer and an integer is basically a whole number

33:43 so it's a counting number right if I was counting on my fingers 1 2

33:46 3 4 5 I'm just counting out whole numbers and I'm not saying like 1

33:50 1 and a half to 2.3 like I'm counting in whole numbers and that's what

33:54 an integer is So to create an integer we could just say int And we'll

33:58 give this a name so we could say like age and we could set age

34:02 equal to like 40 or something Now whenever we want to use an integer in C.

34:07 We always just want to type out the number So you'll notice I'm not putting

34:11 quotation marks or parentheses or anything around

34:13 this I'm just typing out the number like that.

34:16 And now we actually have our variable age another type of number

34:21 we can create is a Decimal number so this would be

34:25 a number unlike an integer So it would be like 2.5

34:28 or eight point six seven or seven point five three eight, right?

34:31 It's a number that has a decimal point associated to it.

34:34 So there's actually two types of decimal number in C

34:38 there's something called a double and there's something called a Float,

34:42 and I'm not gonna get too much into the difference between

34:44 the two of them But as a beginner to see for the most

34:48 part You're just gonna want to use a double So I'm

34:50 just gonna type out double and this could be like someone's GPA.

34:53 For example, it might be like a 3.7 or whatever so, you know,

34:59 you can use the double to represent decimal numbers it's also important

35:03 to note that I could also represent like the number three so I

35:06 could just say like 3.0 and that's gonna be a decimal or I

35:10 could say 40 point O So the difference between 40 and 40

35:14 point O is that forty point O is a double It's

35:17 a decimal number and 40 without the decimal point is an integer So

35:21 that's something just to keep in mind so you can make this like

35:24 three point six So those are the two basic types of numbers

35:27 integers and doubles and those are gonna come in handy all the time

35:30 Now the next type of data that we're gonna want to store

35:33 or is gonna be characters So in C we can store single

35:37 characters inside of a variable so I could just say CH AR

35:41 and that stands for character and we could give this a value

35:44 So maybe it'd be like someone's grade and when we create a character

35:47 in C We're basically going to specify the character that we want

35:51 to store inside of single quotation marks

35:53 So inside of these single quotation marks,

35:55 I could put a letter I could put like a a lowercase a we could put h C d

36:00 AF basically you can put any character that you want

36:05 to store So if I put an A here now,

36:08 this character variable grade is gonna store the single character a inside

36:12 of it It's important to note that you can only store one character so

36:16 I couldn't put like a whole bunch of characters over here And that's

36:19 actually gonna throw an error All we can do is store one single character,

36:24 but these three are the basic data types in C So these are the basic types

36:30 of information that we can deal with in our C

36:33 programs and just using these Three data types,

36:36 we can represent all sorts of information I also want to talk to you guys

36:40 about one sort of extra data type and it's not Technically a data type in C.

36:46 It's technically like a modification of the character data type So

36:50 you'll notice when we create a character we can only store one

36:54 letter one single character inside of it But a lot of times

36:57 we're gonna want to represent more than one Character so we're

37:00 gonna want to represent like a string of characters and generally

37:04 this is called a string so it's a collection of characters that are

37:08 grouped together and we can use this char datatype in order

37:12 to create a String so I can basically say char I can

37:16 type out the name of the string that I want to create

37:18 so I could say like phrase and then after this I

37:21 can make an open and closed square brackets and this is basically

37:25 gonna tell see that we want to string a bunch of characters

37:29 together and What we're actually doing is creating something called an array

37:32 and I'm gonna get into arrays later in the course But just

37:36 for now know that we can create a string of characters

37:39 by using char Specifying the name these open and close square brackets,

37:45 and then we can create this using to open and closed

37:48 quotation marks So instead of these single quotes up here

37:51 We're just gonna use double quotes and I could type

37:53 in like a phrase so I could type in whatever String

37:56 of text I want here and this will get stored

37:59 inside of this Variable which like I said is technically called

38:02 an array now It's important to note that Doing something like

38:05 this or creating this like string of characters is a little bit

38:09 different from these guys up here So like these variables

38:12 up here we could modify them We can give them new

38:15 values but you can't do stuff like that with this So

38:17 just know that this is like a special type of data

38:20 and it's gonna act a little bit differently than these ones

38:22 up here But that's one of the basics of working

38:25 with data and those are all the basic datatypes that we're

38:28 going to be using in our C programs In this tutorial

38:35 I want to talk to you guys some more about

38:38 the print F function so if you've been following along

38:40 with this tutorial We've been using an instruction and our C

38:43 programs called Printf and printf basically allows us to print different things

38:48 out onto the screen in this tutorial We're going to talk

38:52 more about what printf does and we're gonna look at how

38:54 we can use it to print out things like variables

38:56 And also things like different data like numbers and strings and all

39:00 that stuff So in order to use printf we just

39:03 type out PR int F and we're gonna make an open

39:07 and closed parenthesis and a semicolon Now printf is what's

39:12 called a function and basically what that means is it performs

39:15 a specific task or a specific function in our case

39:19 printf just print something out onto the screen write the task

39:22 that it's doing is Printing something out and it basically works

39:25 how you'd expect it to work So if I came in here?

39:28 I could print out a specific piece of information and generally when

39:32 we're using printf We're gonna be printing out Text so I can make

39:35 an open and closed quotation marks and I could print out like

39:38 hello world in here And now this will print out onto the screen.

39:42 Hello and Inside of printf I can use all sorts

39:46 of special characters so if I wanted to create a new line,

39:48 for example, I can make a backslash N and Down here.

39:51 I could type world and now this will print out a new line So you'll

39:56 see hello world on different lines We can

39:59 also use these special characters So for example,

40:02 I could print out like a quotation mark So

40:04 if I just printed a normal quotation mark right here,

40:07 you'll notice that it's messing this up so

40:09 it's sort of like counting this as the entire

40:11 string if I wanted to change that I can just put a backslash here

40:15 and now this will literally render a Quotation marks when I run my program you

40:20 can see we have a quotation mark there so that's kind of like the basic usage

40:25 right you can specify some text that you want to print out onto the screen

40:29 and it'll do it but this printf function

40:31 is actually Extremely powerful and we can use

40:33 it to print out different pieces of information

40:36 So in addition to just printing out plain text inside of these quotation marks I

40:41 could also print out other types of information.

40:43 For example, I could print out And if I wanted to print out a number I

40:49 would have to use something called a format

40:51 specifier a format specifier Basically tells this printf

40:54 function that we want to print out a type of data That's not just plain text

40:59 like this So if I said% and then I said D This would actually allow me

41:05 to print out a number So generally when

41:08 we use these format specifiers uses% and that tells

41:11 see that you're going to want to print

41:13 out something Special and then after that we

41:16 would type in a specific letter and that'll

41:18 tell see what exactly we want to print

41:20 out so if I said% D this means that we want to print out an integer,

41:24 so All I have to do now is I can make a comma

41:28 and over here I can specify the integer that I want to print

41:32 out so I could say like 500 and now this is gonna print

41:36 out 500 to the screen so you can see over here we're getting 500

41:39 and this is cool because we can interweave these different types of data

41:44 with text so I could say like my favorite number is% D and this acts

41:51 as a placeholder for this number over here so when I run

41:55 my program now it says my favorite number is 500 and that's really cool.

42:01 We can also use multiple format specifiers so in addition to just using

42:06 this percent d I can also specify another one so over here I could

42:10 say percent ass and You'll notice

42:12 that this is structured similarly to percent d%

42:16 s is basically going to allow us to include some text So I can

42:20 include some more text just like this so if I said% s I

42:24 can make another comma here and I could say Number and now this is

42:29 gonna print out my favorite percent s so it's gonna look for this string

42:34 of text over here It's gonna insert that in and it's gonna say 500.

42:38 So now when we run this it'll say my favorite number

42:41 is 500 and you'll notice here that I'm using multiple commas.

42:45 So I'm saying like% asks% d and over here I have a comma and I'm specifying

42:51 this string that I want to print so

42:53 the string of characters and then the integer,

42:56 and so basically the order that you

42:59 put these format specifiers inside of your String

43:02 of text is the order that you need

43:04 to include them using these commas So you're going

43:07 to separate each of them using a comma hoon addition to% s in% D We can

43:11 also use a bunch of other ones And I'm going to talk to you guys about some

43:15 of the most common% D will print out an integer like 500 but if we wanted

43:19 to print out a decimal number for example

43:20 a double we could say percent F and this is

43:23 gonna allow us to print out a Decimal number so I could say like 500 point

43:27 nine eight seven five four And now this is

43:30 gonna print this out on to the screen.

43:32 Actually.

43:33 I need to build this So my favorite number is five hundred

43:37 point nine eight seven five four zero So essentially you can use

43:41 these print FS and you can use these format specifiers in order

43:46 to include different data types into your output So like this is

43:50 just some general text output and we can use these different guys

43:53 to interweave Like numbers or you know other strings and stuff like

43:57 that and what are these really become useful is when we have

44:00 variables So if I was to create like a number variable up here,

44:04 I can call it like fav num set it equal to 90 I can use this variable

44:09 and print it out inside of this printf so over here we could make this percent

44:14 D and Instead of printing out this number

44:17 we can just access that variable so I could say fav num And this will allow me

44:21 to print out that variable using that printf function.

44:24 So my favorite number is 90 so that's the basics

44:28 of using printf This is a very awesome tool that we

44:31 can use and printf is basically just used for us

44:33 to find out information So when we're running our programs

44:36 a lot of times we're gonna want to be able

44:38 to get some information About what's going on about what

44:41 they're doing in printf can allow us to do

44:43 that So don't be afraid to use these different access modifiers.

44:46 Like I said% s% d% F Those are all sort of the common ones and you

44:51 can also use% C and this will allow you to print out a single Character so

44:56 I can come over here create like a character

44:59 variable We'll call it my char and just

45:02 set it equal to like lower case I and I could print this out using% C.

45:07 So now we could say my char and you'll

45:10 see that we print so Play around with printf.

45:15 It's extremely useful and it's really going to come

45:18 in handy when we're writing more complex programs

45:25 In this tutorial I want to talk to you

45:27 guys about working with numbers and see now a lot

45:30 of times when we're writing programs in C We're

45:32 gonna want to do different things with numbers whether that's

45:35 just storing numbers and keeping track of them or adding

45:38 multiplying Subtracting doing math with numbers or you know,

45:41 really just dealing with numbers in general So I'm just

45:44 gonna give you guys a basic overview of a lot

45:46 of the stuff we can do with numbers We're

45:48 also gonna talk about how different number types in Iraq.

45:51 So how like integers and doubles interact and all

45:53 that fun stuff So down here I am printing out a number so you can see I'm

45:58 using this printf function and inside of these quotation

46:01 marks I'm Basically saying% F And% f stands

46:04 for like a floating-point number so that could

46:06 be either a double or a float basically a decimal number so we put% F in here

46:11 that's gonna allow us to print out a decimal number just like this and You can

46:15 see I can run my program and it'll

46:17 print out 8.9 and you'll notice that it's printing

46:20 out not just 8.9 But it's also printing it out to a very precise decimal point

46:24 and that's what C is always gonna do so

46:26 it's always gonna print it out to you know, A very precise decimal.

46:29 So using these decimals I can do all sorts of stuff So

46:33 obviously I can just print out the decimal but I could also

46:36 do things like math so I could say like five point zero

46:39 plus four point five and This is gonna go ahead and be

46:43 able to do that for us when I run my program

46:45 Not only is it gonna like print this out but it's gonna

46:49 do the actual math operation and you see we get nine point

46:52 five so in addition to addition we can also use subtraction division,

46:55 which is going to be a forward slash and Multiplication

46:59 which is going to be in asterisks and you can

47:01 use all these different things to let you know do

47:03 the four basic math Operations addition subtraction multiplication and division.

47:06 So now I want to talk about how we can

47:09 use floating-point numbers so like doubles and floats Alongside integers.

47:13 So let's say I was going to add an integer With a double.

47:18 So if I said 5 plus 4.5.

47:21 Well, actually when we do this this entire answer is going to get converted

47:26 into a Decimal number so when I say 5 which is an integer plus 4.5.

47:31 We're going to get a floating-point number back so we're going to get

47:35 9.5 and basically any operation that you

47:39 do between an integer and a floating-point

47:41 number is going to return a floating-point number back So that's important

47:46 to realize but if I was to change this to four now This isn't

47:50 gonna work anymore because this is asking for a floating-point number and we're

47:54 giving it an integer So when I do math with two integers it's

47:57 going to give us an integer back and if I do math

48:00 with a floating point number and integer

48:01 it will give us a Floating-point number back.

48:03 It's also important to realize for example if I took

48:06 five and divided it by four This should actually be

48:09 a decimal number right and you'd expect us to get

48:12 a decimal back But if I just print it out percent D.

48:16 So I printed out the result of the integer

48:18 five divided by the integer four I'm actually

48:20 gonna get an integer back So when I run

48:23 the program you'll see we're getting a one here,

48:26 but really this like the answer is one technically

48:28 But it's one with a bunch of decimal points after it.

48:31 Right?

48:31 It's one plus a bunch of other stuff But when

48:34 we just do math with two integers we're gonna get

48:37 an integer back if I was to say five divided

48:40 by four point zero and I've printed out a Decimal now,

48:44 we're gonna get the entire answer back.

48:45 So we're gonna get one point two five So that's just a little bit about dealing

48:50 with integers and floating point numbers We could

48:53 also take these and put these into variables so,

48:55 you know in addition to just having numbers down here I could specify a variable

48:59 like You know num set it equal to six and we can go ahead and print

49:05 num out down here as well I need to make this a D So now

49:09 I will be able to print out that variable with no problem So it can

49:14 be really useful sometimes to store

49:16 these numbers inside of variables I also want

49:18 to talk to you guys about using more

49:20 complex mathematical functions now in C We have

49:24 these things called Functions which are basically

49:25 just little blocks of code that we can

49:27 call and when we call them they're gonna go off and do Certain things for us.

49:32 So they're like modify a value or give

49:34 us information about a value and these functions

49:36 can be really useful I'm gonna show you guys a couple functions that we

49:40 can use with math So I'm just going to come over here and I

49:44 can actually start using these functions and all you have to do is just type

49:47 Out the name the function that you want to use So by default C

49:51 is going to give us access to a bunch of math functions that we can

49:55 use So we really don't have to do anything all we have to do is

49:58 just type in their names and I'm gonna show you guys want it's just POWs,

50:01 so I'm just typing in P Oh w then I'm gonna type an open

50:05 and closed parenthesis and inside of these parentheses

50:07 I can actually give this two numbers

50:10 what this is gonna do is it's gonna take the first number that we

50:12 give it and it's gonna take it to the second number so if I said

50:16 like 2& 3 And you'll notice I'm separating them with a comma this is

50:20 basically gonna give us 2 raised to the third power So to give us 2

50:25 cubed so when I run my program and actually we need to print this out

50:28 as a decimal number So this is gonna give us a decimal number back.

50:31 So I need to print this out with F When I

50:35 run my program we're gonna get 8 back just like that.

50:38 So it's basically Cubing 2 I could say like 4 raised to the third power

50:43 and now we should get back 64 which we do So that can be pretty useful.

50:48 There's a couple other ones.

50:50 I'll show you another one is SQ RT So this will allow you

50:53 to take a number square root So if I said like 36 in here now,

50:57 we're gonna print out the square root of 36 Which is gonna be 6 and there's

51:01 a couple other ones So let's say that I had a decimal I could say

51:06 C EIL which stands for ceiling So if I have like thirty six point seven

51:10 or 36 point three five six This is

51:13 gonna give us the hot the next highest number.

51:16 So it'll basically round this number up and this is gonna give us 37

51:22 as you can see We can do another one which is called floor and floor.

51:26 We'll do the opposite so floor We'll just round the number down no matter what.

51:30 So if I have like 36 point six five six,

51:33 this will round it down to 36 So there's all sorts of little,

51:38 you know things we can do little functions that we can call and get

51:42 information about numbers and like I said

51:44 these Functions will either like modify a number

51:46 so it would like give us the floor or the ceiling Or sometimes they'll like

51:50 give us information about numbers So that's

51:53 sort of the basics of working with numbers,

51:55 you know you can do all sorts of math with numbers you can add

51:58 in some to multiply and you can even

52:00 Multiply add subtract integer numbers and floating-point numbers.

52:02 So play around with all these different things with numbers and if

52:06 you want to find some more of these little math functions All you

52:09 have to do is just go online and Google search for C

52:13 math functions there's like dozens of these little functions that you can use,

52:15 but I think these are some of the most widely used

52:23 In this tutorial I want to talk to you guys about

52:26 using comments and see well comment is a special block of code

52:30 and see Which actually gets ignored when we run our program,

52:33 so if you want to write a comment you can use a special

52:37 starting and ending tag and any of the Text any of the code anything

52:41 that you put inside of that tag isn't gonna get executed or really looked

52:46 at by C So we can use comments to do all sorts of things,

52:49 you know You can leave little notes or little comments inside of your program.

52:53 You can use comments to Temporarily disable certain lines of code.

52:57 They can be really useful So I'm gonna show you

53:01 guys the basics down here in my program if I

53:03 want to create a comment I can start a comment

53:05 Using a forward slash and an asterisk and you'll see when

53:09 I put this asterisks in all the code in my text

53:12 editor changed color and basically what this means is anything

53:15 that comes after this forward slash and Asterix is gonna

53:18 be considered a comment in Order to close off the comment.

53:22 I can use an another asterisk and another forward

53:25 slash You'll see the code changed back to normal

53:28 code Inside of these two asterisks is I can

53:31 basically write out anything I want and it's gonna

53:34 get considered as a comment so for example If

53:37 I you know wrote out like my program and I

53:40 ran my program this whole thing is gonna get

53:43 ignored by C So it's just gonna print out

53:46 comments or fun It's not gonna do anything else

53:48 and you know Like I said comments are really

53:50 useful so I could leave a little note up

53:52 here I could write something like to do like I have to do something and a lot

53:57 of times people we use comments to Explain certain lines

54:01 of code so I could put a comment here

54:03 and I could say like this print out text, right?

54:07 So maybe you know, this was like a really complex

54:09 line of code We could use this text in order

54:12 to explain it You can also use comments to do

54:15 something called commenting out a line of code So for example,

54:18 let's say I wanted to run my program but I didn't want to have this line

54:22 of code in there Well one thing I could do would just be to delete

54:26 the entire line of code Right so I could delete this code and now I can

54:29 run my program and it's no longer gonna

54:31 print it out It's no longer gonna do anything.

54:33 But here's the problem in order to do that.

54:35 I had to actually delete the of code so instead

54:38 of deleting it I could just bring it back and I could

54:40 comment it out so I could basically surround it with these comment

54:45 tags and Now the computer is just gonna ignore it.

54:48 So it's not gonna render it because it's been commented out So

54:52 now when I run my program it's gonna do the same thing.

54:55 It won't execute that line of code

54:57 But I didn't have to actually delete the line.

54:59 I could just comment it out So a lot

55:01 of times when you're writing programs You might want to try

55:04 to run your program without a certain line or a certain

55:06 you know series of lines of code and instead

55:08 of deleting them and then running the program you can

55:11 just comment them out and See will basically ignore them

55:14 so that can be really useful So those are the two

55:17 basic uses for comments and essentially comments are open, right?

55:20 It's just a way that we can write out information in our files.

55:24 That won't get rendered by C And so you can really use comments to do whatever

55:28 you want Now I will say one best

55:30 practice with comments is to use them sparingly Generally,

55:34 you only want to use a comment when you

55:36 absolutely have to so we can be really annoying if

55:39 you open up a C File and there's like comments

55:42 littered all around and obviously if you want to put

55:44 a bunch of comments in your code I mean

55:46 no one's stopping you but as a best practice going

55:49 forward I think generally only using comments when they're absolutely

55:53 necessary is kind of like a good practice But again, it's open, you know,

55:57 you can use comments for whatever you want In this tutorial

56:04 I want to talk to you guys about constants in C

56:07 Well constant is a special type of variable and C which

56:11 can't be modified So when I create a constant and C,

56:15 I'm basically creating a value that is Unable to be modified.

56:19 So I'm gonna show you guys basically how this works out.

56:22 Let's say I created a variable Let's say I created an integer

56:25 and I just called it num and I set it equal to 5 right,

56:28 and then I printed out that integer onto the screen so we can print this out

56:33 percent D and We're just gonna pranaam right so I can create this num variable.

56:38 I can print it out onto the screen Let's say that down here

56:42 I wanted to modify num so I could basically say num is equal

56:45 to 8 so I can modify The value that's stored inside of the num

56:49 variable And let's come down here and we'll print this out as well.

56:53 So d And we'll print out num again.

56:56 And actually I'm gonna print out a new line here So when

57:00 I go ahead and run this program and I forgot to send me:

57:03 when I go ahead and run this program you'll see we're printing

57:07 out five and then we modified the value to eight and then

57:10 we printed out eight so I was able to modify this value

57:15 down here I was Mabel to modify num But in certain

57:18 circumstances and see you're gonna want to create variables or you're gonna

57:22 want to create values that can't be modified So these would be

57:26 values that just like can't change and those are called constants So

57:29 if I wanted to make a num a constant in other words,

57:31 let's say for some reason I didn't want the num variable

57:34 to be able to be modified in my program I could come

57:37 over here and I could say Const so Co NS T

57:40 and I'm gonna say that right before I declare the type now,

57:44 you can also say Const Right after you declare the type,

57:48 but I've always preferred to just put it before So I say Const

57:53 int num 5 and now this program is actually gonna throw an error.

57:57 So when I try to run this You'll see down here.

58:02 It's getting highlighted in red.

58:04 That's because I can't modify a constant So I'm trying to modify

58:08 a num down here But I can't because I gave it

58:11 this Const keyword because it's now considered a constant So in a lot

58:15 of cases you want have variables that just can't be modified.

58:18 Can't be changed and In a situation like that you want to make them

58:22 consonants also a lot of times when we make constant variables in C We'll

58:25 give them all uppercase names and this isn't required this is just sort of like

58:30 what a lot of developers will do so they'll say like instead of n

58:33 Um like this they would say like num or maybe this is like

58:36 your favorite number so you could say fav underscore num So that would be how

58:41 you could like distinguish two words

58:43 but generally like constants will be all capital and that's just so it's kind

58:47 of obvious that they're Unchangeable that they're constants.

58:50 But again, you don't have to do that.

58:52 But that's kind of like a best practice so

58:54 this is one way that we can create a constant,

58:57 and we basically create a constant variable

58:59 that can't be changed but a constant could also

59:02 refer to Just like any number or any text that we're using in our program So,

59:07 for example, I'm gonna get rid of this if I came down here and I

59:11 just like printed out some text So let's say I just printed out like Hello.

59:16 This is actually also gonna be considered a constant

59:18 So this is just like a string of characters

59:20 a string of text That's also considered a constant

59:23 because it's just like text on the screen.

59:25 We can't change it.

59:27 We can't modify it It's just kind of there

59:29 so you would also consider this a Constant

59:31 if I was printing out a number to like let's say I came over here

59:34 and print out 77 T is also considered

59:38 a constant So it's just considered to be like

59:42 a piece of information or a piece of data

59:44 in our program That is sort of unchanging.

59:47 Right?

59:47 So 70 this isn't gonna change I mean unless I physically

59:50 came in here and changed it like This value is always

59:53 gonna be 90 no matter what I can't modify it unless

59:56 I like manually come in and modify it So that's also considered

1:00:00 a constant But I would say like for the most part

1:00:04 like where this is going to come in handy is obviously

1:00:06 doing stuff like this But creating constant variables variables that can't

1:00:10 change In this tutorial I'm gonna show you guys how to get

1:00:18 input from a user in C So a lot of times

1:00:21 in our C program So we're going to be working

1:00:23 with all different types of information and a lot of times

1:00:26 we're gonna want to get some of that Information from the user

1:00:29 so I'm going to show you guys how we can prompt

1:00:32 the user to enter in some information We can take that information

1:00:35 store it inside of variables and then we can use it

1:00:39 inside of our program So this is gonna be pretty cool.

1:00:41 So down here I will basically write out the code for this and the first thing

1:00:45 we want to do whenever we're getting input

1:00:47 from the user is We want to prompt them.

1:00:49 So I want to prompt the user, you know,

1:00:51 as far as what they should enter in So I'm just gonna say printf and over here.

1:00:55 I'm just gonna print out a prompt.

1:00:57 So why don't we Ask the user to enter in their age.

1:01:01 So well, I'll show you guys how we can get

1:01:03 a number from the user so I'm just gonna say enter

1:01:06 your age and Now that we've prompted them to enter their age

1:01:10 I need to do two things So the first thing I

1:01:13 want to do is create a variable where we can store

1:01:18 the input that the user puts into the program So I

1:01:21 want to create a variable where we can store the age

1:01:24 that the user inputs So I'm gonna create an inn over here.

1:01:27 Oh, it's called age.

1:01:28 And I'm not gonna give this a value so all

1:01:30 I'm gonna do up here is just Declare the variable.

1:01:33 I'm just gonna tell see that I want to use this variable

1:01:36 But I'm not gonna give it a value in other words.

1:01:39 I'm gonna allow the user who's inputting the age

1:01:42 to give this a value So down here I want

1:01:45 to be able to get input for from the user

1:01:47 so I can use another function which is called

1:01:49 scanf and scanf is basically going to allow

1:01:53 the user to enter in some information into our program

1:01:56 and This works similar to printf it's kind of doing

1:01:59 the opposite of printf right printf is printing something onto

1:02:02 the screen Scanf is allowing the user to input

1:02:04 something Into the program and we're gonna make an open

1:02:08 and close quotation marks and in here I basically

1:02:10 want to tell see what type of information I'm asking

1:02:14 the user to enter in So in our case

1:02:16 we're asking for an integer right age is going

1:02:18 to be an integer it's a whole number so I'm

1:02:21 gonna accept as an input and integer and Over here.

1:02:25 We want to tell C where we want to put integer So

1:02:29 I'm basically gonna tell see what variable I want to store this in.

1:02:32 So I'm gonna store this inside of my age variable and I can

1:02:36 essentially just type out the name of the variable here But in order

1:02:40 to get input from the user I'm actually gonna have to type

1:02:44 an ampersand in front of this So instead of just typing out age.

1:02:48 I'm gonna have to type ampersand age And when I say ampersand age,

1:02:52 this is what's called a pointer and we're gonna talk about pointers in a later

1:02:56 tutorial I'm gonna cover everything you need to know about pointers But for now,

1:03:00 that's a little bit beyond what we need to learn so all you need

1:03:03 to know is that when you're using scanf and you want to store information inside

1:03:07 of like An integer or a float or like a character you need to use

1:03:12 this ampersand over here So down here now that we've scanned for the users age.

1:03:17 I'm just gonna go ahead and print it out So we'll just print out like you are

1:03:22 and I'll say% D years old and over

1:03:26 here I'm just gonna print out that age variable.

1:03:28 So essentially what I'm doing is I'm prompting the user to enter their age

1:03:31 I'm storing whatever age they enter inside

1:03:34 of this age variable and then I'm printing

1:03:37 it out to them So let's go ahead and run this program and we'll see

1:03:41 how we did So I'm gonna run the program and it says enter your age.

1:03:45 So let's say someone is 50 when I click enter

1:03:47 It's gonna take that value It's can take that integer

1:03:50 that we entered 50 store it inside of that age

1:03:53 variable and print it out You'll see over here.

1:03:55 It says you are 50 years old.

1:03:57 So that's how we can get an integer from the user We

1:04:00 can also get like a double from the user so for example,

1:04:04 I could say enter your GPA and so now instead of asking

1:04:09 for an integer we're gonna be asking for a double so

1:04:11 I can come up here and say like double and We

1:04:13 could call this GPA and now I can do the same thing.

1:04:17 But instead of saying D I want to say L F and L F is basically going

1:04:21 to tell this scanf function that we're looking

1:04:24 for a Double and then obviously instead of age.

1:04:26 We're just gonna put GPA inside of here.

1:04:28 And so now we can just say like your GPA is and then we can

1:04:34 put% F because we're gonna be printing out a double and I'll just say GPA.

1:04:40 So you'll notice here in printf when we want to use

1:04:43 a double We're using percent F to print it out.

1:04:46 But when we're using scanf we're going to use

1:04:49 percent LF So that's just like a little difference

1:04:51 and now let's go ahead and run this program And we should be able to get a GPA.

1:04:55 So let's say someone's GPA is like 3.1.

1:04:57 Now.

1:04:57 It says your GPA is 3.1.

1:05:00 So that's how we can get a double.

1:05:01 I also want to show you guys how we can

1:05:03 get a character so why don't we create a character up

1:05:07 here and we'll just call it grade and We'll say enter

1:05:11 your grade and Now when we want to get a character

1:05:16 we can just say percent C and again I can

1:05:19 just come down here and say ampersand grade and then down

1:05:22 here we can just say your grade is Percent C

1:05:27 and we'll go ahead and print out the grade So now we

1:05:31 should be able to get a character from the user so

1:05:34 enter the grade let's say I got an A and it

1:05:38 says you're good as a so we can use this scanf

1:05:41 to get specific types of Input from the user we

1:05:45 can store those specific types of input inside of variables now

1:05:48 I also want to show you guys one more thing we

1:05:51 can do which is we can actually get a string

1:05:52 from the user so in addition to getting numbers and a character,

1:05:56 we could also get like a string of characters from the user and this is

1:06:00 going to be a little bit different from Doing like numbers and characters.

1:06:04 So I want to kind of show you guys how we

1:06:07 can do this really quick so over here Let's create a variable.

1:06:10 We'll just call it.

1:06:11 Let's just call it name.

1:06:12 So we'll be storing someone's name

1:06:14 and Whenever we're creating a string of characters,

1:06:17 we always need these open and close square brackets and inside of here

1:06:21 I'm actually going to specify how many characters I want to be able

1:06:26 to store inside this string of characters so I'm just gonna put 20

1:06:30 and In the past in this course when we when we've been creating strings.

1:06:34 We haven't put a number in there We've just kind of said like whatever.

1:06:38 All right, we basically just given this a value

1:06:40 straight away But in this particular situation,

1:06:42 we're not gonna be giving this variable of value right away in other

1:06:47 words Like I don't know what the users name is gonna be like,

1:06:49 I don't know that right up front They're gonna tell us what that is.

1:06:53 And so if I'm not gonna give this a value, right?

1:06:55 I do actually need to tell see how big I want

1:06:59 this variable to be in other words I need to tell see how

1:07:02 many characters I want this variable to be able to store

1:07:05 and that way si can go ahead and Allocate enough memory for this variable.

1:07:09 So I'm just gonna put 20 and 20 basically means this will be able

1:07:13 to store 20 characters And I think that's enough for a name so down here.

1:07:17 We'll just say enter your name and I can use scanf in order

1:07:22 to get input in the form of a string but instead of saying percent

1:07:26 see I'm just gonna say percent s and Over here instead of saying

1:07:30 ampersand grade I'm just gonna type out the name of the string of characters.

1:07:34 So I don't actually need this ampersand here.

1:07:37 I can just Specify the name of the string so down here it says printf your grade

1:07:43 is and actually let's just say your name is I would have say percent s and then

1:07:50 over here we can print out the name So this should work just like it did

1:07:56 in the other cases So let's go ahead and run this and it says enter your name.

1:08:00 So my name is Mike and You'll see it says your name is Mike.

1:08:05 So that works out really well,

1:08:06 but there is one problem when we're using scanf in order

1:08:11 to get a string from the user So for example,

1:08:13 if I came in here, and I said enter your name,

1:08:15 and I said my name was john Smith when I

1:08:18 click enter You'll notice that it's only saying your name is.

1:08:23 John, it's not including smith over here and Here's the problem.

1:08:27 This is because Whenever I use this scanf function and I use it with a string.

1:08:33 It's only gonna grab the characters up to the first

1:08:37 Space so once it sees this space it's gonna be like,

1:08:40 okay, we're done getting characters, right?

1:08:42 So that's kind of a problem and that's just kind of how scanf works.

1:08:46 It's not really, you know scanf fault There is a way that we can modify

1:08:50 scanf in order to be able to get

1:08:52 input with spaces but there's another function which

1:08:55 I want to show you guys which we can use to get a line of text

1:08:59 from the user and it's called F

1:09:01 gets and Afghans is another function it's similar

1:09:04 to scanf but f gets is Basically going to be more generous F gas is

1:09:09 essentially just gonna grab like a whole line of text It's not gonna be able

1:09:13 to grab it and store it inside of like an integer or a variable or a character

1:09:18 It's just gonna be able to store it inside of like a string of characters.

1:09:21 So When we're using a forgets the first argument We want to give it is the name

1:09:26 of the variable where we want to store to the line of text So in our case,

1:09:29 it's just gonna be this name variable The next thing we want

1:09:32 to do is specify how many characters we want to be able to input

1:09:35 from the user So this will essentially

1:09:37 limit the amount of characters that the user

1:09:40 can input And this is always a good idea when we're trying to get

1:09:44 a string from the user because one common problem is that the user

1:09:48 could try to Enter in like a million characters and they would overflow

1:09:52 the like the buffer in other words Like see wouldn't be able to handle

1:09:56 accepting that many characters and the program might break So over here with F

1:10:01 gaps We can specify how many characters we want to be able to accept

1:10:03 so I'm just gonna say 20 Because that's how many characters we can

1:10:07 store inside of this variable and over here I'm just gonna say stdin

1:10:11 and this stands for standard input essentially

1:10:13 what we're doing over here is we're telling

1:10:15 FCAT s— where we want to get the information from and standard input

1:10:19 is set as essentially just like that little console that we've been using so

1:10:23 Now I'm using F Gatz and this is going to do exactly what scanf

1:10:26 did so we'll be able to store the input inside of this named variable,

1:10:29 but now we'll be able to store Multiple words instead of just one single word.

1:10:35 So let's run this program and it says enter your name So now if we entered

1:10:39 in John Smith You'll see it's able to enter

1:10:42 in John Smith Now the one downside with just

1:10:46 using this F gets is you'll notice that it printed out this new line over here

1:10:51 So let me actually demonstrate this a little bit So if I was to print out like,

1:10:56 you know, just some random text over here Whenever I get input using scanf,

1:11:01 so if I entered in John Smith When I click

1:11:05 the enter key So when I'm entering this by clicking

1:11:08 the Enter key that's actually going to represent a newline

1:11:11 character and that's going to get stored Inside of the string.

1:11:15 So when I click enter you'll notice it says your name is

1:11:19 John Smith And then it prints out a newline and it prints out

1:11:23 this text And so that's just something that you need to be aware

1:11:27 of when you're using this F gets function but for the most part,

1:11:30 I would say if you're getting input from A user in the form

1:11:34 of a string you can use F gets you can also use scanf

1:11:38 and you could use multiple Percent asses and i'm actually going to show

1:11:42 you guys how we could do something like that in a future tutorial

1:11:45 But for the most part I would say whenever you're getting a string

1:11:48 from the user you just want to use F Guess so that's the basics

1:11:51 of getting input from the user like I said there's a couple other

1:11:54 things that we can do and I'm gonna talk about At least one other

1:11:58 way that we can get strings from the user in a future tutorial

1:12:01 But this is kind of the basics and you can kind of play

1:12:04 around with doing this stuff in your programs In this tutorial I'm going

1:12:11 to show you guys how to build a basic calculator in C we're basically

1:12:15 gonna build a little program where the user can enter in two numbers

1:12:18 and then Our program will take those two numbers add them together and spit

1:12:23 out the answer So this is gonna be kind of cool and we'll

1:12:26 also learn about getting numbers as input from a user So over here,

1:12:30 I want to show you guys exactly how we can do

1:12:33 this the first thing I want to do is Print out a prompt.

1:12:37 So I basically want to prompt the user for some input

1:12:40 so over here I can just say printf and Inside here.

1:12:44 We're just gonna give them a little prompt.

1:12:47 So I'll say enter first number and Essentially what

1:12:52 we're gonna have them do is enter in two numbers,

1:12:55 so I'm actually going to create two variables up here

1:12:58 so we'll make an int and we'll call it num1

1:13:00 and Then we'll make another inch and we'll call it

1:13:03 num2 and i'm not actually going to give these Values

1:13:06 right up front we're gonna end up giving these whatever

1:13:09 the user enters in So after the user enters the first

1:13:13 number we need to actually grab that number so I

1:13:16 can use another see function called scanf and In here,

1:13:20 we're basically just gonna say percent d because we want

1:13:23 to grab an integer and then over here I'm going

1:13:26 to say the name of the variable Where I want

1:13:28 to store the value that gets entered now if you're

1:13:31 following along with this course in the last tutorial We

1:13:34 were able to get a string of characters as input

1:13:37 from the user And basically we just typed in like

1:13:40 the name of the variable But when we're getting input,

1:13:43 that's not a string of characters In other words when we're

1:13:46 getting input that's not percent s so if it's like percent

1:13:49 d or percent 4 percent C so if it's a guy

1:13:52 a decimal or an integer or a float or a character,

1:13:55 we actually need to use a special symbol this ampersand So I'm gonna have to say

1:14:01 ampersand and then the name of the variable

1:14:02 what I want to store this value so I'm

1:14:05 just gonna say ampersand 1 and Basically what

1:14:08 this means is we're accessing the address of num

1:14:10 1 and we're gonna talk about addresses

1:14:12 and pointers in a future video but for now

1:14:15 just know that you need this ampersand here

1:14:17 in order to Store the value that gets

1:14:21 entered inside of this variable So once we do that and now we can move

1:14:25 on and we're basically just gonna do

1:14:26 the same exact thing but for the second number

1:14:29 so I'm gonna copy these and I'll paste

1:14:32 this and now instead of saying enter first number.

1:14:34 We'll say enter Second number and we're

1:14:37 just gonna store this inside of num2 Alright,

1:14:41 so once we're done with this, the last thing we want to do is add

1:14:44 them together and print out the answer So I'm just going to come down here.

1:14:48 I'm going to say printf and We'll just say answer

1:14:51 and over here We'll print out the answer so it's gonna be

1:14:55 an integer and we're basically just gonna print out num1 plus

1:14:58 num2 So we're gonna print out the value of num1 plus num2.

1:15:03 So we have our basic calculator.

1:15:05 We're getting the first number We're getting the second number.

1:15:08 We're storing them inside of these variables.

1:15:09 We're adding them together and printing them

1:15:11 out Let's try to run our calculator.

1:15:13 So I'm gonna run my program and you'll see over here.

1:15:16 It says enter first number So let's go ahead and enter 6

1:15:19 enter Second number will enter A8 and now we're gonna get 14.

1:15:25 So 14 is the correct answer So our program worked

1:15:28 it was able to add the numbers correctly and everything's awesome

1:15:32 But let me show you guys one problem with this program

1:15:35 if I wanted to do math on Multiple non decimal numbers,

1:15:38 for example if I said 2 and then over here I said like 6

1:15:42 point 8 when I add these together We're not going to get the correct answer.

1:15:47 We're gonna get 6 plus 2 Which is gonna be an integer 8,

1:15:51 but we're not gonna get 8 point 8,

1:15:53 so we're not getting the correct value So instead of letting the user enter

1:15:58 in only integers Why don't we instead let them enter in doubles so over here?

1:16:02 I'm going to change these to double so I'm gonna say num one's gonna be

1:16:05 a double and num2 is gonna be a double and Since we're getting doubles as input,

1:16:09 we're gonna have to come down here and modify

1:16:11 these So right now this is accepting an integer

1:16:15 but we want to make this accept a double so

1:16:17 normally if we're using Printf and we wanted to print

1:16:20 out a double we would say f and that stands

1:16:22 for like floating-point number but when we're scanning

1:16:24 for a number if we want to use a double

1:16:26 we have to say LF just like that so down here.

1:16:29 I'm also gonna say LF and we'll change this to just F so print F is

1:16:34 a little bit different than Scanf right and scanf

1:16:37 if we want to accept a double as input.

1:16:40 We need to use LF in printf We just use% F.

1:16:43 So let's run our program now and we should

1:16:46 be able to add floating-point numbers so decimal numbers

1:16:50 over here I will do four point five plus

1:16:53 six point seven and we get 11 point two,

1:16:55 so that looks good to me Looks like everything is working properly.

1:16:59 And now we have a basic calculator So

1:17:02 the user can enter in any numbers that they want

1:17:04 and the calculator will be able to handle them Now

1:17:07 this calculator is not like 100% ironclad secure for example,

1:17:11 like if I was to come up here and Enter in like a string

1:17:16 of characters instead of a number you'll

1:17:18 see that it's gonna break the program, right?

1:17:21 So it's just like not doing what we wanted to do

1:17:23 And as we go further in this course We're gonna learn all sorts

1:17:26 of ways that we can check to see if certain things are getting

1:17:30 entered correctly and we can basically

1:17:31 mitigate circumstances like that, but for now,

1:17:33 this is just kind of an introduction into how we can get

1:17:37 numbers as input and do things like Addition or multiplication once we

1:17:41 have them In this tutorial I'm going to talk to you guys

1:17:48 about building a little game in C more specifically We're gonna be

1:17:52 building a mad libs game now If you ever played the game

1:17:54 mad libs it's basically a game where you write down a bunch

1:17:57 of random words so it could be like You know nouns

1:18:00 or verbs or someone's name or you know a verb ending in ing,

1:18:03 something like that you take all of those words that you enter in and you kind

1:18:07 of Sprinkle them in into a story and then

1:18:10 generally the story is like kind of funny

1:18:11 because it has all these random words in it

1:18:14 It's actually if we head over to my web

1:18:15 browser You'll see I have a picture of a Madlib up here And basically you just

1:18:20 add in a bunch of random words into the story and then you'd read the story

1:18:23 back and it could be Kind of funny so I'm gonna show you guys how we can

1:18:27 build something like that in C And we're

1:18:30 also gonna talk about some more ways that we

1:18:32 can use that scanf function that I showed

1:18:34 you guys in the last tutorial So over here,

1:18:37 we have a little story that I printed out.

1:18:39 It just says roses are red violets are blue I

1:18:42 love you kind of like a classic poem But I think

1:18:46 this poem would be a lot funnier if we turned it

1:18:48 into a mad libs So that's exactly what we're gonna do.

1:18:52 I'm gonna replace roses are red.

1:18:54 I'm gonna replace red with a random color So

1:18:57 we're just gonna have a user enter in a color.

1:19:00 I'll replace violets with a plural noun So

1:19:02 we're gonna have them enter in a plural noun,

1:19:04 and I'm gonna have instead of saying I love you

1:19:07 We're gonna say I love and then a specific celebrity.

1:19:10 So I'll just type in celebrity right there Alright,

1:19:14 so this is basically what we're gonna be printing out we're gonna be

1:19:17 printed out roses are and then whatever color they enter in Plural nouns

1:19:22 are blue and then I love whatever celebrity so let's talk about actually

1:19:27 creating this program So we're actually going to need to do a couple things

1:19:30 And actually the first thing I want to do is I want to create

1:19:34 variables we're gonna create variables to store The color that the user inputs

1:19:39 the plural noun that the user inputs and the celebrity the user inputs we're

1:19:43 going to create three variables and these are

1:19:46 basically going to be character strings,

1:19:48 so they're going to be Collections of characters and we can

1:19:51 store them in Variable so I'm gonna create some of these variables.

1:19:55 Why don't we create one for color?

1:19:57 And we remember whenever we create a String or like a collection of characters?

1:20:02 We need to make these open and closed square brackets And what I also

1:20:06 want to do because I'm not gonna be giving color of value right away.

1:20:10 In other words.

1:20:11 We're letting the user Determine the value of color

1:20:14 I just need to tell see how many characters we

1:20:16 want this String to be able to store that way

1:20:19 si knows how much memory it needs to allocate

1:20:22 for this variable So I'm just gonna say

1:20:24 20 and we'll basically just say they can enter

1:20:27 in a color that is up to 20 characters we're

1:20:29 gonna do the same thing for plural noun and again,

1:20:33 we'll let them enter in 20 characters maximum and Finally,

1:20:38 we're gonna do the same thing for celebrity.

1:20:40 So again 20 characters sounds good Alright

1:20:43 now that we have our variables created.

1:20:45 I want to actually get information from the user so I want

1:20:48 to prompt the user for Information and I want to take the information

1:20:51 that they entered in and I want to store it inside of each

1:20:54 one of these variables First order of business is to prompt them for input

1:20:58 so I can just say print F and inside here We'll basically just

1:21:01 type in enter a color Once we've prompted them to enter the color

1:21:06 we can actually get whatever color they enter and store it inside

1:21:10 of a variable I'm going to use a function called Scanf and over here.

1:21:15 I'm gonna accept a string so I'm going

1:21:18 to accept a string of characters and we're gonna

1:21:21 store this inside of our color variable just

1:21:23 like that and remember if you watched the last

1:21:26 tutorial we use the Ampersand here when we

1:21:29 were getting numbers or also you'd do the same

1:21:32 thing if you were getting a single character

1:21:34 But when we're getting input for a string of characters,

1:21:37 we don't need that ampersand so you can just get rid of that Let's copy

1:21:42 these and I'm gonna paste this two more times so the second thing we want to get

1:21:46 from them is going to be the plural noun and I'm gonna store this inside

1:21:50 of the plural noun variable so you can

1:21:53 see now we're getting the plural noun and Finally,

1:21:56 we're gonna do the same thing for celebrity.

1:21:59 And again, we're gonna get that celebrity Okay, cool.

1:22:04 So now I'm getting the color.

1:22:06 I'm getting the plural noun and I'm getting

1:22:09 the celebrity So the last thing we have to do is we have to take all

1:22:13 of these variables and put them into our story Right,

1:22:15 so we need to be able to print out the story with all of those variables

1:22:19 So I'm gonna come down here and I'm just going to say percent s and over here.

1:22:23 We'll pass in the color Same thing here.

1:22:27 I'm gonna replace the plural noun here with a percent s and we'll

1:22:31 pass in the Plural noun and finally same thing for celebrity down here

1:22:42 Alright so everything seems to be wired up and you'll notice that I

1:22:46 have new lines here so that this story prints out on new Lines,

1:22:49 let's go ahead and run this program and we'll see how we did So over here,

1:22:54 it's prompting us for a color.

1:22:56 Why don't we enter in magenta?

1:22:58 Enter a plural noun, let's do Microwaves and enter a celebrity.

1:23:03 Why don't we just say?

1:23:06 Prince so when I click enter it's

1:23:08 gonna say roses are magenta Microwaves are blue.

1:23:11 I love prints.

1:23:12 So we were able to prompt the user to enter in all of that input We

1:23:16 took everything that they input we stored it

1:23:19 in variables Then we printed all those variables

1:23:21 out inside of our story and we have

1:23:24 our Madeleine the program seems to be working

1:23:26 really well I do want to show you guys one way that this program could mess up.

1:23:30 So let's go ahead and run this again So let's enter in a different color.

1:23:34 I'm going to enter in like blue enter in a plural noun So why don't we enter in?

1:23:38 phones And now enter in a celebrity so I'm gonna show you

1:23:42 guys one way that we could actually break this program If I entered

1:23:46 in a celebrity with a first and a last name like Tom Hanks when

1:23:50 I click enter now You'll notice that instead of saying I love Tom Hanks.

1:23:56 It's only saying I love Tom Here's the problem when we use that scanf

1:24:01 function scanf is only going to grab characters up to the first Whitespace,

1:24:06 so essentially when we put this space here.

1:24:09 We're telling see that we don't want to grab

1:24:11 anymore but in reality we want to be able

1:24:14 to grab the there's Full-name want to be able

1:24:17 to grab the celebrities first and last name if need be?

1:24:21 So this is a situation in C where we would have to modify our a little program

1:24:25 so what I could do is instead of just

1:24:27 getting one variable like the celebrity I can actually

1:24:30 get to so I could say over here like

1:24:33 Celebrity F and that'll stand for celebrity first name

1:24:36 and then down here We can make another variable

1:24:39 called celebrity l that'll stand for a last name.

1:24:42 So now when we scan instead of just

1:24:46 scanning for one string of characters I can scan

1:24:49 for two strings of characters and we'll have

1:24:51 celebrity F and then celebrity l and Down here.

1:24:56 We're gonna want to do the same thing so we

1:24:59 can just say celebrity F and Celebrity L and we

1:25:03 just need to add another percent here So it's gonna

1:25:07 say I love celebrities first name and celebrities last name.

1:25:09 So let's run our program and see how we did so I can

1:25:13 enter in like red and Microphones and now we can enter in Tom Hanks

1:25:21 and we're printing out the actors first and last name So that's one way

1:25:25 that we could remedy this program and make it be able to accept two inputs

1:25:30 with a space in the middle And it also just shows you guys a little

1:25:33 bit more about how scanf works so it's gonna stop scanning It's gonna stop

1:25:37 getting the input at that first space now Here's the thing about this program

1:25:41 though if I wanted to enter in a celebrity with Who only had one name?

1:25:47 So if I only wanted to enter in one the program actually isn't

1:25:50 going to be able to handle that so if I said like hats

1:25:52 and down here if I said Like Gandhi and I click enter you'll

1:25:56 notice that it's still waiting for me to enter in a last name Right,

1:26:01 so I could enter in something here and then the program

1:26:03 will work But it was waiting for me after I entered

1:26:07 in just that one name So that's something that you're gonna

1:26:10 have to you know play around with in your programs Basically

1:26:12 C is gonna force you to be very specific about what

1:26:15 the user is entering so if the user needs to enter

1:26:18 two things like two words and you need to specify

1:26:20 that if the user is only going to Enter in one word.

1:26:23 You have to specify that.

1:26:24 So you have to be very specific when you're getting input from the user

1:26:28 like that In this tutorial I'm gonna talk to you guys about using

1:26:35 a raisin seed a lot of times when we're writing our C programs we're

1:26:39 gonna be dealing with a bunch of different data and one of the things we

1:26:44 can do to control and manage and sort of keep track of and organize

1:26:48 that data is Put data inside of things called arrays an array is really

1:26:52 useful because it's basically a container where

1:26:54 we can store a piece of information But what happens in your programs when

1:26:58 you're dealing with huge amounts of information?

1:27:01 Specifically huge amounts of information that are related right imagine I

1:27:05 had a list of like a bunch of names or a list

1:27:08 of a bunch of numbers that I needed to keep track

1:27:10 of Let's say I had like a list of a hundred numbers, right?

1:27:13 I wouldn't want to have to create a hundred

1:27:15 different variables to store all of those different numbers

1:27:19 and this is where arrays come in an array

1:27:22 is essentially a Data structure where we can store

1:27:25 a bunch of different data values So inside

1:27:28 of an array unlike a variable where I can only store

1:27:30 one value I could store hundreds or thousands or even

1:27:33 millions of values So inside of a single array,

1:27:36 I could store like five things or seven

1:27:39 things or ten numbers or twenty characters,

1:27:41 you know I could sort a bunch of different pieces

1:27:43 of information and then all that information would be nice

1:27:46 and neat and Organized in my program so I'm gonna

1:27:49 show you guys how to create arrays We can create

1:27:52 an array a lot Like we create a normal variable

1:27:54 and a variable in array are very similar variable is basically

1:27:58 used to define a container that Stores a single value

1:28:02 and an array is used to define a container that stores,

1:28:05 you know any number of values So the first thing we're gonna have to do when

1:28:10 we create an array is give C some

1:28:12 Information and the first piece of information is what

1:28:14 type of data we want to store inside of the array so for example I could

1:28:19 say int and now I'd be creating an array

1:28:22 that would hold integers If I said char,

1:28:25 I'd be creating an array that would hold characters Or if

1:28:28 I said like double the same thing but for double numbers,

1:28:31 so why don't we create an array of integers?

1:28:32 And I'm just gonna call this lucky numbers whenever I create an array I always

1:28:38 want to use a special little symbol and this symbol will tell see that instead

1:28:42 of just creating a normal Variable we want to an array and it's an open

1:28:47 and closed Square brackets whenever I use

1:28:49 these open and close square brackets after the name.

1:28:52 It's gonna tell C like, okay We want to store multiple pieces

1:28:55 of information Now there's a bunch of different ways.

1:28:58 We can create these arrays the easiest one though is

1:29:01 to just say equals and I can make an open and closed

1:29:05 curly bracket and inside of this open and close curly

1:29:08 bracket I can just start typing out some numbers that I

1:29:10 want to put in this array so we could type out like 4 8 15 16 23 42 Now I can

1:29:19 basically type out as many numbers as I wanted and I

1:29:22 could store them Inside of this array so over here,

1:29:25 I you know, I printed out six numbers I

1:29:27 could print out 600 if I wanted You know,

1:29:29 the only thing that's limiting me is how many I can actually put in here

1:29:33 So you'll notice that I put in the piece of data so like 4,

1:29:36 for example And then I put a comma and a comma

1:29:39 is gonna separate all the elements in the array

1:29:42 So we would say 4 is the first element

1:29:44 in the array 8 is the second element in the array

1:29:47 These are all considered to be elements Inside of this array

1:29:51 and what's cool about this array is it's now

1:29:53 storing all of this information So unlike a variable where

1:29:57 we can only store one value now in this array.

1:29:59 I'm storing 6 different numbers so if I wanted I can print this out

1:30:03 and I'm actually gonna print this out just to show you guys so remember,

1:30:07 we're gonna print out a Number and I'm gonna show you

1:30:10 guys how we can access specific elements inside of this array So

1:30:14 all of these numbers are stored in this lucky numbers array

1:30:18 in this array structure But the question becomes how do we access them?

1:30:21 Right, how can I gain access to one of these?

1:30:24 well, I could say present' d so I'm gonna print this out and now

1:30:27 I can just say lucky numbers so I'm gonna refer to the name

1:30:31 of the array and when I want to access a specific element I can make

1:30:34 an open a Close square bracket and inside of this open and close square bracket

1:30:39 I want to put the index of the element that I want to access

1:30:42 so all of these elements in here have a specific index And I can

1:30:46 access them by putting the index inside of this square bracket so if I

1:30:50 wanted to access this for for example I can put a 0 inside of here.

1:30:54 And now when I run my program you guys will see

1:30:58 We'll be able to print out that for So over here,

1:31:03 I'm printing out four if I wanted to access this 15 I can put a two in here.

1:31:10 So now when I run my program I'll be accessing the 15 as you

1:31:14 can see so one thing you might have noticed by now is that we

1:31:19 start the Indexes and arrays at 0 so when I wanted to access this 4

1:31:23 instead of putting a 1 in here I put a 0 in here, right?

1:31:27 You might think that 4 would be at index position 1 because

1:31:30 it's the first element in the array but in C we start

1:31:34 array indexes at 0 so actually the first element this 4 is

1:31:39 gonna be at index position 0 so if I was going to write

1:31:42 out indexes I would say like 4 is at index position 0

1:31:45 this 8 is at index position 1 this 15 is at index

1:31:49 position 2 16 is a 10x position 3 etc So that's gonna

1:31:53 allow me to access all these elements inside of the array Individually,

1:31:57 so I could print them out just by referring to the index inside of these square

1:32:02 brackets Another thing I can do is I can modify some of the elements inside

1:32:06 of here so Let's say I want to modify lucky numbers in x position 1 so

1:32:10 let's say I want to modify this 8 so I don't like this 8 anymore so I

1:32:14 can basically just assign this a different value

1:32:17 like I normally would so I could say

1:32:19 lucky numbers and I can make an opening close square bracket we can say 1 and I

1:32:25 can just set it equal to something else so I could set this equal to like

1:32:28 200 and Now when we print out lucky numbers 1 instead of printing out an 8.

1:32:32 It's gonna be printing out a 200 so let's go ahead and do that and run

1:32:37 my program and you'll see we're printing out 200 instead of 8 so you can modify

1:32:43 any of the individual elements Inside the array

1:32:46 just by referring to the index and really

1:32:48 when you think about this Conceptually an array

1:32:51 is basically just holding a bunch of variables,

1:32:54 but those variables don't have names So when I access lucky numbers 1

1:32:58 it's the same as me accessing an integer variable It's just that you know,

1:33:03 the array is holding potentially hundreds or thousands or millions

1:33:06 of these different variables So an array is a very useful structure,

1:33:10 especially if you're storing data, that is very similar So, like I said,

1:33:14 I could do this with an N I could also do

1:33:16 this with a double I could do this with a float I can do

1:33:19 this with whatever I wanted But there's gonna be situations where you might

1:33:23 not necessarily know all the elements that you want to put inside your array

1:33:26 So let's say I'm creating an array and I don't know what numbers

1:33:30 I want to put in it yet So maybe I just like want

1:33:33 to create it I want to tell C that we need it but I

1:33:37 don't want to necessarily put anything in it right away Well up here.

1:33:40 We're basically saying lucky numbers that open and close square brackets,

1:33:43 and then we're giving it a bunch of information But if we don't want

1:33:47 to give it all that information instead I can just put a call a semicolon

1:33:52 here but inside of these square brackets I need to tell see how many

1:33:57 elements that this array can hold so I need to tell C Like hey,

1:34:01 this array can only hold like ten elements or this array can hold 20 elements

1:34:05 or whatever So let's say we wanted an array that could hold like ten elements.

1:34:09 I can put a 10 right here and now the Capacity of this integer

1:34:13 array is 10 so it has the potential to hold 10 items and what I

1:34:18 could do is I could come down here and I can start assigning values

1:34:21 to those items so I could say like Lucky numbers 1 is equal to 80, right?

1:34:28 so I'm giving lucky numbers 1 of value and now over here I could print

1:34:33 that value out onto the screen and You'll see we're printing of 80 but if I try

1:34:38 to print out for example lucky numbers 0 so the lucky numbers at in X position

1:34:43 0 This is gonna give me a negative 2 which basically means that it's not found.

1:34:49 So it basically means that there's no Element inside

1:34:53 of lucky numbers at index position 0 but if I

1:34:56 wanted I could do that so I could say like

1:34:59 lucky numbers 0 is equal to 90 and now when

1:35:04 I run my program it's gonna be able to do

1:35:07 that So it'll be able to get that value so

1:35:10 in a lot of situations You're just gonna want

1:35:12 to give these arrays values up front right off the bat,

1:35:15 but in a lot of other situations You're not

1:35:17 gonna necessarily know what's supposed to go in there

1:35:20 But in a situation like that you still have

1:35:22 to tell see how many elements the array is

1:35:25 gonna You still have to like rigorously defined

1:35:28 like hey This array can only hold ten elements

1:35:30 and that's just so C is able to allocate

1:35:33 enough memory in order to hold all of those Elements,

1:35:35 so that's kind of the basics of working with arrays now I do want

1:35:39 to point out one thing that we've been using in this course has been strings,

1:35:44 so I've been creating character strings so I

1:35:47 could say like char phrase and I could

1:35:49 set this equal to Giraffe Academy or other

1:35:53 times in the program we created Like when

1:35:55 we were making our little game we created an arrays and I would just say like

1:35:59 20 and then I would get input from the user and Put that information in here.

1:36:04 This is basically a String of characters.

1:36:07 So anytime we create a string in C like if I was

1:36:11 to create a string over here I called it like array whatever.

1:36:14 This is an array So we've kind of just

1:36:17 been taking this for granted Like I've just been calling

1:36:20 this a string or calling it like a string of characters

1:36:23 But it's basically just an array of characters The only

1:36:26 difference is this is such a common thing to use

1:36:29 in our programs that C makes it a little C

1:36:32 makes it special and makes it really easy for us

1:36:35 to create it But just like that array of integers.

1:36:37 This is also an array So now you kind of have a better understanding of what

1:36:41 strings are and what they're actually doing

1:36:46 In this tutorial I'm going to talk to you

1:36:48 guys about functions in C a Function is

1:36:51 basically just a collection of code that performs

1:36:54 a specific task So what you can do is you can take a bunch of code,

1:36:59 you know maybe like five or six or twenty lines of code and put

1:37:02 it inside of a function and then when you want to access that Code

1:37:06 that's inside the function You can call

1:37:08 the function and generally when you create

1:37:10 a function you'll design the function to perform

1:37:13 a specific task So the function will

1:37:15 have a specific purpose so I'm gonna show you guys how to create functions

1:37:19 how to work with them and we'll just talk about the basics of using functions

1:37:23 like I said a function is basically just a collection of code that Does

1:37:27 a specific task so we can actually create a function here in our C program?

1:37:31 Now you'll notice up here.

1:37:32 We have this block of code It says int main and there's an open

1:37:36 and closed parenthesis and it open and close curly brackets and we've kind

1:37:39 of just been using this for the entire course like up to this point

1:37:43 the course we've Just been kind of using this main little block of code here.

1:37:48 But actually this is a function and Sometimes you'll

1:37:52 hear people will call this a method as well.

1:37:54 So function and method are two words that Essentially mean the same thing,

1:37:58 but you generally in C.

1:38:00 We're gonna refer to these as functions But this is called

1:38:03 the main function and the main function is Basically a function

1:38:06 that's gonna get executed when we start running our program

1:38:10 And so we've already been using a function this main function,

1:38:13 but if you want we can actually create other functions in our program

1:38:17 So make sure you guys have to do that So let's go down

1:38:19 and we're gonna go here outside of this function and the way you

1:38:22 can tell we're outside The function is

1:38:24 we're outside of this ending curly bracket.

1:38:26 So down here I'm going to create a function

1:38:29 and when we create a function We actually have

1:38:31 to give C a couple pieces of information The first

1:38:35 thing we have to tell C is the return

1:38:37 type of the function And so actually we're going

1:38:40 to talk more about return type in the next

1:38:42 tutorial But for now just know that return type

1:38:45 is basically the type of data that the function

1:38:48 is going to return to the user So sometimes

1:38:50 your function can actually give information back to whoever

1:38:53 calls it in our case though We're just gonna

1:38:57 say void and void basically means that this function

1:39:00 isn't to return any information That's the first

1:39:02 thing that you have to tell see the second

1:39:04 thing we have to do is give this function

1:39:06 a name And generally when we're naming a function,

1:39:09 we're gonna want to name the function according to what it does So in our case,

1:39:14 we're gonna create a function that says hi to the user So I'm

1:39:17 just gonna call this function say hi because that's what its gonna do.

1:39:20 It's gonna say hi Whenever I create a function now I want to make

1:39:24 an open and closed parenthesis and I'm

1:39:26 gonna make an open and closed curly bracket.

1:39:28 So now any Code that goes in between these curly brackets.

1:39:32 It's going to be considered code that's inside of this function So let

1:39:37 me show you guys I'm just gonna make this a very simple function.

1:39:39 We're gonna do one thing inside of here

1:39:41 We're just gonna make a printf and we're gonna

1:39:44 print out hello user So I've created my function

1:39:47 it's called say hi and inside of this function.

1:39:50 We're just printing out.

1:39:52 Hello user So now let's go ahead and run our program and see what happens.

1:39:56 So I'm just gonna build and run my program But you'll see

1:39:59 up here that hello user doesn't get printed out right in other words.

1:40:04 I came down here I said printf hello user But this didn't get printed out

1:40:09 and this is the first lesson with functions is that in order to use the code?

1:40:13 Inside of the function we have to call it Calling a function

1:40:17 basically means we're telling C that we want to execute all the code

1:40:21 inside of it So if I want to execute this code I

1:40:24 can Call this function And the way that I do that is just

1:40:27 by typing out the name of the function and an open and closed

1:40:30 parenthesis So now instead of just printing out nothing C is actually going

1:40:34 to go over here and execute all the code in the say hi

1:40:38 Function so let's run our code and you'll see now we're printing out.

1:40:42 Hello a user So the difference between this main

1:40:46 function up here and this say hi function is

1:40:49 this main function is a very special function right when

1:40:52 we run our program this main function basically gets called

1:40:55 but then if we want to use other Functions

1:40:58 we can call them from inside of this main function

1:41:00 and that's sort of how this is gonna work So

1:41:03 also I want to show you guys one other thing.

1:41:05 I just want to illustrate The flow that these functions

1:41:09 take I want to show you guys how these get

1:41:12 executed by the program So I'm printing out top and I'm

1:41:16 printing out bottom and I'm calling say hi right here

1:41:19 So now I'm gonna run my program again and you'll

1:41:21 see we're printing out top And then we're printing out

1:41:24 hello user then we're printing out bottom and this might

1:41:27 have looked a little bit better if I put new Lines,

1:41:29 but essentially what's happening is when C goes

1:41:32 to execute this program It's gonna execute this first

1:41:35 line of code printf So it's gonna print out top then we're telling C that we

1:41:40 want to call this a hi Function C is now gonna jump over here to the say

1:41:46 hi function and it's gonna execute all

1:41:48 of the code inside of here So in this case, we just have one line of code,

1:41:52 but I could put any number of lines of code inside

1:41:55 this function So if I have like five lines of code,

1:41:57 it would execute all five of those lines of code Then it's gonna

1:42:00 jump back up here and it's gonna move on to the next instruction.

1:42:03 So that's kind of how that works And that's

1:42:06 sort of the basics of writing functions anytime You have

1:42:09 a block of code or a bunch of code

1:42:11 that does one thing or that you want to kind of?

1:42:14 Encapsulate into its own little container that you

1:42:17 can call You can put it inside

1:42:19 of a function and then whenever you want to access it You just call it,

1:42:23 but these functions can actually do a lot more So one thing we can do

1:42:27 is we can give these functions some

1:42:29 Information so I can actually give this function

1:42:31 a piece of information and it can use

1:42:34 that information to do different things This is

1:42:36 what's called a parameter and a parameter is basically just a value that we give

1:42:41 to a function So let's say in this say hi function instead of just saying

1:42:44 hi to the user We want it to say hi to someone specifically well I can

1:42:49 actually come down here in these parentheses

1:42:51 and I can specify a function Parameter and I

1:42:54 can specify a parameter a lot like I would specify a variable so I could

1:42:58 say for example char name and an open

1:43:01 and closed square brackets and I'm basically what

1:43:04 I'm saying down here is this say hi function is going to take as a Parameter

1:43:09 in other words as an input a string of characters called name and what I

1:43:14 can do is I can come down here and I can actually print that out

1:43:18 so I could say Percent s and we could print out name now Over here

1:43:24 when I call this say hi function because

1:43:26 I specified that it's gonna take a parameter

1:43:29 I have to give it a string so I could give this a string like

1:43:33 Mike like I'll put my name in here when I call this say hi function now,

1:43:38 I'm Passing the value Mike into this function So Mike is

1:43:43 gonna get stored inside of this name array and we're gonna

1:43:46 be able to print it out down here Let's go ahead

1:43:49 and run our program and you'll see it says hello Mike.

1:43:52 So instead of saying hello user now We're able

1:43:55 to customize what it says The cool thing about this is

1:43:58 I could copy this line of code Like I could

1:44:00 call this function a bunch of times with different names.

1:44:03 Actually.

1:44:03 I'm gonna put in a new line down here So

1:44:05 this is more obvious so I could say hello Mike.

1:44:08 Hello, Tom Hello Oscar now when I run my program It's saying hello to each

1:44:15 of these different people because we define

1:44:19 the function and we can pass it different

1:44:21 names and Depending on what we pass it depending on the parameter that we give

1:44:26 it it'll perform its task a little

1:44:27 bit differently You can also specify multiple parameters.

1:44:30 So let's say in addition to accepting the name as a parameter.

1:44:34 We also accepted their age So I could say char

1:44:37 name And then down here I can specify int age.

1:44:40 And now we're basically saying that this is going to take

1:44:43 an age as well So I could say hello and then

1:44:46 I could say you are and we'll say the age So

1:44:50 now over here instead of an addition to just saying name we

1:44:53 can also say age so now when I run this Function

1:44:57 and when I call it I have to also give

1:44:59 this an age so we could say like 40 23 and 70

1:45:05 and now this is gonna print out all that information for us.

1:45:09 So hello Mike you are 40.

1:45:10 Hello.

1:45:11 Tom.

1:45:11 You're 23 Hello, Oscar, you're 70 and you can pass as many parameters

1:45:15 as you want into one of these functions You always

1:45:19 just have to make sure though that you're passing

1:45:21 a parameter When you're calling the function just like that.

1:45:24 So that's the basics of using functions and in the next tutorial.

1:45:27 We're going to talk about This guy over here,

1:45:30 which is the return type and we're gonna look at how we can

1:45:33 get information back from a function In this tutorial I'll talk to you

1:45:42 guys about return statements in C Where a turn statement is a special

1:45:46 line of code that we can include in our functions which will allow them

1:45:50 to return information back to whoever called them so I can write a function

1:45:55 and that can have that function give information back to whoever called it

1:46:00 that could be information like the result of some operation or you know

1:46:04 It could be it a message telling whoever called it how the function went.

1:46:07 It could be anything like that So I'm gonna show you guys Basically how we can

1:46:11 do that and I'll just give you an overview

1:46:13 of like return types and all that stuff.

1:46:15 So down here I'm actually going to create

1:46:17 a function and we're gonna create a function

1:46:18 that Cubes a number so when you cube a number generally you take it to the third

1:46:23 power So I could say like 2 raised to the third power Which is the same

1:46:27 as just saying 2 times 2 times 2 this would be cubing a number, right?

1:46:31 You're taking it to the third power So I'm gonna

1:46:34 make a function that's gonna do that basically This function will

1:46:37 accept one parameter which is gonna be a number and it'll

1:46:41 cube that number and then once it's done cubing the number,

1:46:44 it'll give That value back to the caller

1:46:46 and you guys will see how that will work So I do want to point out one

1:46:51 thing whenever we are Returning values in our function.

1:46:55 So if you're gonna write a function that's gonna

1:46:57 return a value You always want to put it

1:47:00 above the function that's going to be calling it

1:47:02 So for example like this main function down here,

1:47:05 this is the first function that executes So any code we

1:47:08 put in here is gonna get executed first if I want

1:47:11 to return a value with my function I'm gonna define

1:47:14 the function here above the main method and there's a couple reasons

1:47:17 why we want to do that essentially It'll just make sure

1:47:20 that everything works correctly So we want to make sure that we

1:47:24 define this function before we actually call it down here

1:47:26 and get a value back so the first thing that I need

1:47:29 to tell C when I want to create a function is

1:47:32 What type of data I want to return if you've been

1:47:35 following along with this course You'll know in the last tutorial

1:47:39 We use the void return type which basically means we weren't going

1:47:44 to return any information But now we want to actually return

1:47:47 information So we want to Cuba number so why don't we

1:47:51 return a double and you can put any datatype here you

1:47:53 want you can also put like int or Character or character array,

1:47:57 but let's just do double and we're gonna give this a name So I'm

1:48:00 gonna call this cube and I'm gonna

1:48:02 make an open and closed parenthesis and inside

1:48:05 of this parenthesis We're gonna allow this method

1:48:07 or we're gonna allow this function to accept

1:48:10 one parameter as input So it's just going to be another double and call it num.

1:48:14 So Basically, what we want to do is we want a cube num

1:48:18 and we want to give that value back to the user so over Here,

1:48:21 I'm just gonna create a variable It's just gonna be another double called

1:48:25 result and I'm gonna set it equal to num Times num times num.

1:48:31 So basically this variable result now represents or now stores the cubed value

1:48:38 of num all we need to do to return this value now is just

1:48:42 say return Result and this return keyword is basically gonna do a couple

1:48:49 things The first thing it does is it breaks us out of the function.

1:48:52 So whatever we put that return keyword It's gonna break us out of the function

1:48:57 and it's gonna return this value back to the caller So down here we can

1:49:02 actually call this function so actually what I want to do is I want to print

1:49:07 out the answer that we get back so I can do printf and in here,

1:49:11 I'm just gonna say Answer and we'll print out the answer.

1:49:14 So percent F so I'm using% F because we're expecting to get

1:49:19 a double back from here remembered double is a return type and Over here.

1:49:24 I can just say cube and we'll pass in a number.

1:49:28 Let's pass in 3.0 Essentially what's gonna happen now

1:49:32 is we're going to call this function All this code

1:49:35 is going to execute and this function is going

1:49:38 to get a value back basically the value of cubing 3.0.

1:49:41 So normally like in the past in this course what we wanted to print

1:49:45 on a number we'd have to put like 4 or 76 or you know,

1:49:49 whatever we'd have to put a number right there But we

1:49:53 can just call this function because eventually this is going to get

1:49:57 a number back anyway So this will contain a number after the functions

1:50:02 done being called So I can actually just run my program now

1:50:06 and this is going to print out the result of cubing 3.0

1:50:09 So you'll see down here we get 27 point 0 0 so

1:50:13 3 cubed is 27 3 times 3 is 9 9 times 3

1:50:17 27 so looks like our cube function works Let's try with another number.

1:50:21 Why don't we do 7?

1:50:24 So let's run this again and we get 343 I'm guessing

1:50:28 that's right So basically what we did is we created this function

1:50:33 and we use this return keyword and we returned back

1:50:36 to the caller The value of cubing the number and that's really cool

1:50:40 And actually we can make this a lot simpler so I could

1:50:43 actually just get rid of this Result variable and I could just

1:50:47 straight up return Num cubed and this is and this is gonna

1:50:50 do the same exact thing so we should get the same answer.

1:50:54 Yeah So this can be really awesome And one thing I want to point

1:50:59 out is this return keyword will actually break us out of the function

1:51:02 So if I came down here and I said like printf and I

1:51:06 printed out like here This actually

1:51:08 this code is actually never gonna get executed.

1:51:10 So if I was to run my program You'll notice that it's not printing out here.

1:51:15 So that never gets printed out in other words

1:51:18 this line of code down here Print out here.

1:51:22 This never gets touched because when we use

1:51:25 this return keyword This breaks us out of the function.

1:51:29 So whenever C sees this return keyword, it just exits the function and goes back

1:51:34 down here to the normal program So just keep

1:51:38 in mind that you can't really put any

1:51:40 code after you use this return keyword because it

1:51:42 will break you out of the function and actually

1:51:44 want to talk to You guys about one more thing really quick you'll notice up here

1:51:48 I'm creating my cube function above the main function.

1:51:51 So I actually created this above here and the reason

1:51:54 that I did that was because if I was to move

1:51:57 this down here below the main function and I try to run

1:52:00 my program you'll notice that we're getting an error over here

1:52:03 and actually if we look at the error down here

1:52:05 in the Output it says error Conflicting types for cube now Essentially what's

1:52:13 happening is when I create this function down here below this main

1:52:18 method When the main method over here tries to call it.

1:52:21 It actually doesn't know About this function yet like

1:52:25 it doesn't necessarily know like what this function is what

1:52:28 it's going to return and what parameters it's gonna take

1:52:32 because we Created it after we created this main method.

1:52:35 So what I can do is actually something called Prototyping

1:52:39 and when I prototype it'll allow me to create this function below

1:52:43 this main function without getting this error and basically when you

1:52:47 prototype I'm basically just going to write out the Function signature,

1:52:50 so we would call this like the signature basically the return

1:52:54 type and the parameters and the name So if I was

1:52:57 to put this up here and put a semicolon now when I

1:53:01 run my program You'll see that we're not getting this error anymore,

1:53:05 and we're actually getting the answer back So this is a way

1:53:08 that you can create functions below that main function without having

1:53:12 any problems and you know basically any functions that I created I

1:53:15 could put another prototype up there at the top and It's gonna

1:53:20 be fine In this tutorial and talked to you guys about

1:53:26 if statements in C an if statement is a Programming structure which

1:53:30 we can use to help our programs to make decisions by using

1:53:34 an if statement our programs can actually respond So in certain circumstances,

1:53:38 they can do certain things and in other circumstances They can

1:53:41 do other things and if statements are Extremely powerful and they're

1:53:45 a great way to add some intelligence to our programs

1:53:48 for the purposes of this tutorial We're gonna be building a function.

1:53:51 So we're gonna build a function and it's

1:53:53 gonna be called the max function Basically

1:53:55 what this function is gonna do is it's

1:53:57 gonna take two parameters two numbers and it's

1:53:59 gonna tell us which number is the biggest so I could give it like four

1:54:03 and I can give it a Ten and it'll tell us which number is bigger.

1:54:07 Let's go ahead and do that.

1:54:09 I'm gonna make this function up here and It's going to return an integer

1:54:14 and I'm just gonna call it max and it's gonna take as parameters two integers so

1:54:19 it's gonna take an integer num one and it's gonna take another integer num two

1:54:23 and Now we'll just make the actual function

1:54:27 body so inside this function our job is

1:54:29 to figure out whether num one is the biggest or num two is the biggest

1:54:33 and we're going To return the biggest to the caller so the first thing I'm

1:54:38 gonna do is create a variable and it's gonna be an integer and I'm just

1:54:41 gonna call it result and I'm not gonna give it a value right away What we

1:54:45 need to do in this function is we need to figure out which one's bigger

1:54:49 we need to figure out if num one is the biggest or We need to figure

1:54:53 out if num two is the biggest and this is a perfect situation To use

1:54:57 an if statement an if statement allows us to check a certain condition And if

1:55:02 that conditions true we can do one thing and if the conditions false we can

1:55:06 do another thing so down here I'm gonna use an if statement I'm basically just

1:55:10 gonna say if and then we're gonna make an open and close parenthesis and I'm

1:55:13 gonna make an open and closed curly bracket

1:55:15 now Inside of here inside of these parentheses.

1:55:18 We need to specify a condition This is

1:55:21 something that's either gonna be true or false if

1:55:24 the condition inside of these parentheses is true We're

1:55:28 gonna execute the code inside of these curly brackets.

1:55:30 If it's false, then we're just gonna move on so In order

1:55:35 to tell whether or not num1 is bigger than num2 or num 2 is

1:55:39 bigger than num 1 we can actually compare them so down here I

1:55:42 could say if Num 1 and I can use this greater than sign Num2.

1:55:47 I'm basically saying if num 1 is greater than num2 then I want to do something

1:55:55 so down here we can set result equal to num 1 if This condition is true.

1:56:01 Then we're gonna do this.

1:56:02 So only when num, 1 is greater than num2 are

1:56:06 we gonna set result equal to num 1 so basically

1:56:09 by using this if statement we're able to Tell which one

1:56:12 is bigger but here's the thing what if num 1 isn't?

1:56:14 Greater than num- what if num 2 is greater than num 1?

1:56:18 well I can use something called an else statement so I

1:56:20 can come down here and I could say else and I

1:56:23 don't actually need to use an open and close parentheses I

1:56:26 can just make an open and close curly bracket And the code

1:56:28 inside of this else block is going to execute if

1:56:32 the condition up here is false so if this condition is false

1:56:36 if num 1 is not greater than num 2 then we

1:56:40 can just Set result equal to num 2 and then down here.

1:56:45 Finally.

1:56:45 We're gonna return Result so depending on whether or not

1:56:49 this is true result is either gonna set equal to num 1

1:56:54 or it's gonna be set equal to num- So let's go

1:56:57 down here into our main method and why don't we call this?

1:57:01 So I'm actually gonna print out the result So I'm just gonna

1:57:04 say printf and we're gonna be printing out an integer and Over here,

1:57:09 I'm gonna call this function.

1:57:10 So I'm just gonna say Max and I'm gonna pass in two numbers

1:57:14 So we're passing a 4 it will pass in a 10 So I'm passing

1:57:17 in a 4 and I'm passing in a 10 and this is going

1:57:20 to return to us whichever one is bigger in Other words this should return 10.

1:57:25 So let's go ahead and run our program and you'll see over here.

1:57:28 We're returning 10 So we got the value of 10 back from this function.

1:57:33 Let's try it with the other number So let's make the first number bigger.

1:57:36 So now we have 40 and 10 So this should give us

1:57:40 40 back awesome so over here we have an awesome function, right?

1:57:44 We're basically checking to see if num 1 is greater than num- if it is,

1:57:47 then we're gonna return num 1 otherwise we're

1:57:50 going to Num2 and it's important to note

1:57:52 that even if I made these equal So if I made it forty and forty,

1:57:57 we're still going to get back the correct

1:57:59 answer So it's still gonna give us forty back.

1:58:02 So this is the basics of using if statements, right?

1:58:04 We say if we specify a condition if that conditions

1:58:08 true We're gonna execute this code if that conditions

1:58:12 false We're gonna execute this code down here

1:58:15 And that is an extremely powerful structure in our programs

1:58:18 and we can really use this to respond

1:58:21 to different input right So depending on what the user

1:58:24 puts in num1 and num2 we can do different

1:58:26 things But this is just sort of the beginning.

1:58:29 So why don't we try to make this function a little bit more complex?

1:58:32 So down here we're allowing the user to input two parameters So

1:58:37 we're accepting two parameters into this max function num1 and num2 and we're

1:58:40 able to spit out which one is bigger But let's up

1:58:44 the ante a little bit what if we wanted to accept three parameters?

1:58:48 So instead of just passing in two numbers We

1:58:51 wanted to be able to pass in three numbers

1:58:53 and have the max function spit out the biggest

1:58:55 so over here we can modify This max function,

1:58:58 so I'm just gonna make another parameter.

1:59:00 So I'm going to say int and we'll call this one num three So now

1:59:05 this function is accepting three parameters num1 num2

1:59:08 and num three But in order to figure

1:59:11 out which one of these is the biggest we're actually gonna need a more complex

1:59:15 if statement So I'm gonna get rid of this and let's just start from scratch.

1:59:19 So let's think about this How can we

1:59:21 figure out which one of these is the biggest?

1:59:24 well, what I would recommend is we could say

1:59:26 if num 1 is greater than num2 and Num,

1:59:29 1 is greater than num 3 then we know num ones the biggest right?

1:59:33 So if num1 is bigger than num2 and it's bigger

1:59:35 than um 3 we know it's the biggest if num2 is

1:59:38 greater than num1 and num2 Is greater than num 3 then

1:59:41 we know num2 is the biggest and otherwise we can just

1:59:45 say that num 3 is the biggest so I'm gonna

1:59:47 show you guys how to do this and we're actually going

1:59:50 to be introducing a couple more Concepts for if statement so

1:59:53 I'm just gonna say if and I want to check a condition

1:59:56 so I basically want to see if num 1 is greater

1:59:59 than or equal to num 2 and num threes so down

2:00:02 here I can say num one is Greater than or equal

2:00:06 to num2 and now I also want to check to see

2:00:09 if num1 is greater than or equal to number three Because

2:00:12 if num1 is greater than or equal to num2 and it's

2:00:15 greater than or equal to number three We know num1

2:00:18 is the biggest so I can use this special signal here.

2:00:22 This is called a logical operator and Basically,

2:00:25 this is going to allow us to put another condition in here so I could say num

2:00:29 1 is greater than or equal to num 3 basically what this and does is it allows us

2:00:35 to put two conditions so we can check

2:00:37 two things and This whole thing is only going

2:00:41 to be true if this conditions true and this conditions

2:00:45 sure So if one of these guys is false,

2:00:47 then this if statement isn't going to get executed so down here

2:00:51 I'm just gonna open this up and if this is the case

2:00:53 that we want to set result equal to num 1 because

2:00:56 if num 1 is greater than or equal to num- and Num,

2:01:01 1 is also greater than or equal to number 3 then we know num one's the biggest.

2:01:06 Here's the thing We also want to check to see if num 2 is the biggest

2:01:10 So if this is false We also want to check to see if num 2 is

2:01:15 the biggest now we can use something called else if so down here I could type

2:01:19 out else if and I can make another

2:01:22 open and closed parenthesis and inside of these parentheses.

2:01:25 We can actually check another condition So if this stuff up here

2:01:29 at this conditions false then else if is saying let's come down here.

2:01:34 Let's check another condition So here I want to do basically the same thing I'm

2:01:38 gonna check to see if num 2 is greater than or equal to num 1

2:01:41 and Num 2 is greater than or equal to num 3 So if this condition

2:01:48 is true if num 2 is greater than or equal to num 1 and it's greater

2:01:52 than equal to num 3 Then we know for a fact that result is gonna

2:01:57 be num- because that's the biggest now finally down here We can just say else

2:02:01 and we can make another to open and close curly bracket And remember this code

2:02:05 is going to get executed when neither of these conditions is true So here we can

2:02:10 just set result equal to num 3 So let me walk you guys through

2:02:15 this one more time up here We have this if statement and in addition to checking

2:02:19 to see if num1 is greater than or equal to num2 we're also using this special

2:02:23 operator called and and we're checking to see if num1 is greater than or equal

2:02:28 to num 3 if That's the case then result is going to be equal to num

2:02:32 1 Otherwise if this condition is false Then we're gonna come down here and we're

2:02:38 going to check this other condition So we're gonna check to see if num 2

2:02:42 is bigger than num 1 and if it's bigger than num 3 if that's true,

2:02:46 then we're gonna do this Finally if neither

2:02:49 of these conditions up here is true We're

2:02:51 just gonna go ahead and set result equal

2:02:54 to num 3 and then we're gonna return results.

2:02:56 So this looks good to me Let's test this out and we're gonna see if it works.

2:03:01 So now when we call max we need to include

2:03:04 three numbers So why don't we just do 1?

2:03:06 2& 3 and This is basically going to spit out the biggest one,

2:03:11 which should be 3 so Let's run our program and you can see

2:03:14 over here we get 3 so let's try to make one of the other

2:03:16 ones the biggest so we'll make the 1 in the first slot the Biggest

2:03:19 and let's run this again and you can see that it returns 10.

2:03:23 So this function is working So that's how we

2:03:26 can use these ands and we can also use else--if

2:03:29 in order to check additional conditions so the and is

2:03:32 used to check more than one condition inside of the if

2:03:36 Parentheses and the else if is used to check

2:03:39 another condition when this conditions false and so that is

2:03:42 actually gonna work out pretty well Now I want

2:03:45 to show you guys a couple other things There's a couple

2:03:47 other things that we can do with these if statements

2:03:50 so for example I'm gonna come down here and just

2:03:52 show you guys some of these In addition to using

2:03:55 that and we can also use something called or so

2:03:57 I'm just gonna type out a simple if statement I

2:04:00 could say if 3 is greater than 2 and instead of using and like this I can use

2:04:06 or and or is basically gonna allow us to Check two conditions,

2:04:10 and the whole thing is going to be true

2:04:12 if only one of those conditions true so I could

2:04:15 say if 3 is greater than 2 or 2 is greater than 5 So only one of these is true,

2:04:22 right 3 is greater than 2 that's true but 2

2:04:25 is not greater than 5 so this guy's false,

2:04:28 but when we use or One of these two conditions

2:04:32 has to be true for the whole thing to be true.

2:04:35 So if I like printed something out here like true

2:04:41 Now when I run my program we should print true

2:04:44 because we're using that or and you see that we

2:04:47 do But if I was to make this guy false, so if I made this 3 less than 2 now It's

2:04:53 not gonna print out true and we can just print out false.

2:04:56 So Now we're gonna end up renting out false because both

2:05:00 of these guys are false So you can see down here.

2:05:04 We're saying false.

2:05:05 So the difference between and and or when we have and like

2:05:09 this Both of these conditions have to be true in order for the whole

2:05:13 thing to be true and for us to execute this code When we

2:05:16 have or only one of these guys has to be true so that's kind

2:05:20 of the difference there and I want to show you guys a couple other

2:05:23 things that we can use so Over here we're using like a less than

2:05:27 sign so for example If 3 is less than 2 I can also use

2:05:30 a greater than sign I could use the less than or equal to sign.

2:05:33 I Could use also a greater than or equal

2:05:36 to sign and if I wanted to check for equality I

2:05:39 could use a double I could use a double equals

2:05:41 So the double equals will check to see if 3

2:05:44 is equal to 2 we can also use one more

2:05:47 which is this exclamation point an exclamation point basically means

2:05:51 not equals so I could say if 3 is not

2:05:55 equal to 2 then we'll print out true so down here.

2:05:57 Let's run this and you'll see that we get true So yeah, there you go.

2:06:02 One more thing.

2:06:03 I want to show you is how we can negate an entire Operation.

2:06:07 So for example, if I said 3 is greater than 2

2:06:10 this is gonna be true right so we'd end up printing

2:06:13 out true down here because 3 is greater than 2

2:06:15 but I could surround this whole thing with Parentheses and I could

2:06:20 put an exclamation point right before this and this is

2:06:23 called the negation operator and it's basically going to negate Whatever

2:06:27 this ends up being so this is true And we put

2:06:30 this negation operator here this whole thing is going to be false.

2:06:33 And so now you'll see Because this is

2:06:36 true and we're using the negation operator.

2:06:37 We're not going to end up printing out true And you can see

2:06:42 we just don't print But if I put a false condition in here,

2:06:46 like if I said three is less than two This is false.

2:06:49 But because we're negating it now, it's gonna end up being true So when I run

2:06:54 my program you can see that we get true So that's

2:06:57 sort of the basics of using if statements and I cover

2:06:59 just about everything that you can do We can use ands,

2:07:02 we can use all these different comparison operators We can use

2:07:05 ORS and we can use this exclamation point to negate something

2:07:13 In this tutorial I'm going to teach you guys how to build

2:07:17 a calculator in see have you been following along with this course?

2:07:20 You'll know that in the beginning of the course.

2:07:22 We actually created a calculator and it was very

2:07:25 basic calculator basically We let the user input two numbers

2:07:27 and we took those numbers we added them together and we

2:07:30 printed the answer out onto the screen in this tutorial

2:07:33 I'm gonna take some of the stuff that we've

2:07:35 learned since then and show you guys how we can

2:07:37 build a fully functional for function calculator So this calculator

2:07:41 will be able to add Subtract multiply and divide

2:07:43 and we're gonna let the user decide which one

2:07:46 they want to do So we'll let them decide if

2:07:48 they want to add or subtract or whatever it's gonna

2:07:50 be pretty cool and we're gonna end up using some

2:07:52 of the stuff that we've learned recently in the course

2:07:54 like if statements we're also gonna use like getting input

2:07:57 from users and I'm gonna show you guys how you

2:08:00 can get characters as Input from a user as well.

2:08:03 So down here.

2:08:04 We want to start making our calculator,

2:08:06 basically What I'm gonna do is I'm gonna ask the user to enter

2:08:09 a number Then I'm gonna ask them to enter an operator like plus minus division

2:08:14 or subtraction finally we're gonna ask them to enter in a third number

2:08:17 and then we'll Depending on what operator

2:08:19 they specify so addition and subtraction whatever we

2:08:22 will perform the correct operation and we'll print out the number So the first

2:08:26 thing I want to do is create Variables where we can store the numbers

2:08:30 and the operator so I'm gonna make some double variables and I'm just gonna

2:08:34 call this one Num 1 and then we'll make another double call it num2 finally,

2:08:40 we'll make a variable that will store the operator that they enter so

2:08:43 this is just gonna be a char and I'm just gonna Call it op4 Operator.

2:08:47 All right.

2:08:47 So now we want to actually get input from the user I want to figure out what

2:08:52 numbers they want to use and then what operator

2:08:55 they want to use as well So down here.

2:08:57 Why don't we get some input?

2:08:59 The first thing I'm going to do is I'm just

2:09:01 gonna print out a prompt so I'm just gonna say enter

2:09:03 a number And now what we want to do is

2:09:07 we want to be able to Take the number they give

2:09:10 us and store it inside of one of those doubles

2:09:13 that we created So I'm just gonna use this scanf function

2:09:16 Scanf and in here I'm gonna specify that we want

2:09:19 to get a double So remember when we're using scanf if we

2:09:23 want to scan for a double we use F and L F is going to scan for a double and now

2:09:29 I'm going to use a comma and we'll put the Variable

2:09:32 that we want to store in here so I'm gonna use

2:09:35 this ampersand and I'm just gonna say num 1 So

2:09:38 this is exactly what we need to do to be able

2:09:41 to get the users input and store it into num 1

2:09:44 The next thing we want to do is get the operator.

2:09:46 So I'm gonna type out another prompt and I'm

2:09:49 just gonna say enter operator basically They'll be entering

2:09:53 in plus sign minus sign asterisks or forward slash

2:09:56 depending on what they want to do and again

2:09:59 now we're going to scan for a Character

2:10:02 now when we're scanning for a character and scanf

2:10:04 and actually I don't think i've talked about this yet

2:10:07 in this course We want to do something special.

2:10:09 So I want to put a% and a C.

2:10:12 But before I put% C I want to put a space

2:10:16 so whenever we're getting a character from the user using scanf,

2:10:19 we always want to put a space Right here before% c otherwise,

2:10:23 it's not gonna work.

2:10:25 And again we want to store this in that Opie variable so I'm

2:10:29 just gonna say o P and One more time we're gonna get another number.

2:10:33 So I'm just gonna copy this paste this down here and We're gonna get

2:10:38 this one for num2 So now we should have all of the input for our program.

2:10:44 So I'm getting the first number I'm getting

2:10:46 the operator and I'm getting the second number.

2:10:48 The last thing we have to do now is actually do the math

2:10:51 So we're gonna have to figure out which operator they wanted to use Right,

2:10:56 so we have this Opie variable and this is storing like a plus sign a minus sign

2:11:01 a Asterix or a forward slash So depending on what

2:11:04 that's storing we want to print something Different out

2:11:07 so we can actually use an if statement to do this so I can use an if statement

2:11:11 to check and see What operator is inside

2:11:13 of our operator variable and depending on which one it is?

2:11:16 We can do a different Operation so I can say

2:11:19 if and the first thing I'm going to do is

2:11:21 just check to see if the operator is equal

2:11:23 to a plus sign so If the operator that the user

2:11:27 entered is equal to a plus sign then we're gonna

2:11:29 want to add the two numbers together So I'm just

2:11:32 gonna print out these two numbers and I'll just say

2:11:35 num1 plus num2 Because we're gonna add both of them together.

2:11:39 We can also use an L safe So I'm going

2:11:42 to want to check a few other conditions I'm going to check

2:11:44 to see if the operator is equal to a minus Sign

2:11:47 and if the operator is equal to a minus sign then instead

2:11:50 of adding the numbers we're going to subtract them so over

2:11:53 here I'll just copy this and we'll paste this guy down

2:11:56 here So it's going to be num1- num2 And I can

2:12:01 actually just copy this whole thing and we'll paste it down here.

2:12:05 So it makes more room down here We'll paste this other elsif and here we're

2:12:09 going to check to see if it's division So we'll check for a forward slash.

2:12:13 And again, we're just going to want to print out a Decimal

2:12:16 number so it's going to be num1 divided by num2 Finally,

2:12:20 we're gonna check for multiplication.

2:12:22 So if it's multiplication, then we're going to multiply them together So

2:12:26 now we're checking for each of the operators.

2:12:28 We're checking for addition subtraction multiplication and division

2:12:32 But here's the thing what happens if

2:12:33 the user enters in an operator that we didn't want so for example if they

2:12:37 don't enter in any of these operators We're gonna want to tell them so I'm

2:12:41 gonna have this like print out a little error message We're just gonna say else

2:12:45 and basically the code inside this else block

2:12:47 will get executed If none of these conditions

2:12:50 up here true and down here we can just make a print F and I'm

2:12:54 just gonna print out invalid operator So now we have our if statement or if

2:13:00 block setup and this should be everything

2:13:02 that we need to use our little calculator Alright,

2:13:06 so let's go ahead and run this program so you can see

2:13:09 over here It says enter a number so we can enter a number.

2:13:12 Let's enter in five point nine and Enter

2:13:15 an operator we'll add numbers and now let's enter

2:13:18 in 4.0 So we're gonna add five point nine

2:13:21 and four so when I click enter we should

2:13:23 go through all of those if Statements and figure

2:13:25 out what we need to do and you'll

2:13:27 see over here that we're adding five point nine

2:13:30 and four together And we're getting nine point nine.

2:13:32 So looks like the program worked.

2:13:34 Let's try it again.

2:13:35 We'll try another operator Why don't we try to multiply some numbers?

2:13:38 So let's multiply six and Times I don't know five point seven

2:13:44 so we get thirty four point two that seems about right All right,

2:13:49 and then one more time we'll run this and I want to try to enter

2:13:52 in an invalid operator So we'll say like 5.7 and I'm just gonna enter in a G.

2:13:57 So that's an invalid operator That's not going to work and I'll enter my number.

2:14:01 Let's do 8 and you can see it tells us invalid operator so

2:14:05 basically we have a four function

2:14:07 calculator this calculator can multiply divide subtract

2:14:10 and Add and if you don't enter in a correct operator It's smart

2:14:14 enough to yell at you and tell you that you have an invalid operator.

2:14:17 So This seems to work pretty well.

2:14:19 I'm pretty happy with it And you can see down here like

2:14:22 this is a perfect situation for using something like an if statement, right?

2:14:25 We have this variable opie, right?

2:14:27 It's storing some sort of operator We

2:14:30 don't necessarily know what's inside of there

2:14:32 But I can use if statements and I can respond to the different scenarios So

2:14:37 in the situation where it's a plus sign

2:14:39 I can respond in the situation where it's

2:14:41 a minus sign I can respond etc And this just makes our programs a lot smarter

2:14:45 and it helps us to do different

2:14:47 things when different situations arise In this tutorial

2:14:54 I want to talk to you guys about using switch statements in C now a switch

2:14:59 statement is basically a Special type of if

2:15:02 statement which will allow us to compare one value to a bunch of different

2:15:07 values And it's essentially doing the same thing

2:15:09 as an if statement It's allowing us

2:15:11 to check different conditions and if different conditions

2:15:14 are true Then we can do certain things

2:15:16 but a switch statement makes it really really

2:15:18 easy for us to compare one specific value to a bunch of other values now I'm

2:15:24 gonna show you guys how we can use that in this tutorial to make a little

2:15:27 test app So I'm gonna build a little program that will take a letter grade

2:15:32 and depending on the letter grade It will tell you whether or not you did good

2:15:35 or bad on a test So if you got an a on the test it

2:15:38 would say hey you did a good job If you got a B on its house,

2:15:41 it'd be like hey you did Alright, if you got an F on the test to tell you

2:15:45 that you failed the test so we're basically gonna have a variable where

2:15:48 we'll store a grade that you would get on a test

2:15:51 and we'll be able to Respond to that grade using a switch statement.

2:15:54 So it's gonna be pretty cool.

2:15:56 So down here the first thing I'm gonna do is create a variable and it's just

2:15:59 gonna be a character and I'm just gonna call it grade and I'm gonna set it equal

2:16:03 to a So we're gonna say that whoever took this test got an a on the test

2:16:08 and they did really well Now what I want

2:16:11 to do is I want to create a switch statement.

2:16:13 So I'm just gonna say Switch and I'm gonna make an open

2:16:17 and closed parenthesis and inside of this parenthesis I'm gonna put grade so I'm

2:16:21 just gonna put grade and I'm gonna make an opening close curly bracket

2:16:25 So this is sort of like the basic structure for our switch statement.

2:16:29 Now, here's what I'm basically gonna do I'm gonna take this variable

2:16:33 here that I pass into the parentheses grade and I'm gonna compare it

2:16:37 to a bunch of different values So I'm gonna take this I'm gonna

2:16:40 see if it's equal to one value then I'm gonna see if it's equal

2:16:42 to a different value or a different value or a different value and Depending

2:16:45 on the value that it ends up being equal to we're gonna do

2:16:49 something different and these are what are called cases so down here I can

2:16:53 say case and I just want to type out a Character so for example,

2:16:58 I could put a right here and then I'm gonna

2:17:01 put a colon and when I put a right here,

2:17:04 basically what this is saying is the case

2:17:07 that grade is equal to a then I'm gonna put

2:17:10 some code down here that I want to execute

2:17:12 so I could just put like a print statement

2:17:14 and I could say like you did great because

2:17:19 they did great on the test and basically what

2:17:22 this is saying is in the case that the grade

2:17:25 is equal to a we're gonna print this out

2:17:27 and then I want to Put one more thing down here Which is called break and I'm

2:17:31 gonna explain what break does in a second So

2:17:34 I could make one of these little case statements

2:17:36 for every possibility so for every possible grade

2:17:39 that we could get so I'm actually just gonna copy

2:17:42 this whole thing and I'm gonna paste it down here So now I can put a B in here.

2:17:48 So we're saying in the case that the grade is equal to B I'm gonna say you did.

2:17:53 All right, and Then we're also going to break and down here.

2:17:58 I'm gonna make another one So I'll say case and this time

2:18:01 we'll make it C in the case that they get a C.

2:18:04 I'm gonna print out portal e and There's a couple more that we

2:18:09 can check so we can check if they got a D or an F.

2:18:12 So in the case they got a D and I forgot to put

2:18:17 a break statement here and I'm gonna put one down here, too And finally,

2:18:21 we're just gonna do one more for if they failed the test So I'm just gonna

2:18:24 say in the case that they got an F We will print out You failed so Essentially,

2:18:32 what I'm doing here is I'm writing out all of these different

2:18:35 cases So I'm saying in the case that you got an F.

2:18:37 I'm gonna print out you failed and the case

2:18:39 they got a D I'm gonna print out you did

2:18:41 very bad And then we also have this break

2:18:43 statement here and this break statement basically tells our switch

2:18:46 statement that we want to leave So whenever we're

2:18:49 executing this switch statement whenever C is executing the switch

2:18:52 statement It's checking to see if grade is equal

2:18:55 to each one of these cases So for example,

2:18:58 let's say the grade is equal to a it's gonna execute

2:19:01 this print F But if I don't put this break statement in here,

2:19:04 then it's just gonna keep going and keep checking these different

2:19:07 cases So it's always important to put a break statement

2:19:10 in there because I basically just want to break out

2:19:12 of this switch statement I don't want to be in here anymore.

2:19:15 So finally we can check one more thing in the case

2:19:17 that the user Or that we get an invalid grade.

2:19:21 So for example, if someone entered in like a t or G or something that's not

2:19:26 a valid grade so I can use something

2:19:28 called default and Default is basically like an else

2:19:31 so default is going to execute whenever none

2:19:34 of these conditions up here or Matt so then

2:19:37 I can just put something like Invalid grade

2:19:40 or something and this will basically be like, hey,

2:19:43 you got an invalid grade So let's take a look at this switch statement

2:19:47 and we'll just kind of talk a little bit more about what it's doing Basically,

2:19:52 I'm passing this in a variable or a value or something and I'm

2:19:57 gonna check this value against all of these different conditions so in the case

2:20:02 the grade is equal to a We're gonna print you did great

2:20:05 and the case that B is equal to grade will print you did.

2:20:08 All right, etc and this is basically doing what an if

2:20:12 statement doing so I can replicate this entire statement this entire switch

2:20:15 statement as An if statement I can write out if etc else--if

2:20:20 else--if else--if But this just makes it a lot easier So I

2:20:23 don't have to constantly write out all of that if statement stuff

2:20:26 So hopefully we did everything right and let's actually check and see

2:20:30 how our program does So grade is equal to a let's

2:20:34 run our program and see what our message is It says hey,

2:20:38 you did great because we got an A if I made this an F instead of an A Now

2:20:43 it should tell us that we failed So it says you failed if I made this a C

2:20:50 It'll tell us that we did poorly and it says you did poorly So this is a super

2:20:56 easy way for us to check one single value

2:20:58 against a bunch of different conditions And that's really

2:21:01 the point of a switch statement is you're checking

2:21:04 one single value like grade Against a bunch of other

2:21:07 values and that's where it can be extremely useful

2:21:11 So consider using the switch statements They're not appropriate

2:21:14 in every situation and there's I'd say like very

2:21:17 select few situations where switch statements will come in handy

2:21:20 But when they are appropriate switch statements will save

2:21:22 you a lot of time and a lot of hassle

2:21:29 In this tutorial I want to talk to you guys about strux in C now a struct is

2:21:35 a data structure where we can store groups

2:21:38 of data types so inside of a struct I could

2:21:42 store like an integer alongside of a String alongside

2:21:45 a character alongside a double I could sort all

2:21:48 these different data types in one single data Structure

2:21:51 and there's tons of uses for structs and one

2:21:53 of the things we can do with them is

2:21:56 model real world entities So I could basically like

2:21:59 a model Something in the real world inside of my program

2:22:02 and I'm gonna show you guys how to do

2:22:03 that in this tutorial We're gonna be looking at how

2:22:07 we can use a struct to represent a student

2:22:09 inside of our program So imagine we were writing

2:22:12 a piece of software that was using like students.

2:22:15 So it was like storing records of students

2:22:17 or something I could use a struct to represent

2:22:20 a student in my program so over here in my program I'm just gonna come up here

2:22:25 above the main method and I'm actually gonna

2:22:27 create a struct you guys will see like how

2:22:29 these work and how we can use them so I'm just gonna say struct and Over here,

2:22:35 I'm gonna give this a name so I'm gonna call this Student

2:22:38 and a lot of times in C when we're making structs.

2:22:41 You're gonna give them a Capital letter just like

2:22:44 that and I'm gonna make an open and closed curly

2:22:46 bracket now Inside of this struct I can start specifying

2:22:50 what types of data are gonna make up a student

2:22:53 in my program So basically I can define like

2:22:57 different attributes of a student and place them in here

2:23:00 And this will kind of act as a template and you'll

2:23:02 see how we can use this later So let's think

2:23:05 about different attributes of a student Well one thing

2:23:07 would be like the student's name so I could say

2:23:10 char name and I'm gonna make this a array

2:23:13 of characters so this is gonna represent the name and actually

2:23:16 why don't we give this a number so I'm gonna

2:23:19 say 50 so this String can hold 50 characters maximum

2:23:23 and now we're gonna make another one for major So

2:23:27 we're gonna want to store the students major and again,

2:23:29 we'll make this 50 I also want to make an integer

2:23:33 for the students age and a double for the students

2:23:38 GPA So I have this struct student and in here I

2:23:42 have a bunch of after buttes of a student right have

2:23:44 like the students name their major An integer that stores

2:23:48 their age and a double for their GPA Essentially what I did

2:23:52 was I created like a type of student data type So

2:23:55 I basically am allowing myself now to represent a student inside

2:23:59 of my program so let's come down here to this main

2:24:02 method and I'll show you guys how we can use this so

2:24:04 I can basically Create an instance of this student structure so

2:24:08 I can create like an actual student Inside of my program

2:24:12 and the way that I do that is just by saying

2:24:14 Struct and I want to type out the name of the struct

2:24:18 that I want to make in our case It's gonna

2:24:20 be a student and I want to give this a name.

2:24:23 So I'm just gonna call it student 1

2:24:25 and I can just use a semicolon here basically,

2:24:29 what I did now was I created a container

2:24:32 called student 1 that's gonna be able to store

2:24:35 a Name a major in age and a GPA So if you're familiar with arrays in C,

2:24:42 you'll know an array is a special structure that can hold

2:24:45 multiple pieces of information But all the pieces of information inside

2:24:49 of an array needs to be of the same data type

2:24:52 and also they don't have names with a Struct I can have

2:24:55 a bunch of different data types like this and I can

2:24:58 also give them names like name major age and GPA So now

2:25:02 let me show you guys how we can assign some values

2:25:04 to these So for this particular student student 1 I can give them

2:25:08 a name I can give them a major a GPA

2:25:11 and an age so I could say student 1 dot age and I

2:25:16 can set this equal to something so I could say let's

2:25:20 say student ones age is 22 so basically Inside of this student

2:25:24 1 container I'm saying the age of this particular student is

2:25:28 gonna be 22 I can do the same for the GPA so

2:25:32 I can say student GPA is equal to maybe they have

2:25:35 a 3.2 So I'm saying this particular student's GPA is a 3.2.

2:25:39 I can also do the same thing for those strings

2:25:42 So for the name and the major now,

2:25:45 here's the thing about working with strings Remember

2:25:48 in C a string is actually just an array

2:25:51 of characters and when we have an array We

2:25:54 can't like give it a particular value So for example,

2:25:57 if I wanted to give this students name of value,

2:26:00 this is just an array of characters So I can't just

2:26:04 come down here and say student name is equal to something That's

2:26:08 not gonna work because you can't do that with an array

2:26:11 I can use something called the string copy function and this is

2:26:15 a function that will take a String and it'll give it

2:26:19 a value that we specify so it'll basically do what we want

2:26:22 to do like what we did over here with the age

2:26:25 and the GPA but with the name so I could say like Str.

2:26:29 C py and inside of here.

2:26:31 I need to pass this two parameters The first thing

2:26:34 I want to pass is the destination for the string.

2:26:37 So we're gonna pass in student 1 dot name the second thing I want

2:26:41 to pass in is the String that I want to store inside of student 1 name.

2:26:46 So in our case, let's just name it him Jim So now we have

2:26:51 the student 1 names value is equal to Jim and I can do the same

2:26:55 thing for major so I'm just gonna copy this and we'll come down here

2:26:59 and I could say student 1 major and we're gonna set this equal to Business,

2:27:04 so let's say Jim is a business major Essentially what I've

2:27:08 done here is I've created a student and that student had all

2:27:11 the attributes that we defined up here so this particular student had

2:27:15 a name a major in age and a GPA and I gave all

2:27:19 of those values So I assigned this student 1 an age

2:27:23 of 20 to a GPA of 3.2 etc So now what I can

2:27:27 actually do is I can print out all these different values

2:27:30 that are stored inside of this structure so if I wanted for example,

2:27:33 I could like Print out the GPA so I could print

2:27:36 out Student 1 GPA and now we're gonna be printing out 3.2.

2:27:42 And actually it looks like I have a typo here.

2:27:45 This should be student 1 not student Alright so here we're printing out 3.2.

2:27:50 So we're printing out the value of the student's

2:27:53 GPA I can also print out like their name.

2:27:55 So why don't we do that student 1 name and now this is gonna print

2:28:00 out So you see we prints out over there So a struct is a really

2:28:05 useful structure And another cool thing we can do with structs is we could

2:28:09 actually create another Student so I could

2:28:11 create like another instance of that student structure.

2:28:13 So I'm actually just gonna copy all this stuff I'll show you how we can do this.

2:28:18 I'll just come down here and paste it and Instead of student 1 why don't we call

2:28:23 this one student 2 and we can set this student's

2:28:26 age to be like maybe 20 Maybe their GPA

2:28:29 is a 2.5 Let's say their name is Pam and they're studying art So now I have

2:28:37 a completely different student And if I wanted I

2:28:39 could print out this students attributes so I can

2:28:41 print out like student 2 Dot name and now

2:28:44 we're gonna get Pam instead of Jim so you

2:28:46 can see how that works right there So I

2:28:49 could create as many of these students as I

2:28:51 want and this is what's cool about Structs is

2:28:54 I can just define the basic template for a student

2:28:57 in my program and then I can create

2:28:59 Individual students down here that I can work with.

2:29:02 So now I have this like student variable

2:29:04 here I could do whatever I want with it.

2:29:06 I could pass it into a function I could print it out onto

2:29:09 the screen I could use it in something like an if statement I could do

2:29:13 basically anything I want with it It's it acts a lot like a variable

2:29:16 or an array so remember variables and arrays or just Containers we can do

2:29:20 just about anything we want with them and that's the same with a struct

2:29:24 so this has just been kind of an introduction and What you could do

2:29:27 as an exercise is think of other things you might want to model

2:29:30 in your program So maybe something like a book or something like a phone you

2:29:34 could model a phone using a struck basically any type of Information you could

2:29:39 store using a struct just like this In this tutorial I want to talk

2:29:47 to you guys about while loops in C now a while loop is basically

2:29:52 a structure in the C programming language that we can use to loop over

2:29:56 and Continually execute a specific block

2:29:59 of code until a certain condition is false

2:30:02 So I can specify a condition and then I can basically just loop over

2:30:06 a bunch of code until that condition is no longer true and Loops can

2:30:11 be really useful there's a lot of times in our programs or we're gonna

2:30:14 want to let Continually do something while something else is true or while

2:30:18 a certain condition Is met and I'm gonna show you guys a really basic while

2:30:22 loop in this tutorial and then in the next tutorial We're actually gonna build

2:30:26 a really cool guessing game using while loops so it should be pretty fun

2:30:29 And you should get a pretty good idea of like how these things work

2:30:32 The first thing I want to do And this isn't directly related to creating

2:30:35 a while loop though is I want to create an integer So I'm just

2:30:39 gonna create an integer called index and I'm gonna set it equal to 1

2:30:42 now What I want to do is I want to actually create a while

2:30:45 loop and you guys will see where this index comes into play So

2:30:48 in order to create a while loop I can just type out while and open

2:30:51 and close parentheses and then an open and close curly bracket And this is

2:30:55 basically like the outline sort of like

2:30:57 the template for our while loop Basically

2:30:59 how this is gonna work is up here in these parentheses I'm gonna specify

2:31:03 a condition and as long as that condition is true I'm gonna loop through

2:31:09 all of the code inside of these curly

2:31:11 brackets So inside of these curly brackets, I'm gonna put a bunch of code.

2:31:15 And as long as the condition in these parentheses is

2:31:17 true I'm gonna execute all that code so Up here.

2:31:20 We're going to specify a condition.

2:31:22 I'm gonna specify a condition that index is less than or equal

2:31:26 to 5 so while index is less than or equal to 5,

2:31:31 I want to execute all the code inside

2:31:34 of here So I'm gonna put some code in here.

2:31:36 The first thing I'm going to do is I'm just gonna print out the value

2:31:39 of index so over here I'm just gonna say we're gonna print out an integer

2:31:45 and I'm just gonna print out index so we'll be printing out index and actually

2:31:49 why don't we print out a new line every time to There we go.

2:31:54 And Finally after this.

2:31:55 I want to increment the index variable so I can

2:31:58 just say in X is equal to index plus one Basically,

2:32:02 I'm adding one onto the index every time and actually

2:32:05 I want to show you guys something in C

2:32:08 there's gonna be a lot of Situations where you're

2:32:10 gonna want to do something like this basically add

2:32:13 one to an existing variable and in C there's

2:32:16 actually a shortcut we can use so I can

2:32:17 come over here and I could just say index

2:32:19 plus plus and This is gonna do the same thing.

2:32:22 So this is just gonna add one to the index Essentially,

2:32:24 what I'm doing is I'm saying I want to print out index and then

2:32:29 I want to increment index and I'm gonna keep looping Through this code while

2:32:33 index is less than or equal to five So let's go ahead and run

2:32:37 this program and maybe you can predict

2:32:39 what's going to happen You'll see over here.

2:32:43 I'm printing out the numbers one through five So I'm printing

2:32:47 out one two three Four and five and let me walk

2:32:50 you guys through exactly what's happening in this loop over here

2:32:53 we create this index variable and we set it equal to one

2:32:56 and Basically when C goes to execute this program the first

2:33:00 thing it's gonna do before it does anything else is it's gonna

2:33:03 look at this condition and it's gonna check the condition and it's

2:33:06 gonna say okay is index less than Or equal to five.

2:33:09 Well, the first time through index is equal to one.

2:33:11 So it's definitely less than five C's gonna

2:33:13 go through It's gonna execute this line of code

2:33:16 printing out one Then it's gonna execute

2:33:18 this line of code incrementing index Then what C's

2:33:21 gonna do is it's gonna come all the way back up here and it's gonna check

2:33:25 this condition again so it's gonna say okay is

2:33:28 index less than or equal to five and Index

2:33:30 at this point is equal to two so it's less than five so it's gonna go

2:33:34 through execute this line of code execute this line

2:33:37 of code now index is equal to three

2:33:39 and C's gonna go all the way back up to the top it's gonna check this condition

2:33:44 again So before C goes through this loop It's

2:33:47 always gonna it's always gonna check the condition first.

2:33:50 So before it's allowed to go and execute that code again It

2:33:54 has to check the condition to make sure that it's true Eventually,

2:33:57 we're gonna get to a situation where index gets incremented and it

2:34:00 gets incremented up to 6 then C's gonna come back up

2:34:03 here and it's gonna check to see if 6 is less than

2:34:06 or equal to 5 it's gonna be False and we're gonna move

2:34:10 with our lives in the program and that's the basics of while

2:34:13 loops So this seems very simple and that's because it is

2:34:16 all we do is specify condition As long as that condition is

2:34:20 true We keep doing all this stuff in here and there's tons

2:34:23 of situations where while loops are gonna come in handy This is

2:34:27 just one of them where we can print out like numbers

2:34:29 between 1 and 5 I also do want to point out one

2:34:31 thing That you might want to watch out for which is called

2:34:35 an infinite loop and an infinite loop is a situation Where

2:34:38 this condition up here never becomes false So this condition will just always

2:34:43 stay true and there are certain circumstances where you're gonna want

2:34:46 to use something like an infinite loop But in a lot of circumstances,

2:34:49 you're not gonna want to go anywhere near something like that So let

2:34:53 me just demonstrate so you guys can see so if I stopped incrementing

2:34:56 this if I just got rid of index plus plus Now when I

2:35:00 run my program you'll see that it's just continually printing out ones I

2:35:04 mean look at all these ones that are getting printed out you can

2:35:07 see like the text editor is kind of going crazy This program is

2:35:10 just it keeps executing through that loop and it's still going right now

2:35:14 So this would keep going forever if I let it keep going infinitely.

2:35:17 Hence the name, so I'm just gonna terminate this but that'll

2:35:20 kind of show you like how an infinite loop works

2:35:22 and you definitely want to watch out for that because you'll

2:35:25 get into situations as you work with while loops where they'll just

2:35:28 become infinite and you you know you need to watch out

2:35:30 for That especially if you're doing something a little bit more

2:35:33 advanced where you're like siphoning off memory or you're creating processes

2:35:36 or something it can Slow your computer down quite a bit.

2:35:40 So this is a while loop and this is a really great

2:35:42 loop there's also one other type of loop that's similar to a while

2:35:45 loop that I want to talk to you guys about and Before

2:35:48 I do that, I just want to do something really quick.

2:35:50 So I'm gonna set this index equal to 6 All right I'm gonna

2:35:54 set this equal to 6 and I'm gonna run my program and what

2:35:58 you're gonna notice is nothing's gonna get printed out so the code inside

2:36:01 of this while loop Isn't gonna print out because the first thing we

2:36:05 always do when we were on this while loop is we check

2:36:08 this condition So the first thing C's gonna do before it prints anything before

2:36:12 it increments anything it's gonna check this condition and if it's false It's

2:36:16 not gonna do any of this stuff So when I run this program,

2:36:19 you'll see nothing's gonna get printed out and that's exactly what

2:36:22 happens There's another type of a loop that's similar to a while

2:36:26 loop Which is called a do-while loop and I'm going to show

2:36:29 you how to make one All you have to do is

2:36:31 take this wild thing up here and paste it down here below

2:36:34 and then up here I'm just gonna say do and A do-while

2:36:38 loop is similar to a while loop But the first thing

2:36:42 a do-while loop does instead of checking this condition down here.

2:36:45 Is it executes all of this code?

2:36:47 So even though index is equal to 6 we're still gonna print

2:36:51 out 6 and we're still going to increment it and Then we're gonna

2:36:55 check the condition to see if we can keep looping So let

2:36:58 me show you guys this and this should illustrate what this is doing.

2:37:01 And actually I need to put a semicolon here So you can see over here

2:37:05 We're printing out 6 so unlike before when

2:37:08 we just use that while loop now because

2:37:10 we're using a do-while loop We're doing whatever

2:37:13 was inside of that loop before we're checking

2:37:15 the condition and there's gonna be certain circumstances

2:37:18 where a do-while loop will Come in handy.

2:37:21 I would say by far while loops are way more common, but just you know,

2:37:25 it's good to know about do-while Loops

2:37:27 because there are certain circumstances where they'll

2:37:29 come in handy Basically any time you

2:37:31 don't want to be checking the condition first,

2:37:33 you can use a do-while loop But those are

2:37:36 the two basic types and there's actually another

2:37:38 type of loop that we'll talk about later

2:37:39 in the course Which is called a for loop,

2:37:41 but for now We're gonna have some fun with while loops and in the next tutorial,

2:37:44 we're gonna be building a little guessing game In this tutorial I'm gonna

2:37:52 teach you guys how to build a little guessing game and see This is

2:37:55 gonna be pretty cool because we're gonna use a lot of the stuff

2:37:58 that we've learned up to this point in the course In order to build it,

2:38:02 so let's get started basically This guessing game

2:38:04 is going to allow the user to guess

2:38:07 a secret number so we're gonna define a secret

2:38:09 number and we're gonna give the user the opportunity

2:38:12 to try and guess it so if they'll try to guess it if they get it right then

2:38:16 they'll win the game and if They don't get

2:38:18 it right then We'll let them guess again Right,

2:38:20 so we'll basically let them keep guessing what the secret number is until

2:38:23 they get it so down here We want to create a couple variables

2:38:27 The first variable I want to create is gonna be an integer

2:38:30 and it's gonna be the secret number So I'm just gonna call it secret

2:38:33 number and why don't we just set it equal to five the next

2:38:37 variable We want to create is gonna store the user's guess so this is

2:38:41 also gonna be an integer and I'm just gonna call it guess

2:38:44 and I'm not gonna give it a value yet because Right off the bat.

2:38:48 The user wouldn't have guessed a number So what we want to do is we want

2:38:52 to be able to prompt the user to enter in the number until they get it Right.

2:38:57 So as long as the user hasn't entered in the correct

2:39:00 number We want to keep asking them to enter it in in order to do that in order

2:39:04 to like keep asking them to Enter it until they've

2:39:07 guessed it and we can use something called a while

2:39:09 loop So I'm just gonna type while and when making

2:39:12 open and close parentheses and an open and closed curly

2:39:15 brackets Let's think about a good condition for our game,

2:39:19 basically We want to keep asking the user to enter

2:39:22 an input as long as they haven't guessed the secret number

2:39:26 So it's right there we could keep looping as long

2:39:29 as the guess is not equal to The secret number right?

2:39:34 and basically this is always going to be true until They

2:39:38 guess the correct number now inside of here we want to prompt

2:39:42 them to enter their guests and then we want to take

2:39:45 whatever they enter and Store it inside of our guest variable.

2:39:48 So let's do this first we'll say printf and I'm

2:39:51 just gonna say enter a number and Then down here.

2:39:57 I'm gonna use Scanf in order to get whatever number they enter and I'm

2:40:03 gonna store it inside of that guest variable so I'm just gonna say at guess

2:40:07 and this is gonna take whatever number Or whatever integer they enter and it's

2:40:12 gonna store it inside of our guest

2:40:14 variable So basically what's happening in this loop

2:40:16 is every time through the loop we're getting a different number and then

2:40:20 we're gonna come up here and we're gonna check to see if it's equal

2:40:23 to the secret number if It's not then we're gonna go through again and we're

2:40:26 gonna keep doing this But if the guest is equal to the secret number,

2:40:30 then we'll break out of the loop and that means we'll be down here So I'm just

2:40:34 gonna print out a success message and I'll

2:40:36 basically just say like hey you win All right,

2:40:40 awesome so you can see this isn't actually that many lines of code I mean,

2:40:44 it's probably like ten lines of code to write out this whole thing

2:40:48 and it looks like everything's set up So this should actually work properly.

2:40:51 So let's try to run this and we'll see if we can guess the secret number So

2:40:54 remember a secret number is five So I'm

2:40:58 gonna run the program it says enter a number.

2:41:00 So let's just kind of botch the guesses

2:41:02 two Seven eight nine three so you'll notice

2:41:07 I can keep entering in numbers and It'll

2:41:10 keep prompting me to enter a number as long

2:41:13 as I haven't guessed correctly So now let's

2:41:15 type in five And remember five is the secret

2:41:18 number when I click enter now that loop

2:41:20 condition is going to end up being false, right?

2:41:23 Because the guess is gonna be equal to the secret number

2:41:26 and it's gonna execute and it's gonna say hey you win, right?

2:41:30 So we broke out of that loop we executed the print

2:41:32 statement that said you win and the program Terminated and that is

2:41:36 a really cool little game so one of the cool

2:41:38 things about this game and It's different from programs that we've

2:41:41 written in this course in the past is it'll keep running

2:41:45 Essentially infinitely until we enter in that correct guessed So it's

2:41:48 gonna keep going and keep going keep asking us to do

2:41:51 something and to enter in a value Until we entered correctly.

2:41:54 So this is pretty cool now This is a pretty nice

2:41:58 game and it's it's pretty simple But I want to maybe make

2:42:00 it a little more complex one of the things that sucks about

2:42:03 our game right now Is that the user gets unlimited guesses, right?

2:42:08 So basically like they can just guess every

2:42:10 single number and eventually they're gonna get it,

2:42:12 right, you know So why don't we do this?

2:42:15 We should impose a guess limit So why don't we say like the user can only guess

2:42:19 three times and if they can't get the secret

2:42:21 number in three tries Then they'll lose the game.

2:42:24 So let's think about how we can go

2:42:26 ahead and add that functionality Into this little app,

2:42:29 I'm gonna walk you guys through how we can

2:42:31 do that The first thing we're gonna need to do

2:42:33 is we're gonna need to create some variables I'm

2:42:36 going to create another variable up here and I'm

2:42:39 just gonna call it guess count and Guess count

2:42:42 is basically going to tell us how many times

2:42:44 the user has tried to guess the secret number

2:42:47 and I'm just gonna set this equal to zero because Initially,

2:42:51 the user will have guessed zero times so remember this guest

2:42:55 count variable is basically gonna get incremented every time they guess Right,

2:42:59 so I'm gonna come down here.

2:43:00 Every time we go through this loop.

2:43:02 The user is guessing So every time we go through this loop,

2:43:06 I'm gonna increment the guest count.

2:43:07 So I'm just gonna say Guest count plus plus and that means

2:43:11 every time the user guesses The guest count will get incremented

2:43:15 and we'll have a accurate count of how many times the user

2:43:18 has guessed All right the second variable that I want to make

2:43:22 is Going to be another integer and I'm just gonna call

2:43:25 it guess limit and this is gonna tell us how many times

2:43:29 the user Can guess so this will basically determine like how many

2:43:33 guesses the user gets So why don't we set this equal to three?

2:43:36 I think three guesses is a pretty good limit and finally

2:43:39 we want to create one more variable and this is also

2:43:42 gonna be an integer and I'm just gonna call this out

2:43:45 of guesses and I'm gonna set out of guesses equal to zero

2:43:50 and out of guesses will basically Tell us whether or not

2:43:54 the user has any more guesses and this might not be

2:43:56 super clear what this is doing right now But keep following

2:44:00 with the video and you'll see how this comes into play.

2:44:02 This is actually gonna be really important So we're actually gonna

2:44:05 have to modify our code a little bit so down here we

2:44:08 have this set up So the user can keep guessing as long

2:44:11 as the guess isn't equal to the secret number But we want

2:44:14 to be able to limit the amount of times that the user

2:44:17 can guess and actually down here I'm going to create an if

2:44:19 statement and every time we go through this loop I want

2:44:22 to check and see if the user has any more guesses left.

2:44:26 I want to in other words I want to see if they've reached their guessed limit

2:44:30 so I'm gonna make an if statement and I'm gonna check to see if the guest

2:44:34 count is less than the guests limit as Long as the guest count is less than

2:44:41 the guests limit That means they haven't guessed

2:44:44 as many times as the limit specifies Basically, they can guess again,

2:44:48 right so down here inside of this if statement I'm gonna put all of this code

2:44:52 and I'm just gonna surround this with curly brackets

2:44:55 so basically the user can only guess the number

2:44:59 this code will only get executed when the Guest

2:45:03 count is less than the guessed limit when

2:45:05 the user has Additional guesses right when they haven't

2:45:09 guessed as many times as the limit specifies Otherwise though,

2:45:13 so if they have guessed too many times Right

2:45:17 if the guest count is either equal to or greater

2:45:19 than the guessed limit I'm actually gonna set

2:45:21 this out of guesses variable equal to one so I'm

2:45:24 gonna say out of guesses is equal to one

2:45:28 and Basically what this is going to tell us is

2:45:31 if out of guesses is greater than zero Then

2:45:34 that'll tell us that the user is out of guesses.

2:45:37 So Essentially if out of guesses has a value that's more than zero

2:45:40 we'll know that they've run out of guesses and If you ever use

2:45:44 any other programming languages before this is what we would refer to as like

2:45:48 a boolean variable It's basically storing like a true or a false value,

2:45:52 which we're representing with zero or one So

2:45:56 if the guest counts less than the guessed

2:45:58 limit they can guess again Otherwise we're gonna

2:46:01 say out of guesses is equal to one.

2:46:03 In other words.

2:46:04 We're gonna say that they're out of guesses There's

2:46:07 one more thing we have to do though is we

2:46:08 have to come up here and we have to change

2:46:10 this Condition because right now we're gonna keep looping through

2:46:13 this code as long as the guest is not

2:46:16 equal to the secret number But there's actually two situations

2:46:19 where we want to break out of the loop

2:46:21 The first is when they've guessed the number correctly Right, then.

2:46:25 We want to come down here and print out you win But we also

2:46:28 want to break out of the loop when they've run out of guesses So I

2:46:32 want to check to see whether or not they're out of guesses so I'm

2:46:35 gonna say guess is not equal to secret number and out of guesses is equal

2:46:42 to Z So as long as out of guesses is equal to zero That means

2:46:47 that they're not out of guesses and they still have some guesses left But if

2:46:51 the guest counts less than the guessed limit and out of guesses gets set

2:46:56 equal to one Then out of guesses isn't going to be equal to zero anymore.

2:47:00 So we're gonna break out of the loop So basically,

2:47:03 there's two scenarios now that will break us out of the loop

2:47:06 either They get the word right or they run out of guesses So

2:47:09 one more thing we have to do in this program is come

2:47:12 down here and you'll see down here We're always printing out you win.

2:47:15 So no matter what like no matter what happens We're

2:47:19 printing out you win and that's because before if they

2:47:22 made it down here if they made it out

2:47:24 of that loop That means that they guessed the secret number correctly

2:47:28 but There's actually two scenarios Down here where they would

2:47:31 have broken out of the loop where there are they're out

2:47:34 of guesses or they guess the secret word So I

2:47:36 want to actually check to see which one we're dealing with.

2:47:38 So I'm going to check to see if Of guesses

2:47:41 and I'm gonna check to see if it's equal

2:47:44 to one and if it's equal to one That means

2:47:47 that they lost and they ran out of guesses.

2:47:49 So I'm just gonna say of guesses otherwise though,

2:47:54 it means that they won so I'm gonna say You win and this will basically be

2:48:00 like hey you won the game awesome So this is a lot of code I know

2:48:06 that and let's just run this see how we did And we'll see if our games working

2:48:11 properly and then I'll kind of explain all

2:48:13 the code to you guys And actually you know what?

2:48:16 I just noticed the typo down here.

2:48:18 I said out of guesses double equals one This is just have to be a single equals.

2:48:22 So we're assigning this of value.

2:48:24 So yeah, that was a little typo I had two equals that needs to be one.

2:48:28 So let's go over here We'll run our program and it says enter a number.

2:48:31 So why don't we try to lose the game?

2:48:33 I'm just gonna say two two and now we're on our third and final guess if

2:48:37 I don't get it this time That means we're gonna lose the game So I'm gonna enter

2:48:42 and you'll see it says out of guesses So we weren't able to guess it within

2:48:46 our three tries and the game told us that we lost Let's run it one more time.

2:48:50 We'll try to win so I'll say like two one now We're on our third and final

2:48:54 guess So if I don't get it this time we're going to I'm gonna guess of five

2:48:57 and we guessed right so it's gonna tell

2:48:59 us that we want so that's sort of Basically

2:49:01 how this little program works and I'm gonna walk

2:49:03 you guys through one more time so we have

2:49:05 all these variables up here secret number is Just

2:49:08 storing the number they need to guess the guest

2:49:10 keeps track of what they're guessing each time

2:49:13 The guest count is gonna tell us how many

2:49:15 times they've guessed the number guess limit is

2:49:17 gonna tell us how many times they can guess

2:49:20 and Out of guesses is going to tell us

2:49:23 whether or not the user is out of guesses.

2:49:25 So down here We're checking two conditions for this while loop.

2:49:28 We're checking to see if the guess is not

2:49:30 equal to the secret number and We're gonna keep

2:49:33 looping as long as out of guesses is equal

2:49:35 to zero every time we go through this loop

2:49:37 We check to see if the guest count is less than the guessed limit If it is

2:49:41 then we ask them to guess we increment

2:49:43 the guest count Otherwise we say out of guesses is

2:49:46 equal to one and that's gonna break us

2:49:49 out of the loop down here There's two situations

2:49:51 the first situation is that they ran out of guesses

2:49:54 the second situation is that they won the game

2:49:56 and We're using this if statement to check

2:49:58 both of those so that's basically how this works

2:50:01 and this kind of shows you how we can

2:50:03 use things like while loops and if statements and also,

2:50:07 variables together in like one single program In this tutorial I'm going

2:50:15 to teach you guys how to use for loops in C now

2:50:18 a for loop is a special type of loop that we can

2:50:21 use in C which allows us to use something called an indexing variable

2:50:25 and this indexing variable will basically tell us what iteration of the loop

2:50:30 we're currently on and we can use that indexing variable to do

2:50:33 a bunch of stuff like we could Loop through an array

2:50:36 of items we could do all sorts of stuff inside of a loop.

2:50:40 So down here in my program You'll see that I have a while loop Set up and I

2:50:45 have just sort of a basic while loop

2:50:47 and I want to show you guys what it's doing.

2:50:49 So Essentially up here I'm saying int I is equal to 1 so I'm

2:50:53 giving this integer I the value of 1 and I'm saying wow I is

2:50:57 less than or equal to 5 I'm gonna print out the value of I

2:51:01 and then I'm gonna increment I so this is a very simple wire loop basically,

2:51:05 we have this variable I and every time we go through the loop,

2:51:08 we're printing it out until it's Greater than 5 so let's run

2:51:12 this program and we'll see what it does So you'll see over here.

2:51:17 We're basically printing out values between 1 and 5 and I

2:51:20 want to point out what's actually happening so the first

2:51:23 time that we go through this while loop we're printing out

2:51:26 1 The second time we're printing out to the third time.

2:51:30 We're printing out 3 in other words this variable I

2:51:33 is Basically telling us what iteration of the loop are currently

2:51:37 on so on the first iteration of the loop I is

2:51:40 telling us that we're on the first iteration of the loop,

2:51:42 right the first time we go through the loop I is equal to 1

2:51:45 Second time we go through the loop I is equal to 2 third time

2:51:48 I is equal to 3 So this variable I over here is basically telling

2:51:53 us how many times we've gone through the loop So on the third time,

2:51:57 it's telling us 3 fourth time for etc,

2:51:59 and believe it or not This is actually a very useful thing for us to have

2:52:04 when we're looping It's there's a lot of situations

2:52:06 where you're gonna want to know what iteration

2:52:09 of the loop you're currently on when you're

2:52:12 looping so with a normal while loop like

2:52:14 I can basically just specify a condition up

2:52:16 here and I can do whatever I Want right?

2:52:18 It's very open You can kind of just do whatever you want

2:52:20 with it but in a lot of situations and there's tons of these situations

2:52:25 we're going to have a variable just like I That will tell

2:52:29 us what iteration of the loop that we're currently on or you know

2:52:32 Basically just a variable that's gonna keep changing every time we go through

2:52:35 the loop and this is such a common situation in see that there's

2:52:40 actually something called a for loop and a for loop allows us

2:52:43 to take all of this code and Condense it into its own single loop.

2:52:48 So it's taking this situation where we want to have a variable

2:52:52 like I and It's basically allowing us to do it a lot easier

2:52:56 and a lot cleaner So I'm gonna show you guys how we

2:52:58 can create a for loop I'm gonna come down here below this while

2:53:01 loop and I'm gonna create a basic for loop structure So I'm

2:53:04 just gonna say for I'm gonna make an open and closed parenthesis

2:53:07 and an open and closed curly bracket now the difference between a for loop

2:53:11 in a while loop is Basically gonna happen inside of these parentheses.

2:53:15 So in the parentheses of a while loop,

2:53:17 we have our loop condition or our loop guard Right.

2:53:20 This is specifying whether or not we can keep looping

2:53:24 through inside this for loop We're gonna have three different things.

2:53:27 So instead of just one loop guard We're actually gonna have

2:53:30 three different things that we want to put inside of here.

2:53:33 So with a for loop the first thing I'm gonna put in here is This variable I so

2:53:38 you'll notice in the while loop we have our variable

2:53:41 I up here and this is basically Allowing us

2:53:44 to loop through and keep track of how many

2:53:46 times we've gone through the loop So what I can

2:53:49 do down here is I can do something similar I

2:53:51 could say like int I and I'm not gonna give

2:53:53 this a value inside of here I'm gonna say

2:53:55 I is equal to 1 so basically I'm gonna take

2:53:58 my variable I and I'm gonna give it an initial

2:54:01 value Of 1 and now I have my I variable

2:54:04 I have my variable that's gonna change every time

2:54:06 we go through the loop The next thing I want

2:54:10 to do is include my looping condition so up here

2:54:13 We have I as less than or equal to 5, that's our condition.

2:54:16 I can put the same thing down here So I'm gonna say I is less than or equal to 5

2:54:21 The third thing I want to do is increment I so

2:54:24 you'll notice down here Every time we go through this loop,

2:54:27 we're incrementing that variable.

2:54:28 I I'm gonna do the same thing over here in this little

2:54:32 third quadrant So you'll notice I have these little like sections.

2:54:36 Here's the first section We're initializing the very I were saying

2:54:40 I is equal to one Here we're specifying our looping conditions so

2:54:45 i'm saying we're gonna keep looping while I is less than

2:54:47 or equal to five and Over here is a little line of code.

2:54:51 That's gonna get executed.

2:54:52 Every time we go through the loop So here

2:54:55 I'm saying I plus plus I could also say like

2:54:58 I minus minus and that would decrement I I could

2:55:01 say like I is equal to I plus two Etc.

2:55:04 Like I could do basically anything I wanted over here.

2:55:07 Let's just keep it at I plus plus for now so you'll

2:55:10 see I was basically able to take all of this code and condense

2:55:14 it into its own little type of loop and now Instead

2:55:17 of having to like print this out and create this variable up here.

2:55:20 I can do all of that Inside of this for loop now,

2:55:24 I can basically take this line of code I

2:55:27 can paste it down into here and we essentially

2:55:30 have the same thing So right now this block

2:55:33 of code and this block of code are 100% equivalent

2:55:37 They're doing exactly the same thing So let's go

2:55:40 ahead and get rid of all this code and let's

2:55:42 test out our shiny new for loop So I'm

2:55:45 just gonna run my program and you'll see over here.

2:55:47 We're getting exactly the same thing that we got

2:55:50 before We're printing out one two, three, four five.

2:55:53 So it's the same exact program doing the same exact

2:55:57 thing and that's why for loops are great because we can

2:56:00 take that little Structure like where we have an indexing

2:56:03 variable and we can use it with something like this so

2:56:06 I want to show you another situation where these four

2:56:08 loops can come in handy and We can use them

2:56:11 to loop through all the elements inside of an array

2:56:13 so actually up here I have this array that I created.

2:56:16 It's called lucky numbers and I'm just gonna grab this and bring it down here.

2:56:20 So We have this lucky numbers array.

2:56:23 It has 4 8 15 16 23 42 and What I could actually do is I could loop through

2:56:29 all of the elements inside of this array

2:56:32 From this for loop so I'm gonna show you guys how we

2:56:35 can do that now Let's first off see how many elements we have we have 1 2 3 4 5

2:56:39 6 so we have 6 elements in this an array

2:56:42 so what I'm gonna do is I'm actually gonna say

2:56:45 I is equal to 0 and you'll see why we

2:56:47 need to do this in a Second but basically array

2:56:50 indexes start at 0 So this first element in the array

2:56:53 is at index position zero and just for a little

2:56:56 refresher if I wanted to access one of these elements

2:56:59 from inside the array I could say like lucky

2:57:02 numbers Was zero and this is going to give me

2:57:05 access to this element If I said lucky numbers two,

2:57:09 then I'll get access to this element this 15 So this is basically how

2:57:13 we can access an element inside the array so I'm gonna set I equal

2:57:18 to zero and I'm gonna say I want to loop while I is less

2:57:22 than six and Six was how how many elements we had in the array?

2:57:26 So I want to keep looping as long as we're less

2:57:28 than six and I'm gonna say I plus plus Now down here.

2:57:31 I'm gonna do this same exact thing but instead of printing out I I'm gonna

2:57:36 print out lucky numbers I So I'm gonna

2:57:41 print out the array element at index position.

2:57:44 I and lucky numbers Basically the first time we

2:57:47 go through this loop We're gonna be printing out lucky

2:57:50 numbers zero Because I is going to be equal

2:57:53 to zero the second time we go through the loop

2:57:55 We're gonna be printing out lucky numbers one because

2:57:57 I is gonna be equal to one etc And we're

2:58:00 gonna keep doing that Until we get all the way

2:58:03 up to five which is gonna be the last element,

2:58:05 which is this 42 So let's go ahead and do that.

2:58:08 So I need to put this back to I and Let's run this program.

2:58:12 So you'll see over here We're basically doing exactly what I said.

2:58:16 So the first time through the loop were printing out four We're printing

2:58:19 out that first element the second time through the loop are printing out eight,

2:58:22 which is the second element 15 16 23

2:58:25 and 40- so we're printing out all of those elements

2:58:28 in turn as we go through this for loop

2:58:31 and There's a lot of situations where for loops are

2:58:34 gonna come in handy but this is a very

2:58:36 very very very common situation where we want to loop

2:58:39 through all the elements in an array and either

2:58:41 print them out or You know do something to them,

2:58:44 whatever so that's the basics of working

2:58:45 with for loops and I do just want to say

2:58:47 like Anything that you do with a for loop you could do with a while loop,

2:58:51 you know I basically showed you guys how we transform that while loop

2:58:53 into a for loop the thing with for loops though is that they're very Convenient

2:58:57 so it makes it really easy to do something like this without having

2:59:01 to go through all the trouble of creating a while loop In this tutorial I'm

2:59:09 gonna talk to you guys about two topics in C the first topic we're

2:59:13 to talk about our two-dimensional arrays

2:59:15 a Two-dimensional array is basically a situation where

2:59:18 we have an array where all of the elements in the array are actually

2:59:22 arrays themselves The second thing I want to talk to you guys about is nested.

2:59:27 Loops So we're gonna look at how we can use

2:59:29 a looping structure where we have a loop Inside of loops.

2:59:33 It's gonna be pretty cool and actually these two

2:59:35 topics can go together really well and I'm gonna

2:59:37 show you guys how we can use nested loops

2:59:40 and Two-dimensional arrays in order to make an awesome program.

2:59:44 So let's get started the first thing I

2:59:46 want to show you guys is two-dimensional arrays so down here we can create a 2d

2:59:50 array and actually the concepts that I'm showing you

2:59:53 in this video can apply to Multi-dimensional arrays

2:59:56 so not only two dimensions but three four

2:59:59 or five really as many dimensions as your heart

3:00:01 desires So in order to create a two-dimensional array,

3:00:04 I'm just gonna make an array of numbers So I'm

3:00:06 just gonna say int and we'll give this a name.

3:00:08 So why don't we just say nums and Normally when we create an array after

3:00:13 we'd say the name of the array we'd make an open and close square bracket

3:00:17 But when we create a two-dimensional array

3:00:19 we're gonna make two open and closed square

3:00:22 brackets just like that these two open

3:00:24 and closed square brackets will basically represent like

3:00:26 the Width and the height of our array so you guys will see what

3:00:30 I mean in a second but basically we're gonna have like Elements in the array

3:00:34 and then each of those elements is gonna be an array and we'll have elements

3:00:38 inside of it So these two squares will

3:00:39 allow us to like manipulate all that stuff.

3:00:41 So what I'm gonna do is I'm gonna set

3:00:44 this equal to An open and closed curly bracket now,

3:00:47 this is normally how we would create an array So

3:00:50 normally I would just say like 1 2 3 4 whatever

3:00:53 I could put all my numbers in here But with a two-dimensional

3:00:56 array all of the elements are gonna be arrays.

3:00:59 So instead of just saying like 1 2

3:01:01 3 We're actually gonna create Arrays inside of here.

3:01:07 So for example, I'll put an array right here and this text editor is

3:01:11 so annoying with making these so for example the first element in this array,

3:01:16 I could just make an array so I'm gonna say this is like 1 So you can see here.

3:01:21 I have two elements Inside of this array which

3:01:25 is itself the first element of the nums array.

3:01:28 So let's make another one We'll come down here

3:01:32 and we'll make another one and this keeps trying

3:01:34 to format differently So now we're gonna say three

3:01:38 four So this thing right here this whole array,

3:01:41 that's the first element in the nums array Right

3:01:45 and that array has two elements inside of it.

3:01:48 Same thing with this one This is the second element

3:01:51 in the numbers array and it has two elements inside of it.

3:01:53 So it's basically an array within an array let's

3:01:58 make one more and So this is going to be

3:02:02 five six So now we have three array elements

3:02:06 right one two Three and each of them has

3:02:11 two elements inside of them So whenever we create

3:02:14 a two dimensional array like this We always have

3:02:17 to specify the number of elements and then the number

3:02:20 of elements inside each array so in our case,

3:02:23 we're going to have one two three elements in the array

3:02:26 and Each array has two elements inside of it So it's gonna look

3:02:31 like that so we would say three and then two and that's

3:02:35 basically how we created So now let's talk about accessing these elements.

3:02:38 So I'm gonna create a little print statement here

3:02:41 and We're gonna print out some of this stuff.

3:02:44 So I'm gonna Say% D and I'm gonna show you guys how we can access individual

3:02:49 elements So basically I'm gonna say nums and let's

3:02:52 say that I wanted to access this top-left

3:02:54 element this one the first thing I want

3:02:57 to do is specify the index where The value that I want to access is stored So

3:03:03 like this would be index position 0 this would

3:03:05 be index position 1 this would be index position 2 so we're gonna say 0 and then

3:03:10 I want to specify the index position

3:03:12 of the individual element inside of 0 so I could

3:03:15 say like this is element 0 this is element 1 so why don't we access element 0

3:03:21 So I print out nums 0 0 and this is gonna print out that Numbers coming out.

3:03:26 It's gonna print out that one for us.

3:03:27 You can see over here we get that one So let's try a different one.

3:03:31 Let's try to grab this for right here.

3:03:33 So this is going to be an index position One and then one so this would

3:03:39 be at 1 1 and now we should get that for let's see Yeah, cool.

3:03:44 So we get the 4 All right so that's basically

3:03:47 how we can access elements inside of these arrays and also

3:03:49 just want to point out that if I didn't want

3:03:52 to give this an initial value I could just like put

3:03:55 a semicolon here and I could just like Manually define

3:03:58 each index location so I could say like 0 0 is

3:04:01 equal to 7 or something like I don't have to give

3:04:05 it a Value like right up front although in our case.

3:04:08 Let's just do that because it's a lot easier All right,

3:04:11 so we have our numbers array and we figured out

3:04:13 how we can print out the elements So now I

3:04:16 want to talk to you guys about another Concept which

3:04:18 is called a nested for loop and you guys will see

3:04:21 in a second why I'm teaching this alongside 2d arrays

3:04:24 but a nested for loop is a situation where we have

3:04:27 a for loop and inside of that for loop we have

3:04:31 Another loop so I'm gonna show you guys this really quick.

3:04:33 Let's say we create a for loop I'm gonna create two variables over here

3:04:37 in I and int J and I don't know if I showed you guys

3:04:41 this in the course yet But if I want to just like declare two

3:04:45 variables I can just say I comma J and that will declare both the variables.

3:04:50 I'm not giving them actual values yet We're gonna do

3:04:53 that inside these four loops So I want to show you guys

3:04:56 how we can use a nested for loop in order

3:04:58 to print out all of the elements inside of this two-dimensional array,

3:05:01 so I'm gonna say for I Is equal to zero?

3:05:06 and we're gonna keep looping as long as I is less than Three

3:05:10 and the reason I'm saying three here

3:05:12 is because that's how many elements are inside

3:05:15 of this nums array so I'm gonna keep looping as long as I is less

3:05:20 than three and then I'm gonna say I

3:05:22 plus plus Now inside of these curly brackets, I want to create another loop,

3:05:28 so every time we go through this one iteration of this top loop,

3:05:32 we're gonna fully execute through another loop so I'm gonna say,

3:05:35 for J is equal to 0 J is less than 2 and the reason I'm saying 2 here is because

3:05:44 that's how many elements Are inside each array Inside

3:05:48 of the nums array and you guys will see in a second.

3:05:50 Why?

3:05:51 this is gonna work and then I'm gonna say J plus plus so

3:05:55 now I'm gonna make some more open and close curly brackets and Down here.

3:05:59 I'm gonna actually be able to print out all of the elements

3:06:03 inside of this array So I'm gonna go ahead and do

3:06:06 that I'm just gonna say printf and we're gonna be printing out

3:06:10 an integer because these are all integers and we're gonna be printing

3:06:14 out nums and I'm gonna print out nums I and J So

3:06:21 whatever the value of I is and whatever the value of J

3:06:24 is that's the index position inside of nums that we're gonna print

3:06:28 out and now just so this is Formatted a little bit better.

3:06:32 Why don't we put a comma?

3:06:35 Right there and then I'm also gonna put another printf over

3:06:38 here that's just gonna print out a new line And you'll

3:06:42 notice that this printf is outside of this inner for loop

3:06:46 so I'm gonna run this program We're gonna see what it does.

3:06:49 You guys will see exactly what's happening and then I'll sort

3:06:52 of walk you through a little bit more What is going on, so?

3:06:57 Let's run this program And you'll see over here when we

3:07:01 ran the program We're basically getting this entire 2d array printed out.

3:07:05 So I'm printed out 1 2 3 4 5 6 So let's

3:07:10 talk about why that happens Over here I'm saying for I is

3:07:14 equal to 0 I is less than 3 I plus plus So

3:07:17 I'm going through this particular four loop three times I'm gonna execute all

3:07:22 the code inside of this four loop right here Three times that's

3:07:27 as many times as there are elements inside of our 2d array Now

3:07:33 every single time I go through this top loop every single time

3:07:37 I go through it I'm going to execute this loop in its entirety.

3:07:42 So I will loop through this loop all the way through I'll

3:07:46 go through all of its iterations and this loop says J is

3:07:49 equal to 0 J is less than 2 and 2 over here

3:07:53 is how many elements are inside of each one of these raise?

3:07:57 So this element in the noms array has one two elements inside of it

3:08:02 This element in the numbers array has one two elements inside of it, right?

3:08:06 That's where this two is coming from Then I'm incrementing J.

3:08:10 And I'm gonna print out so I'm saying print F and I'm printing

3:08:14 out nums at index position I and index position J so the first time

3:08:20 we go through this for loop I is going to be equal to zero

3:08:24 and Remember that first time we're actually going to be going through this loop

3:08:28 two times So we're going to be printing out nums 0 0 and nums

3:08:33 0 1 and then we're coming down here and printing a new line

3:08:37 the next time we go through this I loop we're gonna come down here

3:08:41 and print nums 1 0 and nums 1 1 Finally the third and final time.

3:08:47 We're gonna print out nums to 0 and nums

3:08:50 to 1 That's basically how this is working.

3:08:53 So two-dimensional arrays and Nested for loops are a match

3:08:57 made in heaven and there's a lot of situations Where you

3:08:59 know besides just looping through 2d arrays that we're going

3:09:02 to use nested loops But hopefully this gives you a little bit

3:09:05 of a introduction into what they are and why they're useful

3:09:13 In this tutorial I'm gonna talk to you guys about accessing

3:09:16 memory addresses in C Now in the C programming language

3:09:20 a lot of times we're gonna want to store different information Right,

3:09:24 and there's a bunch of different ways that we can store information We

3:09:27 can use things like variables we could use arrays we could use structs

3:09:30 but the basic point is that whenever we're using c we're gonna want

3:09:34 to keep track of and Maintain a bunch of different pieces of information

3:09:38 and one of the easiest ways to do that is by creating Variables

3:09:43 so down here you'll notice that I have a bunch of variables I have

3:09:46 this integer called age and it's just has the value of 30 We

3:09:49 have this double GPA 3.4 and we have this character grade and it has

3:09:54 the value of the a character I have a bunch of different variables

3:09:57 in my program and it allows me to keep track of data really easily

3:10:01 and I can use these to You know maintain and I can even

3:10:06 modify these different informations variables are great

3:10:08 But I want to talk to you

3:10:10 guys a little bit about how these work in the actual physical memory

3:10:14 of our computer So whenever I create a variable for example when I

3:10:18 create a variable called age And I give it a value of 30

3:10:22 this value 30 actually gets stored on our computer so all computers have Memory,

3:10:28 so a lot of times you'll hear people refer

3:10:31 to this as RAM It's called random access memory

3:10:33 and basically RAM is the memory that your computer is

3:10:37 going to use when it's running programs So for example,

3:10:40 if I was to run this C program my computer would use Ram It would

3:10:45 use that memory in order to store

3:10:47 and keep track of all this different information Right.

3:10:50 So when I create a variable like int age C is actually

3:10:54 gonna store this value 30 at a specific memory Location so it's

3:11:00 gonna take that value 30 and it's gonna store it somewhere

3:11:04 in RAM right when I create this double called GPA C's gonna take

3:11:08 this value 3.4 and it's gonna store it inside of the physical

3:11:12 memory on our computer when I create this grade C is gonna

3:11:16 store this character on the physical memory in our computer Now here's

3:11:20 the thing when I create these variables I give them descriptive names, right?

3:11:23 So I gave this a descriptive name So I know what's stored inside of it, right?

3:11:28 And when I want to access this value, I can just refer to the name, right?

3:11:32 I can just refer to age I could modify and I

3:11:35 could print it out I could do something else to it right

3:11:37 I can do whatever I want with it and the way

3:11:40 that I can access this variable in other words the way that I

3:11:44 can access the value 30 is Just by referring to the name

3:11:47 of the variable same goes down here if I wanted to access

3:11:50 the grade Right if I wanted to access this capital a I

3:11:54 could just refer to the name of the variable But again,

3:11:57 here's the thing All of this information is stored

3:12:01 in our physical memory somewhere And so whenever C

3:12:04 needs to access that information whenever our computer needs

3:12:08 to access this value 30 or this value 3.4.

3:12:12 It's actually going to refer to a specific memory address Right.

3:12:17 So when I want to access this variable age,

3:12:19 I can just type out age and then I can use

3:12:22 this variable But when C wants to refer to this value,

3:12:25 it's not actually using age It's gonna use the memory address

3:12:29 where this value is stored inside of our physical memory So all

3:12:34 of these values are stored inside of a physical address in our memory

3:12:39 So all of these values have an address where they're stored in memory

3:12:42 and when C needs to access them it can use that address

3:12:46 So I'm gonna show you guys how we can access that address how

3:12:49 we can print out the address on to the screen And basically

3:12:52 just wanted to give you guys an introduction into what memory addresses were.

3:12:56 So down here I'm gonna say printf and I'm gonna show you guys how I can actually

3:13:02 print out the physical memory address Where each

3:13:04 one of these values is stored on our computer?

3:13:08 So down here if I want to print out a memory address.

3:13:11 I need to use a% and a P so normally if I was

3:13:15 just gonna print out like a number I could say like% D or I

3:13:18 can say% C for a Character when we want to print out

3:13:22 a physical memory address We're gonna say%

3:13:24 P and this actually stands for pointer.

3:13:27 We're not gonna talk about pointers in this tutorial We're just gonna talk about

3:13:31 memory addresses But just know that you need to say% P and now I'm

3:13:35 going to type out the name of the variable whose memory I want

3:13:38 to access so I'm gonna access the memory address of age and Before the variable.

3:13:43 I'm just gonna say ampersand so I'm gonna make

3:13:47 an ampersand age and I'm gonna say percent P and Now

3:13:51 we should be able to print out the memory

3:13:54 address where the variable age is stored In other words,

3:13:58 we can print out the memory address where this value 30 is stored.

3:14:01 So let's go ahead and run our program and You'll see over here.

3:14:05 We're getting this number So I'm getting this number 0 0 6 0 FF 2 0

3:14:10 0 so this would be like a hexadecimal

3:14:12 number I guess it's not technically a number.

3:14:14 It's like I think it's hexadecimal But basically this is

3:14:17 the physical memory address where the value of 30

3:14:21 is stored in other words It's the place where C

3:14:24 stored the value inside of this age variable I could

3:14:27 do the same thing for these other variables so

3:14:29 actually why don't I format this a little bit I'm

3:14:32 gonna stay here age and then I'm gonna make

3:14:34 a new line and we're just gonna say GPA and again,

3:14:38 I'm gonna print out another one of these addresses

3:14:41 and we'll do one for grade and We'll

3:14:45 print out the address so now I can just come over here and I can sort of modify

3:14:51 this a little bit so I can just say GPA and Grade and you'll notice for each

3:14:57 of these I'm using this ampersand before I type

3:15:00 out the name of the variable So now when

3:15:02 we run our program we should get a nice little list of all of these variables

3:15:07 and their corresponding addresses so you'll see over here

3:15:10 we have age and it's stored up memory address 0

3:15:14 0 6 0 FF OC GPA is stored at this memory address and grade is stored

3:15:20 at this memory address so if you were to like

3:15:23 go into our computer or go into my computer

3:15:25 and Go to memory address is 0 0 6 0 FF 0 C that's actually going

3:15:32 to store the value of 30 it's gonna store

3:15:35 the value that's inside of that age variable If

3:15:38 you were to go to this memory address you would see the value Inside of GPA if

3:15:42 you're going to this memory address you would see

3:15:44 the letter which was a capital A so basically

3:15:47 all of these variables are store at Different

3:15:51 memory addresses when I want to access the information

3:15:54 in the variable I can just refer to the variables name so I could say a age

3:15:59 or I could say GPA But when C wants

3:16:02 to access the information and the variable it has

3:16:05 to use these Addresses so C would use

3:16:08 this address C would use this address and this address?

3:16:11 I can just use the variable names and that's why C is awesome because

3:16:15 it allows us to do stuff like that But when C and when our computer

3:16:20 wants to access these values it has to use these memory addresses and so that's

3:16:25 why these are useful and When we're programming and C it can actually be pretty

3:16:29 useful to be able to know the Addresses like the actual physical addresses

3:16:34 of these certain variables in memory So a lot of times in C when we want

3:16:39 to work with these Variables we can just refer to them by name and we

3:16:42 can do all sorts of stuff with them We can access them we can modify them.

3:16:46 But in other circumstances,

3:16:47 we're not just gonna want to be able to access the value We're

3:16:51 also gonna want to be able to access their physical memory address in other

3:16:55 words we're gonna want to be able to know like what that address is

3:16:58 and we can use that for a bunch of stuff and In future tutorials,

3:17:02 I'm gonna talk about why that can be useful,

3:17:04 but for now I really just wanted to kind of give you

3:17:07 guys an overview of what memory addresses are How we're using them

3:17:10 in our program and how we can print them out so again I

3:17:13 just use this ampersand and then I type the name of the variable

3:17:16 and I use this percent P to print it out In this tutorial

3:17:24 I want to talk to you guys about pointers in C Now

3:17:28 a pointer is basically just a type of data that we can

3:17:32 use inside of our programs So up until this point in the course,

3:17:36 we've been using a bunch of different types of data, right?

3:17:39 we've been using things like integers

3:17:40 and an integer is just a whole number we've

3:17:43 been using things like doubles and a double

3:17:46 is a decimal number we've been using Chars

3:17:49 and a char is basically just a character and now I want to introduce you guys

3:17:54 to another type of data which is called

3:17:56 a pointer and a pointer is basically just

3:17:59 a memory address and a memory address it

3:18:01 basically refers to a you know a physical

3:18:04 address inside of the memory of our computer

3:18:07 where we're Storing a value right and I

3:18:10 think pointers Tend to confuse a lot of people who learn about them and this is

3:18:15 sort of one of those topics and see that everybody dreads Because it's you know,

3:18:20 traditionally very confusing to understand

3:18:22 but actually pointers are Extremely simple

3:18:24 and the problem is that they just get over complicated So what I

3:18:28 want you guys to do is I just want you guys to think

3:18:31 of a pointer as a type of data That's all it is.

3:18:34 It's just a type of data that we can work with in our programs

3:18:37 It doesn't have to be any more complicated than that, right?

3:18:40 I can work with an integer in my program an integer is a whole number, right?

3:18:44 I can work with a double in my program a double is

3:18:47 a decimal number I can work with a char in my program

3:18:50 a char is just a character I can work with a pointer

3:18:53 in my program a pointer is just a memory address That's it.

3:18:57 A pointer is just another type of data that I

3:18:59 can use and I can work with Inside of my programs

3:19:03 and it just happens to be a memory address inside

3:19:06 of the memory or the RAM in our computers That's that's

3:19:09 all it is It's very simple people overcomplicate pointers and they

3:19:12 don't have to be over complicated because it's just another

3:19:15 type of data Just like an integer or a double

3:19:18 but instead of being like a whole number or a decimal number.

3:19:21 It's a memory address So hopefully that makes sense and I

3:19:24 hope you think you guys don't Try to over complicate this too

3:19:27 much because it doesn't have to be so down here in my program

3:19:30 I have an integer that I've created and it's called age.

3:19:33 So I just said in age and Inside of this integer.

3:19:37 I'm storing a whole number I'm storing an integer right third And if you've

3:19:41 been following along with this course

3:19:42 in the last tutorial We talked about memory addresses

3:19:45 and we talked about how all of these variables are actually storing these values

3:19:50 at Physical addresses on our computer's memory

3:19:53 so down here I have this print statement

3:19:56 and I'm just printing out ages memory address and I'm using this percent P

3:20:00 and I'm just printing out Ampersand age

3:20:03 and we talked about how this ampersand when I

3:20:06 put this in front of the variable basically It's going to give me the physical

3:20:11 address in memory where this variable in other

3:20:13 words where this Thermia value is stored.

3:20:16 So if I run this program you guys will see over here.

3:20:19 It says ages memory address and it's this Hexadecimal number right?

3:20:23 This is the physical memory address of eight the age variable What

3:20:28 did we just talk about before remember what I told you pointers are?

3:20:33 Memory addresses that's what they are it's a type

3:20:35 of data a pointer is a type of data

3:20:37 in our program and It's a memory address right

3:20:39 when I use this ampersand and I type out

3:20:42 the name of a variable This is giving me

3:20:44 the memory address right I'm able to print out

3:20:47 the memory address so really this is a pointer

3:20:51 This is a type of data in our program.

3:20:53 That is a memory address just like an integer is a type

3:20:56 of data That's a whole number and a double is a type of data.

3:20:59 That's a decimal number and a char is a type

3:21:01 of data That's a character a pointer is a type of data.

3:21:05 That's a memory address and this right here This guy right here is a pointer.

3:21:10 So it's just the memory address of the age Variable that's

3:21:15 it and when I want to print out a pointer I

3:21:18 can use this percent P and It'll allow me to print

3:21:20 it out onto the screen and everyone's happy So essentially what

3:21:23 I'm doing here is I'm printing out a pointer when I

3:21:25 say percent P I'm telling this print F function that I

3:21:28 want to put on a pointer and then I give it

3:21:31 a pointer over here I give It a physical memory address.

3:21:34 That's what a pointer is now We understand and hopefully

3:21:36 understand that a pointer is just a type of data, right?

3:21:39 It's just a memory address and We can access

3:21:43 the memory addresses of specific variables by using this ampersand

3:21:47 and then type The name of the variable and that can

3:21:50 be pretty useful Now that we understand that I

3:21:53 want to show you guys how we can work

3:21:55 with these pointers so over here I have an integer

3:21:58 variable called age and this integer variable is storing

3:22:02 an integer value 30 I'm gonna say that one more time.

3:22:07 I have an integer variable and this integer variable

3:22:10 is storing an integer value 30 I could also

3:22:13 create a double variable so I could create

3:22:16 a double called GPA and Inside of this double variable.

3:22:20 I could store a double value like 3.4.

3:22:23 I could also create a char variable

3:22:26 Called grade and inside of this char variable.

3:22:29 I can store a character a I could also create a pointer variable and Inside

3:22:36 of that pointer variable I could store a pointer Okay So just like I

3:22:42 stored a character inside of this character

3:22:44 variable and I stored a double inside of this double variable I could store

3:22:49 a pointer inside of a pointer variable so we

3:22:52 can actually store these pointers inside

3:22:54 of a pointer variable Now here's where this is

3:22:57 going to get a little bit tricky When we create a pointer variable we actually

3:23:03 need a physical memory address right so

3:23:06 when I create this Integer variable like I

3:23:08 can just come up with a number right off the top of my head, right?

3:23:11 I can just say like 30 when I create a Double variable I can

3:23:16 just store whatever number I want to store but when we create a pointer

3:23:20 variable remember We're gonna be storing

3:23:22 a memory address and I don't necessarily

3:23:24 Know any memory addresses like right off the top of my head at least.

3:23:28 I don't know any meaningful memory addresses

3:23:30 So when I create a pointer variable, what I want to do is store the memory

3:23:35 address of a variable That's already in a program.

3:23:39 So I'm gonna say that one more time when I'm creating a pointer variable inside

3:23:45 of that pointer variable I'm gonna store

3:23:48 the memory address of a another variable inside

3:23:51 of our program so let's say that I wanted to create a pointer that would store

3:23:55 the memory address of This variable over here

3:23:58 so thats or the memory adjusts of age, right?

3:24:01 I can create a pointer just like this so I could say int

3:24:05 and asterisks and now I'm gonna give this a name so I'm gonna give

3:24:09 this Pointer variable a name and remember this pointer variable is gonna store

3:24:14 the memory address of the age variable

3:24:17 and generally when you're naming a pointer,

3:24:19 you're gonna start with a lowercase P and Then you're gonna type the name

3:24:25 of the variable whose memory address you're storing

3:24:28 in the pointer variable So I'm gonna say

3:24:31 P age right because this pointer variable is

3:24:34 storing the physical memory address of the age

3:24:37 variable Now what I want to do is set this equal to the memory address

3:24:41 of the age variable Which we can access using this ampersand so I could say

3:24:46 ampersand age so now this pointer variable

3:24:49 is Storing the memory address of the age

3:24:53 variable I'm gonna do the same thing down here for this GPA So if I

3:24:59 wanted to create a pointer variable that would

3:25:01 store the memory address of the GPA variable.

3:25:04 I could say double asterisks and I'm just gonna say

3:25:08 P GPA just like that and I'm gonna set this equal

3:25:12 to the memory address of This double variable which we can

3:25:16 access using this ampersand and then the name of the variable

3:25:20 like that I'm gonna do the same thing down here

3:25:23 for this character variable So I want to create a pointer

3:25:26 variable which is going to store the memory address of The character

3:25:30 variable so I'm just gonna say char Asterix a grade,

3:25:36 and I'm gonna set this equal to Ampersand grade.

3:25:39 Okay, so that's all I'm doing, right?

3:25:42 So now I have this integer variable and I

3:25:45 have this pointer variable Which is storing the memory

3:25:49 location or the memory address of the age variable

3:25:53 I have this double variable which is storing a double

3:25:56 and I have this pointer variable which is storing

3:25:59 a pointer and The pointer just happens to be the memory

3:26:03 address of the GPA variable I have this char

3:26:07 variable down here And then I have this pointer variable,

3:26:11 which is storing a pointer which just happens to be

3:26:15 the physical memory address of the great variable in our memory Okay,

3:26:20 and that's basically all you need to know about pointers

3:26:24 to get started a pointer is just a type of data It's

3:26:28 just a type of information that we can work

3:26:30 with in our programs it just happens to be a memory addresses write

3:26:33 an integer is a type of data that we can work

3:26:36 with in our programs and it just happens to be a whole

3:26:38 number a double is a type of data that we can

3:26:41 work with in our programs and it's a decimal number a char

3:26:45 is a type of data that we can work

3:26:47 with in our programs and it's a character a Pointer is a type

3:26:50 of data that we can work with in our programs and it's

3:26:53 a memory address Hopefully that makes sense and just like we

3:26:56 create integer variables and double variables we can also create Pointer

3:27:00 variables the only difference is whenever we create a pointer variable We're

3:27:05 gonna store the memory address of another variable in our program

3:27:09 So so you'll see over here when I create this pointer variable.

3:27:13 I'm using the data type of The variable whose address I'm storing so

3:27:17 over here when I'm storing the address of an integer variable I'm saying

3:27:21 int here When I'm storing the address of a double variable I'm saying

3:27:25 double here when I'm storing the address of a char variable I'm saying char

3:27:29 here and that's the basics of using pointers and Also creating pointer variables

3:27:36 and storing memory addresses inside of variables In this tutorial I want to talk

3:27:45 to you guys about dereferencing pointers in C Now a pointer is basically

3:27:50 just a type of data that we can work with in our programs.

3:27:54 That is a memory address So sometimes in our programs

3:27:57 we're gonna want to work with memory addresses

3:27:59 We're gonna want to use them in our programs

3:28:02 and do certain things with them and those memory

3:28:04 addresses are basically called pointers in our programs

3:28:07 and when you dereference a pointer Essentially what you're doing

3:28:12 is you're going to the memory address of that Pointer

3:28:16 and you're grabbing the piece of information from there.

3:28:19 All right So all points are as our memory

3:28:22 addresses and when we dereference a pointer

3:28:24 We go to that physical memory address and we get the information or we get

3:28:28 the value that's stored inside of there so I'm gonna show you guys how we can

3:28:32 do that and see it's actually really easy and can be pretty useful so Down here.

3:28:37 I have this integer and It's an integer variable

3:28:41 and it's storing the integer value 30 and over here.

3:28:44 I have a pointer variable which is storing

3:28:46 the memory address of 30 so it's storing

3:28:49 the memory address of our age variable as you can see right here and Down here.

3:28:55 I'm just printing out this pointer So I'm printing

3:28:58 out pH I'm using this percent P and I'm

3:29:00 just printing it out to the screen So why

3:29:02 don't we take a look and see what we get.

3:29:04 So you'll see over here We just get this memory address probably

3:29:08 exactly what you expected to get so over here I want to show

3:29:12 you guys how we can dereference a pointer So this pointer variable

3:29:16 P age is storing the memory address of this age variable, right?

3:29:21 So when we printed it out We

3:29:23 basically were given the physical memory address where

3:29:26 this numbered 30 was stored So P age is storing the memory address of 30.

3:29:33 It's storing the memory address where this value is Inside

3:29:38 of our computer's memory if I want to dereference a pointer Basically,

3:29:43 what I'm gonna be doing is I'm going to be

3:29:46 going to that actual physical memory address and grabbing that piece

3:29:49 of information So the way that I can dereference a pointer

3:29:53 is by using the asterisks So when I'm printing this point,

3:29:56 I can dereference it by using this asterisk But now

3:30:00 over here since I'm dereferencing this pointer I need

3:30:03 to print out a number instead of a pointer because

3:30:07 when I dereference the pointer This is no longer a pointer.

3:30:12 This is no longer a memory address now This is

3:30:15 gonna be whatever was stored at the memory address.

3:30:18 The pointer was pointing to so this is going to be an integer

3:30:22 because this pointer Variable was storing the address of an integer Right,

3:30:29 so when I dereference it This is actually gonna be

3:30:32 an integer So let me show you guys I'm gonna print

3:30:34 out or I'm gonna run our program and it should

3:30:36 print out 30 So you'll see that we get a 30.

3:30:41 So basically what's happening here is

3:30:43 we're dereferencing the pH Pointer variable we're

3:30:46 dereferencing the pH pointer which basically

3:30:49 means we're going and grabbing the actual, you know Physical value that's stored

3:30:54 at the memory address that the pointer was pointing to and that's why we get 30

3:30:59 And that's basically what dereferencing is, you know,

3:31:01 you're you have a pointer You can dereference it

3:31:03 and you basically just get the value that it

3:31:05 was pointing to It's essentially all we're doing

3:31:07 so I want to show you guys how we could do this Just normally so for example

3:31:13 I have this age variable if I just typed this out If I wanted to get the address

3:31:17 of this age variable I could use this ampersand, right?

3:31:20 That's basically what we did up here but what I could do is

3:31:23 I could just dereference this so I can say ampersand age and then

3:31:26 I can put a asterisks here and this is actually gonna dereference that entire

3:31:31 thing So now when I run my program it's gonna print out 30,

3:31:35 so it's not gonna print out the memory address It's

3:31:38 just gonna print out 30 so you can see over here.

3:31:41 We get 30 so I could actually do this like as many

3:31:44 times as I want and this will maybe just illustrate what's happening so

3:31:47 I could put An ampersand here and now I'm gonna get that memory

3:31:52 address again So you'll see we're getting this memory address and I can

3:31:58 do the same thing so I could dereference this one more time

3:32:02 and You'll see now we're getting that 30 value again so I mean basically

3:32:10 what I'm doing I'm constantly like like here I'm getting the memory address

3:32:14 where the age variables stored Then

3:32:16 I'm dereferencing and getting the value then.

3:32:19 I'm getting the memory address again,

3:32:21 and then I'm dereferencing it again So, you know,

3:32:23 hopefully that makes sense when you dereference a pointer

3:32:26 you're basically just getting the value that that pointer is

3:32:29 pointing to or you're getting the value that's stored

3:32:32 in the memory address of the pointer In this tutorial

3:32:40 I want to teach you guys how to write two files in C One of the cool things we

3:32:46 can do in C is we can actually modify

3:32:48 and change and also create new files So in this tutorial,

3:32:52 I'll just basically show you guys how to do

3:32:53 that We'll talk about creating files writing files and appending

3:32:57 on to files So let's go ahead and do that down

3:33:01 here in my main function We're actually gonna create a file.

3:33:05 So I'm going to create a file and I'm gonna store some information inside

3:33:09 of that file and the way that we can do that is we can

3:33:13 just type out file all in caps just like that and essentially what we're

3:33:17 doing here is we're creating a pointer to a physical file on our machine,

3:33:23 so I'm just gonna say file and I'm gonna make an Asterix and I'm just gonna give

3:33:27 this a name So I'm just gonna call

3:33:29 this F Pointer and that'll just stand for file pointer,

3:33:32 and I want to set this equal to F Open an F open is actually

3:33:38 a function that's gonna open a file for us And so inside of these parentheses,

3:33:43 I'm gonna give this a couple of different parameters The first parameter

3:33:46 is gonna be the name of the file that I want to open.

3:33:49 So I'm just gonna call this employees dot text and the second

3:33:54 parameter is gonna be a File mode and a file mode is basically

3:33:59 just going to tell C What we want to do with the file

3:34:02 that we're gonna open And there's a bunch of different file modes.

3:34:06 The three most basic types are R and R stands for read So if I put R in there,

3:34:11 I mean we want to read the file W stands for right so if I put right in there it

3:34:17 means I want to either create a new file or write

3:34:20 over an existing file and There's also a which stands

3:34:24 for append and that means I want to append information

3:34:26 on the file in this tutorial we're gonna look at W

3:34:29 and a so what I want to do over here

3:34:31 is put in W and Right now employees dot text.

3:34:36 This file doesn't actually exist So what I'm gonna do

3:34:39 is I'm actually going to create that file We're gonna put

3:34:42 some information inside that file and then that file gets

3:34:45 saved onto my machine and in order to do that We're

3:34:48 gonna need this write mode So it's just going to be

3:34:51 a W Anytime that we're open file in see you

3:34:53 always want to make sure that you close the file

3:34:56 so over here I'm saying F open And down here I

3:34:59 want to use another function called F close and Basically

3:35:02 what this is going to do is it's going to close

3:35:04 off the file so it's gonna remove the file

3:35:06 from the memory on our computer and if we do make any

3:35:10 changes it'll kind of like save the file and Everything

3:35:13 will just be kind of like locked in so you always

3:35:15 want to make sure whenever you're working with a file

3:35:16 that you close It and inside of this F close function.

3:35:19 We just want to pass in this F pointer

3:35:22 So I'm just gonna say F Pointer right there.

3:35:25 So let's talk about what's happening here

3:35:28 file is basically just like Essentially like

3:35:31 a data type I guess and we're creating a pointer to a physical file

3:35:36 on our computer So if you remember a pointer is basically just a memory address

3:35:41 Right a pointer is a type of data that we can use in our programs.

3:35:45 It's essentially just a memory address and this pointer

3:35:48 is storing the memory address of this new

3:35:51 employees dot text file that we're gonna

3:35:53 create so Essentially what's going to happen is

3:35:56 we're gonna create this employee's dot txt file

3:35:58 and F Pointer is going to be pointing to the memory address of that file

3:36:03 on our computer So that's essentially what's happening here.

3:36:07 And what I want to do is I actually want to write some information To this file.

3:36:13 So actually let me just show you guys really quick.

3:36:16 What's happening here So without doing anything else just

3:36:19 using this line right here when I run my program

3:36:23 a new file called Employees dot text is going

3:36:26 to get created so over here I'm in my little directory

3:36:30 and If you don't specify an absolute path or like

3:36:34 a relative path for this It's just gonna create

3:36:37 it in the directory that your C files in So

3:36:40 if I wanted to create this on my C Drive,

3:36:42 I could say like C Whatever and then like employees

3:36:45 out text and this will get created in my C Drive,

3:36:48 but if I don't specify like an absolute path Or even

3:36:52 if I don't specify a relative path then this is just going

3:36:55 to get created in the same directory as our C file

3:36:58 and You'll see over here My main dot C file is right here.

3:37:03 It's in this folder So when I run this program,

3:37:06 let's watch over here and we can see

3:37:08 this employees doubt text file get created So I'm gonna

3:37:11 run my program and you'll see over here

3:37:13 that this employees file got created So if I actually

3:37:16 bring this out a little bit you'll see it's

3:37:17 a text document Its employees dot tax and there's

3:37:20 zero kilobytes inside of this so we haven't put

3:37:23 any information in there But this file got created.

3:37:26 So now let's talk about putting some info into that file inside this file

3:37:30 I'm basically just gonna list out a bunch of like employees that might work

3:37:33 in an office and The easiest way to write information to a file is

3:37:37 just to say F Print F and this is a special Function in C,

3:37:44 which actually works a lot like the printf function So if you've used the printf

3:37:48 function and if you're following along with this course

3:37:50 We've been using it throughout the entire

3:37:52 course the printf function basically allows you

3:37:54 to like print information out onto that console

3:37:56 window F Printf is it can allow us

3:37:59 to essentially just write information to a file?

3:38:02 I need to give this a couple parameters.

3:38:04 The first one is gonna be this file pointer

3:38:06 in other words I need to tell this function,

3:38:08 you know what it should be writing to so

3:38:11 I'm just gonna say f pointer and Then over here

3:38:14 we can just give this some information so I can

3:38:17 just use like after we put in this file pointer.

3:38:19 I Can just use this like I normally would

3:38:22 the printf function so I could just print out like some

3:38:25 employees So I could say like let's say one

3:38:27 of the employees is Jim And we'll put like what they do.

3:38:31 So like I can say sales men and then we could say actually

3:38:36 let's make a new line and we'll create another employee Pam and she is

3:38:40 the receptionist and then we can make some other employees so So now I

3:38:46 have three employees that I'm going to basically write out to my employees file.

3:38:50 So now when I run this program and We can go over here to this employees file

3:38:56 You'll see when I open this up that we have these three lines in here So we

3:39:01 have Jim salesman Pam receptionist and Oscar accounting

3:39:04 So basically I was able to create this file

3:39:07 and write all this Information in the file

3:39:09 from down here just from down here in this program.

3:39:12 So that's pretty cool And I want to show

3:39:15 you guys with right so when I'm using this, right?

3:39:19 This is actually going to overwrite a file.

3:39:20 So for example if I change this to from all this text and I just put like

3:39:28 Overridden in here now when I run this program

3:39:31 and We open this employee's dot text file.

3:39:34 So let me reopen this You'll see it

3:39:38 just says overridden so I've actually overridden all

3:39:40 the text that was in there And that's basically

3:39:43 what happens when we just write to a file.

3:39:45 So let's go back to what we had before I'm just gonna run this again.

3:39:49 So we have this text And you'll see now we're back

3:39:54 with the employees so let's say that um inside of this file We

3:39:58 wanted to add an employee on to the end of it So

3:40:00 we wanted to add like a new employee on to this file, right?

3:40:04 Well, I can use something called append and what append

3:40:07 is gonna do is it's going to allow us to append

3:40:10 a new line on To the end of this file

3:40:13 or actually just append text on to the end of the file

3:40:17 instead of saying w I'm just gonna say a and you'll

3:40:20 notice I'm still keeping employees text here and down here

3:40:23 inside of this F printf I'm just gonna print out

3:40:26 another employee into the file The first thing I'm gonna do

3:40:29 is print out a new line because and actually let

3:40:32 me show you guys this in this employees filed There actually

3:40:36 isn't a line after this last line so the last

3:40:39 like position in this file is over here I can't actually

3:40:43 get on to the line below Oscar So I'm gonna want

3:40:46 to go ahead and print a new line and this will

3:40:48 move the next employee on to the next line we

3:40:51 could say like Kelly and she's gonna work in customer Service.

3:40:56 So now when I run my program instead

3:40:59 of overriding what was in the employees text file now?

3:41:03 This is just gonna append this new employee onto the file.

3:41:06 So I'll come up here run the program And let's open this employee's

3:41:11 file and you'll see now we have another entry Kelly from customer service.

3:41:15 So when we write a file We either create

3:41:19 a new file and add content onto it or we

3:41:22 just override the content in the file At least

3:41:25 using this F printf function and when we append,

3:41:27 we're sub pending information onto the end

3:41:29 of the file so just using those two things,

3:41:31 I mean you can essentially write out any you want and it's

3:41:35 important to note that you can write more than just text

3:41:37 file so I could write like an HTML file or I could

3:41:40 write like a CSS file you could write really I mean any

3:41:44 type of file that you'd want and That's basically how we can

3:41:48 write to a file and append it to the end of a file

3:41:54 In this tutorial I will not teach you guys how to read

3:41:57 the Information from a file in C in the last tutorial

3:42:01 we talked about writing to files So we looked at how

3:42:05 we could create a file overwrite a file and append information

3:42:08 on to a file in this tutorial I'm gonna show you guys

3:42:11 how you can read Information from a file so down here you'll notice

3:42:15 that I have my file pointer setup So I just say file

3:42:19 and the asterisks and I'd called it FP and remember this is

3:42:23 just a pointer which you know and a pointer is just

3:42:25 a Memory address and it's the memory address of this file in Memory,

3:42:32 so it's the memory address of this employees

3:42:34 dot txt file which we created in the last tutorial and I'm using this F open

3:42:39 function which basically Opens this file and then

3:42:44 I'm specifying a file mode We talked about the W file mode and the a file

3:42:49 mode now I want to talk to you guys about the our file mode and this stands

3:42:53 for read So when I use the our file mode I'm actually able to read information

3:42:57 from the file and this can be pretty cool So in order to read the information

3:43:02 in the file I'm actually going to show

3:43:04 you guys how we can read like the individual

3:43:06 lines of the file So actually first let's

3:43:08 look at the file This is employees dot txt.

3:43:11 It just has you know,

3:43:12 basically a bunch of employees their names and then what they

3:43:16 do and We're gonna be able to read this file inside

3:43:19 of our program so the first thing I want to do is

3:43:22 create a variable and I'm gonna create this variable up here before we

3:43:25 create the file pointer and it's just gonna be an array

3:43:29 of characters and I'm just gonna call it line and I'm gonna

3:43:33 give this an initial size of 255 so be able to store

3:43:38 255 characters and then down here in Order to read this file.

3:43:43 I can actually read the file one line at a time and We can use a special

3:43:49 function Called F Gets and F gets basically

3:43:53 is going to allow us to read the lines

3:43:55 of the file one by one so I could say F Gets and I need to give

3:44:00 this a few different parameters the first parameter I need to give it is a place

3:44:05 to store the information that it gets So

3:44:07 this function is going to go and it's going

3:44:09 to read the information from the file and it's

3:44:11 going to store it somewhere So the first parameter we need to give this function

3:44:14 is where it should store that information so

3:44:16 in our case We're going to store it inside

3:44:19 of that align variable that we created up there.

3:44:21 The second thing we need to give this is a size and this is basically just

3:44:26 gonna be like The maximum size that we can read in so I'm just gonna say 255.

3:44:32 Basically this number should match this number up here and Then we also

3:44:37 want to give it that file pointer so I'm just gonna say f Pointer

3:44:41 and basically what this is gonna do is it's going to read the first

3:44:45 line of the file and it's going to store it Inside of this line variable.

3:44:50 So if I was to come down here and say print

3:44:54 F and print it out a string and Just print out

3:44:59 line now when I print out a line It's actually going

3:45:02 to be have the value of the first line in the file.

3:45:05 So let's run this program and You'll see over here.

3:45:09 We're printing out Jim salesman And if you

3:45:11 didn't remember that was the first line inside

3:45:14 of that employees dot text So basically what F

3:45:17 gets is doing is it's reading the first line.

3:45:19 And another thing that it's doing is

3:45:21 it's moving this pointer so remember this pointer

3:45:24 is Essentially pointing to where the file is in our memory and whenever we

3:45:30 use F gets this pointer is gonna get incremented So it's basically gonna move

3:45:34 past the first line in the file So if I was to use this again?

3:45:40 For example if I said f gets again when I use this It's gonna store the value

3:45:46 of the first line of the file inside of line And when I use it again,

3:45:50 it's now gonna grab the second line in the file.

3:45:52 So if I print this out now This should

3:45:55 actually be storing the second line in the file.

3:45:58 So when I run my program you'll see it's storing

3:46:00 the second line Pam Receptionist I can basically do this for all

3:46:04 of the lines in the file So that's basically how we can

3:46:08 use F gets to read the individual lines inside of a file

Study with Looplines Download Captions Watch on YouTube