C Programming Tutorial 4, Variables Pt.2

Create a new float variable so the first thing we need to do when we’re creating any variable is first we need to write out the type of variable that we’re creating so I’m just gonna go ahead right here and type out float FL oh 80 and then I’m just gonna give a name to my variable I’m just gonna.

Num1 and then I’m gonna go ahead and set it equal to something so I just need to write out the.

Equal sign and I’m gonna set this equal to two point seven because remember with floats.

Your number can have a decimal in it and then of course I just need to end the line so I just need to write out a semicolon and then I’m gonna go.

Ahead and create another float variable so I’m just going to write out float again and then I’m gonna name this variable num two and set it equal to five point two and then of course end the line.

Variable and set this equal to num 1 divided by num2 so I’m gonna say float num 3 equals num1 divided by num2 right here and then I’m.

Just gonna go ahead and print num 3 out onto the screen so I’m gonna say printf and then inside of quotes right here I need to do % F because we’re printing out floats out of the screen not integers and then I just need to do a comma.

And then the name of the variable that I want to print out onto the screen and I want to print out num 3 so I’m just gonna type out number 3 right there and of course I just need to end the line so let’s just go ahead and.

Run this program and make sure that it works and as you can see since this is a float it gave us an answer with a decimal point in it all right so now let’s just go ahead and change around these numbers and make sure that it works so I’m just gonna type in 10 right here and type in 2 right there.

And run this program and as you can see it got 5 however it did a decimal place and then a bunch of zeros right after that well that doesn’t look very professional so let’s just say we wanted to make it so there are only two digits after this decimal place no matter what well what we can do right here is change this around we can do a decimal point and then 2 and this number right there we’ll just signify the.

Number of digits that will be after the decimal place when it’s printed out so now when we go ahead and run this we see that we get five point zero zero so there are only two digits after this decimal place right here regardless of what number it is so again if I were to do like something that won’t come.

Out even like 1/3 and run this we see we only get two digits right after this decimal place right here all right and if you wanted to get rid of.

The decimal place altogether you can just change this number to a zero and now when I print it out you see we get zero because remember it just chopped off those numbers after the.

I were to change this yet again to 10 divided by Nam two should get just.

Five yep perfect all right now let’s go ahead and take a look at another variable type and this is CH AR.

You can pronounce this either car or char I prefer car though all right and then let’s just go ahead and name our variable I’m just going to call this my car and a car or a char will hold a character in it so we can just go ahead and set this equal.

To a character and to represent a character in C you just need to put your character inside of two.

Single quotes all right and then inside these two single quotes I’m just gonna type out capital a and then go ahead.

And end this line right here and then let’s just go ahead and print our character out onto the screen so I’m gonna say printf and then inside of quotes right here I need to do percent C for character and then a comma and then of course the name of my character variable right here which is my car so I’m gonna do my CH.

AR and then go ahead and run this and as you can see I just got capital a which is of course the character that I did store in this variable right here.