C Programming Tutorial For Beginners

See wouldn’t be able to handle accepting that many characters and the program might break so over here with F gaps we can specify how many characters we want to be able to accept so I’m just gonna say 20 because that’s how many characters.

We can store inside of this variable and over here I’m just gonna say stdin.

And this stands for standard input essentially what we’re doing over here is we’re telling FCAT s– where we want to get the information.

From and standard input is set as essentially just like that little console that we’ve been using so now I’m using F Gatz and this is going to do exactly what scanf did so we’ll be able to store the input inside of this named.

Variable but now we’ll be able to store multiple words instead of just one single word so let’s run this program and it says enter your name so now if.

You’ll see it’s able to enter in John Smith now the one downside with just using this F gets is you’ll notice that it printed out this new.

Line over here so let me actually demonstrate this a little bit so if I was to.

Print out like you know just some random text over here whenever I get input using scanf so if I entered in John Smith when I click the enter key so when I’m entering this by clicking the Enter key that’s actually going to represent a newline character and.

That’s going to get stored inside of the string so when I click enter you’ll notice it says your name is John Smith and then it prints out a newline and it prints out this text.

And so that’s just something that you need to be aware of when you’re using this F.

Gets function but for the most part I would say if you’re getting.

Input from a user in the form of a string you can use F gets you can also use scanf and you could use multiple percent asses and i’m actually going to show you guys how we could do something like that in a future tutorial but for the most part I would say whenever you’re getting a string.

From the user you just want to use F guess so that’s the basics of getting input from the user like I said there’s a couple other things that we can do and I’m gonna talk about at least one other way that we can get strings from the user in a future tutorial but this is kind of the basics and you can kind of play around with doing this stuff in.

Your programs in this tutorial I’m going to show you guys how to build a basic calculator in C we’re basically gonna build a little program where the user can enter in two.

Numbers and then our program will take those two numbers add them together and spit out the answer so this is gonna.

Be kind of cool and we’ll also learn about getting numbers as input from a user so over here I want to show you guys exactly how we can do this the first thing I.

Want to do is print out a prompt so I basically want to prompt the user for some input so over here I can just say printf and inside here we’re just gonna give them a little prompt so I’ll say enter first number and essentially what we’re.

Them do is enter in two numbers so.

I’m actually going to create two variables up here so we’ll make an int and we’ll call it num1 and then we’ll make another inch and we’ll call it num2 and i’m not actually going to give these values right up front we’re gonna end up giving these whatever the user enters in so after the user enters the first number we need to actually grab that number so I can use another see function called scanf and in here we’re basically just gonna say percent d because we want to grab an integer and then over here I’m going.

To say the name of the variable where I want to store the value that gets entered now if you’re following along with this course in the last tutorial we were able to get a string of characters as input from the user and basically we just typed in like the name of the variable but when we’re getting input that’s not a string of characters in other words when we’re getting input.

That’s not percent s so if it’s like percent d or percent 4 percent C so if it’s a guy a decimal or an integer or a float or a character we actually need to use a special symbol this ampersand so I’m gonna have to say ampersand and then the name of the variable what I want to store this value so I’m just gonna say ampersand 1 and basically what this means is.

The address of num 1 and we’re gonna talk about addresses and pointers in a future video but for now just know that you need this ampersand here in order to store the value that gets entered inside.

Of this variable so once we do that and now we can move on and we’re basically just gonna do the same exact thing but for the second number so I’m gonna.

Copy these and I’ll paste this and now instead of saying enter first.

Number we’ll say enter second number and we’re just gonna store this inside of num2 alright so once we’re done with this the last thing we want to do is add them together and print out the answer so I’m just going to come down here I’m going to say printf and we’ll just say answer.

And over here we’ll print out the answer so it’s gonna be an integer and we’re basically just gonna print out num1 plus num2 so we’re gonna print out the value of num1 plus num2 so we.

Have our basic calculator we’re getting the first number we’re getting the second number we’re storing them inside of these variables we’re adding them together and printing them out let’s try to run our calculator so I’m gonna run my program and you’ll see over here it says enter first number so let’s go ahead and enter 6 enter second number will enter a8 and now we’re gonna get 14 so 14 is the correct answer so our program worked it was able to add the.

Numbers correctly and everything’s awesome but let me show you guys one problem with this program if I wanted to do math on multiple non decimal numbers for example if I said 2 and then over here I said like 6 point 8 when I add these together we’re not going to.

Get the correct answer we’re gonna get 6 plus 2 which is gonna be an integer 8 but we’re not gonna get 8 point 8 so we’re not getting the correct value so instead of letting the user enter in only integers why don’t we instead let them enter in doubles so over here I’m going to change these to.

Double so I’m gonna say num one’s gonna be a double and num2 is gonna be a double and since we’re getting doubles as input we’re gonna have to come down here and modify these so right now this is accepting an.

Integer but we want to make this accept a double so normally if we’re.

Using printf and we wanted to print out a double we would say f and that stands for like floating-point number but when we’re.

Scanning for a number if we want to use a double we have to say LF just like that so down here I’m also gonna say LF and we’ll change this to just F so print F is a little bit different than scanf right.

And scanf if we want to accept a double as input we need to use LF in printf we just use % F so let’s run our program now and we should be.

Able to add floating-point numbers so decimal numbers over here I will do four point five plus six point seven and we get 11 point two so that looks good to me.

Looks like everything is working properly and now we have a basic calculator so the user can enter in any numbers that they want and the.

Calculator will be able to handle them now this calculator is not.

Like 100% ironclad secure for example like if.

I was to come up here and enter in like a string of characters instead of a number you’ll see that it’s gonna break the program right so it’s just like not doing what we wanted to do and as we go further in this course we’re gonna learn all sorts of ways that we can check to see if certain things are getting entered correctly and we can basically mitigate circumstances like that but for now this is just kind.

Of an introduction into how we can get numbers as input and do things like addition or multiplication once we have them in this tutorial I’m going to talk.

To you guys about building a little game in C more specifically we’re gonna be building a mad libs game now if.

You ever played the game mad libs it’s basically a game where you write down a bunch of random words so it could be like you know nouns or verbs or someone’s name or you know a verb ending in ing something like that you take all of.

Those words that you enter in.

And you kind of sprinkle them in into a story and then generally the story is like kind of.

Funny because it has all these random words in it it’s actually if we head over to my web browser you’ll see I have a.

Picture of a Madlib up here and basically you just add in a bunch of random.

Words into the story and then you’d read the story back and it could be kind of funny so I’m gonna show you guys how we can build something like that in C and we’re also gonna talk about some more ways that we can use that scanf function that i.

Showed you guys in the last tutorial so over here we have a little story that I printed out it just says roses are red violets are blue I love you kind of like a classic poem but I.

Think this poem would be a lot funnier if we turned it into a mad libs so that’s exactly what we’re gonna do I’m gonna replace roses are red I’m gonna replace red with a random color so we’re just gonna have.

A user enter in a color I’ll replace violets with a plural noun so we’re gonna have them enter in a plural.

Noun and I’m gonna have instead of saying I love you we’re gonna say I love and then a specific celebrity so I’ll just type in celebrity right there alright so this is basically what we’re gonna be printing out we’re gonna be printed out roses are and then whatever color they enter in plural nouns are blue and then I love whatever celebrity so let’s talk about actually creating this program so.

We’re actually going to need to do a couple things and actually the first thing I want to do is I want to create variables we’re gonna create variables to store the color that the user inputs the plural.

Noun that the user inputs and the celebrity the user inputs we’re going to create three variables and these are basically going to be character strings so they’re going to be collections of characters and we can store them in variable so I’m gonna create some of these variables why don’t we create one for color and we remember whenever we create a.

Or like a collection of characters we need to make these open and closed square brackets and what I also want to do because I’m not gonna be giving color of value right away in other words we’re letting the user determine the value of color I just.

Need to tell see how many characters we want this string to be able to store that way si knows how much memory it needs to allocate for this variable so I’m just gonna say 20 and we’ll basically just say they can enter in a color that is up to 20 characters we’re gonna do the same thing for plural noun and again.

We’ll let them enter in 20 characters maximum and finally we’re gonna do the same thing for celebrity so again 20 characters sounds good alright now that we have our variables created.

I want to actually get information from the user so I want to prompt the user for information and I want to take the information that they entered in and I want to store it inside of each one of these variables.

First order of business is to prompt them for input so I can just say print F and inside here we’ll basically just type in enter a color once we’ve prompted them to enter the color we can actually.

Get whatever color they enter and store it inside of a variable I’m going to use a function called scanf and over here I’m gonna accept a string so I’m going to accept a string of characters and we’re gonna store this inside of our color variable just like that and.

Remember if you watched the last tutorial we use the ampersand here when we were getting numbers or also you’d do the same thing if you were getting a single character but when we’re getting input for a string of characters we don’t need that ampersand so you can just.

Get rid of that let’s copy these and I’m gonna paste this two more times so the second thing we want to get from them is going to be the plural noun and I’m gonna store this inside of the plural noun variable so you can see now we’re getting the plural noun and finally we’re gonna do the same thing for celebrity and again we’re gonna get that celebrity.

Okay cool so now I’m getting the color I’m getting the plural noun and I’m getting the celebrity so the last thing we have to do is we have to take all of these variables and put them into our story right so we need to be able to print out the story with all of those variables so I’m gonna come down here and I’m just going to say percent s and over here we’ll pass in the color same thing here I’m gonna replace the plural.

Noun here with a percent s and we’ll pass in the plural noun and finally same thing for celebrity down here alright so everything seems to be wired up and you’ll notice that I have new lines here.

So that this story prints out on new lines let’s go ahead and run this program and we’ll see how we did so over here it’s prompting us for a color why don’t we enter in magenta enter a plural noun let’s.

Do microwaves and enter a celebrity why don’t we just say Prince so when I click enter it’s gonna say roses are magenta microwaves are blue I love prints so we were able to prompt the.

User to enter in all of that input we took everything that they input we stored it in variables then we printed all those variables out inside of our story and we have our Madeleine the.

Program seems to be working really well I do want to show you guys one way that this program could mess up so let’s.

Go ahead and run this again so let’s enter in a different color I’m going to enter in like blue enter in a plural noun so why don’t we enter in phones and now enter in a celebrity so I’m gonna show you guys.

One way that we could actually break this program if I entered in a celebrity with a first and a last name like Tom Hanks when I click enter now you’ll notice that instead of saying I love Tom Hanks it’s only saying I love Tom here’s the problem when we use.

That scanf function scanf is only going to grab characters up to the first whitespace so essentially when.

We put this space here we’re telling see that we don’t want to grab anymore but in reality we want to be able to grab the there’s full-name want to be able to grab the celebrities first and last name if need be so this is a situation in C where we would have to modify our a little program so what I could do is instead of just getting one variable.

Like the celebrity I can actually get to so I could say over here like celebrity F and that’ll stand for celebrity first name and then down here we can make another variable called.

Celebrity l that’ll stand for a last name so now when we scan instead of just scanning for one string of characters.

I can scan for two strings of characters and we’ll have celebrity F and.

Then celebrity l and down here we’re gonna want to do the same thing so we can just say celebrity F and celebrity L and we just need to add another percent here so it’s gonna say I love celebrities first name and celebrities last name so let’s.

Run our program and see how we did so I can enter in like red and microphones and now we can enter in Tom Hanks and we’re printing out the actors first and last name so that’s one way that we could remedy this program and make it be able.

To accept two inputs with a space in the middle and it also just shows you guys a little bit more about how scanf works so it’s gonna stop scanning it’s gonna stop getting the input at that first space now here’s the thing about this.