C Programming Tutorial For Beginners

The instructions are that’ll determine what the program does and as we go through this course we’re gonna learn more and more complex instructions we’re also going to learn how we can take simple instructions.

Like printf and do different things with them to make our programs a.

Lot more powerful in this tutorial I want to talk to you.

Guys about variables in C now when.

We’re using C programming language a lot of times we’re gonna be dealing with data so there’s gonna be different data values or different information that we’re gonna want to keep track of and use inside of our programs and sometimes it can be difficult.

To maintain and keep track of all that data and that’s where variables.

Come in a variable is essentially a container where we can store different pieces of.

Information so different data values we could sort of things like numbers or texts or characters and these variables make it a lot easier for us to keep track of.

And manage all the data in our program so I’m gonna show you guys basically how variables can be useful and how we can use them in our C programs.

But down here I have a very basic program I’m basically just printing out a little story it says there once was a man named George he was 70 years old he really liked the name George but did not like being 70 so.

This is a simple program we’re just printing out a bunch of different lines of text and then when I run this program you’ll see we print out the actual story so over here we have our story and this is great but let’s say that I’m reading through my story I’m.

Reading through my program and I’m thinking to myself hmmm I think I want to change the character’s name so instead of calling the character George why don’t we call him like John in order to make that change I’m gonna have to look through my entire program and find every place.

Where we mention the character’s name so right here is one change it to John remember keep looking and here’s another one so we’ll change this to John right so I had.

To manually go through and change every instance of the character’s name to John and let’s say that okay that sounds pretty good but maybe I’m thinking I want to make the character.

A little bit younger so instead of 70 why don’t we make him 35 so now again I’m gonna have to look through my entire program find every place where we mentioned the.

Character’s age and change it to 35 so there and there there you go I changed the program I updated and modified it now if I was to run this program then all that information will be updated and we’ll have new story here’s the problem though I’m dealing with a very short story I mean this is four lines we only mentioned the character’s age and.

Name twice but imagine if I was writing out a huge C program that had hundreds of lines in this story so story had hundreds of lines and maybe we mentioned the character’s name hundreds of times they’re mentioned their age hundreds of times well if I wanted to.

Change the characters age your name I would have to go through and manually change it in every single location that would take forever and it would be a huge track basically that’s not a good way for us to manage and maintain the data and our programs all right we basically have two pieces of data that we’re working with consistently in this program the characters age and the character’s name what I want to show you guys now is how we can use variables to better keep track of and manage these pieces of information so.

Remember a variable is basically just a container where we can store some data so I want to create two variables which will store the character’s name and the character’s age and I’ll show you guys how this can make.

Our program a lot easier to manage up here above these print statements I’m just going to make a new.

Line and I’m gonna come over here and I want to create a variable now whenever we create a variable in C we have to give C a couple pieces of information so we have to basically tell.

C a couple different things the first thing we have to tell C is what type of information we want this variable to store now I’m gonna get into the different data types that we can represent and see more in the next tutorial but for now just know that there’s a bunch of different types of data that we can store.

And C we can store things like numbers characters we can store text we can store.

Decimal numbers all sorts of stuff like that in our case I’m going to store the character’s name and the character’s name is basically just a collection of characters so in order to create this variable the first thing I have to do is tell C that I want to create a variable that’s gonna store characters so to do that I’m just gonna type out char just like that and that’ll basically tell C hey we’re gonna store characters inside of this variable.

After we type out char I want to type out the name of the variable that I want to create remember of variables that.

Contain and it’s a good idea to give these containers descriptive names and those names will basically let us know what that variable is storing inside of it so I’m gonna call this character name and.

Now what I want to do is I basically want to store multiple characters so over.

Here I’m telling to see that I want.

But in C we can also store instead of just storing like one single character we can store a bunch of characters and that would be kind of like you know a name so there’s like four characters in here in order to store a bunch of characters inside of this variable.

After we type the character Abel’s name we’re gonna have to make an open and closed square bracket and that’s gonna tell C that we want to store a bunch of characters inside this variable so I’m gonna type char a character name open a close.

Square brackets I’m gonna set this equal to something so I’m essentially.

Assigning a value to this variable and I’m just gonna make an open and closed quotation marks and inside of here we’re gonna type out the value that we want to store so in my case.

I’m just gonna store the character’s name which is John and then as always in C we’re gonna have to end this off with.

A semicolon all right so essentially what I did here was I created a variable and I told C what I wanted to store in the variable I wanted to store a character I gave this variable a name it was character.

Name and I used these open and close square brackets to sell C that I wanted to store a bunch of characters not just one now what we can do is we can make another variable to store the character’s.

Age so in this case we’re gonna want to do.

Something similar but we’re gonna want to store a number there’s a bunch of different ways that we can store numbers and see but for our purposes we’re storing a name so we’re gonna use something called an integer and an integer is basically just a whole number so I’m just gonna type out int and that basically.

Stands for integer and now we’re gonna give this a name so I’m gonna call this character age and I’m gonna set this equal to a number so in our story the.

Character is 35 so I’m just gonna type out 35 and now we can again end this off with a semicolon so I have two variables here this one’s storing a collection of characters this.

One’s storing an integer now the question becomes how can we use the inside of our program well basically what we can do is we can print them out alongside of this text and we can actually use.

This print F instruction in order to do that so I’m gonna show you guys how to do this and then we’re gonna talk about it so basically I’m gonna go over here where we have the character’s name.

I’m gonna erase this and I’m gonna replace it with a % and an S and now over here I’m gonna make.

A comma and I’m just gonna type in the variable name so I’m gonna type in character name and basically what’s happening is when we use.

This percent s we’re telling see that inside of this string of text here we’re gonna want to insert a another string and a string is basically just a collection of characters so I could call this up here a string variable so when I.

Say % s this is basically a placeholder and I’m telling C I’m gonna insert.

A string into here and over here I use this comma and then I type out the string that I want.

To include so in our case it’s.

Character name and so basically over here it’s gonna say there once was a man named % s and it’s gonna replace % s with whatever we specify over here so it’s gonna replace % s with the value that’s stored inside that character name variable so I’m gonna save this and we’ll run our program and now you’ll see that it’s still printing out there once was a.

Man named John even though we didn’t actually type out John over here we just typed % ass in the character name so.

I can do this in this other spot too so down here we’re also using the character’s name so I can type % s and over here we can type character name and again this is going to act as a placeholder for this string.

Of characters that we specify over here let’s do the same thing with the age so over here we have the age is 35 I’m going to do something similar and I’m gonna say percent but now because I have a number instead of using.

Percent s I’m gonna use percent D and that basically means that we’re gonna insert an integer number into here so I’m saying he was percent D years old and over here I’m.