Be modified so these would be values that just like can’t.
Change and those are called constants so if I wanted to make a num a constant in other words let’s say for some reason I didn’t want the num variable to be able to be modified in my program I could come over here and I could say Const so.
Co NS T and I’m gonna say that right before I declare the type now you can also say Const right after you declare the type but I’ve always preferred to just put it before so I say Const int num 5 and now this program is actually gonna throw an error so when I try to run this you’ll see down here it’s getting highlighted in red.
That’s because I can’t modify a constant so I’m trying.
To modify a num down here but I can’t because I gave it this Const keyword because it’s now considered a constant so in a lot of cases you want have variables that just can’t be modified can’t be changed and in a situation like that you want to make them consonants also a lot of times when we make constant variables in C we’ll give them all.
Uppercase names and this isn’t required this is just sort of like what a lot of developers will do so they’ll say like instead of.
N um like this they would say like num or maybe this.
Is like your favorite number so you could say fav underscore num so that would be how you could like distinguish two words but generally like constants will be all capital and that’s.
Just so it’s kind of obvious that they’re unchangeable that they’re constants but again you don’t have to do that but that’s kind of like a best practice so this is one.
Way that we can create a constant and we basically.
Create a constant variable that can’t be changed but a constant could also refer to just like any number or.
Any text that we’re using in our program so for example I’m gonna get rid of this if I came down here and I just like printed out some text so let’s say I just printed out like Hello this is actually also gonna be considered a constant so this is just like a string of characters a string of text that’s also considered a constant because it’s just like text on the screen we can’t change it we.
Can’t modify it it’s just kind.
Of there so you would also consider this a constant if I was printing out a number to like let’s say I came over here and print out 77 T is also considered a constant so it’s just considered to be like a piece of information or a piece of data in our program that is sort of unchanging right so 70 this isn’t gonna change I mean unless I physically came in here and changed it like this value is always gonna be 90.
No matter what I can’t modify it unless I like manually come in and modify it so.
That’s also considered a constant but I would say like for the most part like where this is going to come in handy is obviously doing stuff like this but creating constant variables variables that can’t change in this tutorial I’m gonna show you guys how to get input from a user in C so a lot of times.
In our C program so we’re going to be working with all different types of information and a lot of times we’re gonna want to get some of that information from the user so I’m going to show you guys how we can prompt the user to enter in.
Some information we can take that information store it inside of variables and then we can use it inside of our program so this is gonna be.
Pretty cool so down here I will basically write out the code for this and the first thing we want to do whenever we’re getting input from the user is we want to prompt them so I want to prompt the user you know as far as what they should enter in so I’m just gonna say printf and over here I’m just gonna print out a prompt so why don’t we ask the user to enter in their age so well I’ll show you guys how we.
Can get a number from the user so I’m just gonna say enter your age and now that we’ve prompted them to enter their age I need to do two things so the first thing I want to do is create a variable where we can store the input that the user puts into the program so I want to create a variable where we can store the age that.
The user inputs so I’m gonna create an inn over here oh it’s called age and I’m not gonna give this a value so all I’m gonna do up here is just declare the variable I’m just gonna tell see that I want to use this variable but I’m not gonna give it a value in other words I’m gonna allow the user.
Who’s inputting the age to give this a value so down here I want to be able to get input for from the.
User so I can use another function which is called scanf and scanf is basically going to allow the user to enter in some information into our program and this works similar to printf it’s kind of doing the opposite of printf right printf.
Is printing something onto the screen scanf is.
Allowing the user to input something into the program and we’re gonna make an open and close quotation marks and in here I basically want to tell see what type of information I’m asking the user to enter in so in our case we’re asking for an integer right age is going to be an integer it’s a whole number so I’m gonna accept.
As an input and integer and over here we want to tell C where we want to put integer so I’m basically gonna tell see what variable I want to store this in so I’m gonna store this inside of my age variable and I can essentially just type out the name of the variable here but in order to get input from the user I’m actually gonna.
Have to type an ampersand in front of this so.
Instead of just typing out age I’m gonna have to type ampersand age and when.
I say ampersand age this is what’s called a pointer and we’re gonna talk about pointers in a later tutorial I’m gonna cover everything you need to know about pointers but for now that’s a little bit beyond what we need to learn so all you need to know is that when you’re using scanf and you want to store information inside of like an integer or a float.
Or like a character you need to use this ampersand over here so down here now that we’ve scanned for the users age I’m just gonna go ahead and print it out so we’ll just print out like you are and I’ll say % D years old and.
Over here I’m just gonna print out that age variable so essentially what I’m doing is I’m prompting the user to.
Enter their age I’m storing whatever age they enter inside of this age variable and then I’m printing it out to them so let’s go ahead and run this program and we’ll see.
How we did so I’m gonna run the program and it says enter your age so let’s say someone is 50 when I click enter it’s gonna take that value it’s can take that integer that we entered 50 store it inside of that age variable and print it out you’ll see over here it says you are 50 years old so that’s how we can get an integer from the user we can also get like.
A double from the user so for example I could say.
Enter your GPA and so now instead of asking for an integer we’re gonna be asking for a double so I can come up here and say like double and we could call this GPA and now I.
Instead of saying D I want to say L F and L F is basically going to tell this scanf function that we’re looking for a double and then obviously instead of age we’re just gonna.
So now we can just say like your GPA is and then we can put % F because we’re gonna be printing out a double and I’ll just say GPA so you’ll notice here in printf when we want to use a double we’re using percent F to print it out but when we’re using scanf we’re.
Going to use percent LF so that’s just like a little difference and now let’s go ahead and run this program and we should be.
Able to get a GPA so let’s say someone’s GPA is like 3.1 so that’s how we can get a double I also want to show you guys how we can get a character so why don’t we create a character up here and we’ll just call it grade.
And we’ll say enter your grade and now when we want to get a.
Character we can just say percent C and again I.
Can just come down here and say ampersand grade and.
Here we can just say your grade is percent C and we’ll go ahead and print out the grade so now we should be able to get a character from the user so enter the grade let’s say I got an A and it says you’re good as a so we can use this scanf to get specific types of input from the user we can.
Store those specific types of input inside of variables now I also want to show you guys one more thing we can do which is we can.
Actually get a string from the user so in addition to getting numbers and a character we could also.
Get like a string of characters from the user and this is going to be a little bit different from doing like numbers and characters so I want to kind of show you guys how we can do this really quick so over.
Here let’s create a variable we’ll just call it let’s just call it name so we’ll be storing someone’s name and whenever we’re creating a string of characters we always need these open and close square.
Brackets and inside of here I’m actually going to specify how many characters I want to be able to store inside this string of characters so I’m just gonna put 20 and in the past in this course when we.
When we’ve been creating strings we haven’t put a number in there we’ve just kind of said like whatever all right we basically just given this a.
Value straight away but in this particular situation we’re not gonna be giving this variable of value right away in other words like I don’t know what the users name is.
Gonna be like I don’t know that right up front they’re gonna tell us what that is and so if I’m not gonna give this a value.
Right I do actually need to tell see how big I want this variable to be in other words I need to tell see how many characters I want this variable to.
Be able to store and that way si can go ahead and allocate enough memory for this variable so I’m just gonna put 20 and 20 basically means this will.
Be able to store 20 characters and I think that’s enough for a name so down here we’ll.
Just say enter your name and I can use scanf in order to get input in.
The form of a string but instead of saying percent see I’m just gonna say percent s and over here instead of saying ampersand grade I’m just gonna type out the name.
Of the string of characters so I don’t actually need this ampersand here I can just specify the name of the string so down here it.
Says printf your grade is and actually let’s just say your name is I would have say percent s and then over here we can print out the name so this should work just like it did in the other cases so let’s go ahead and run this and it says enter your name so my name is Mike and you’ll see it says your name is Mike so that.
Works out really well but there is one problem when we’re using scanf in order to get a string from the user so for example if i came in here and i said enter your name.
And i said my name was john smith when i click enter you’ll notice that.
It’s only saying your name is john it’s not including smith over here and here’s the problem this is because whenever I use this scanf function and i use it with a string it’s only gonna grab the characters up to the first space so once it sees this space it’s gonna be like okay we’re done.
Getting characters right so that’s kind of a problem and that’s just kind of how scanf works it’s not really you know scanf fault there is a way that we can modify scanf.
In order to be able to get input with spaces but there’s another function which I want to show you guys which we can use to get a line of text from the user and it’s called F gets and afghans is another function it’s similar to scanf but f gets is basically going to be more generous F gas.
Is essentially just gonna grab like a whole line of text it’s not gonna be.
Able to grab it and store it inside of like an integer or a variable or a character.
It’s just gonna be able to store it inside of like a string of characters so when we’re using a forgets the first argument we want to give it is the name of the variable where we want to store to the line of text so in our case it’s just gonna be this.
Name variable the next thing we want to do is specify how many characters we want to be able to input from the user so this will essentially limit the amount of characters that the user can input and this is always a good idea when we’re trying.
To get a string from the user because one common problem is that the user could try to enter in like a million characters and they would overflow the like the buffer in other words like.