Using and Creating C++ Variables - C++ Programming Article

Using and Creating C++ Variables - C++ Programming Article

 

 Using and Creating C++ Variables-C++ Programming Article


Article for the learning light on C++ programming so in this Articles we're gonna learn how to create a variable you know how to assign some values to the variable and also how to retrieve the values from the variables. 

That in all of you guys have read my previous article you know which explained what is a variable what is a data type and you know about the constants the keywords etc etcetera so n this tutorial we'll be creating the variables and we'll be demonstrating you know how to use them alright a variable...

Using and Creating C++ Variables - C++ Programming Article
 Using and Creating C++ Variables - C++ Programming Article


The name given to a memory location to access that easily so to create a variable in C++ first you need to write the type specifier or the data type so which is nothing.

Type of data will be stored in your variable or if you know that you know there are four arithmetic data types available in C++ you know the integers the character of floating point and also boolean and also when special type is are called ID you know it is normally used with functions to indicate that you know a particular function is not gonna return any values so here we're gonna.

learn you know how to create the variables of type int care float boolean and also out as in some values to them.

how to retrieve the values from that so here I just for the demonstration purpose I'm going to create an integer variable you know our variable which is gonna store some integer value so the data.


Type or the value stored in that 01:36 variable is of type integer and that's why I'm going to use the keyword int you know which is used for integers int and then the variable name so I'm gonna call it as age then we need to add a semicolon here and similarly if you want to create a variable of type float then you need to write float and the variable name for example I'm gonna say average and we need to add a semicolon and if you want to create a variable of type character we need to use the keyword care and then the variable name let's

say sex and then if you want to create a variable of type boolean then you need to use the keyword bull and the variable name let's say is it that's pretty cool now we have defined the variables here so all this variable are going to contain some garbage values and to demonstrate that I'm gonna use the C out and we can use stream insertion operator and then we need to write the variable so here age I'm gonna write here and then stream in such an operator again and then I'm gonna add Ian's DL which is another constant defined in the STD. 

Namespace you know which is used to add our new line and then again the stream insertion operator and then average then again and this can be sex this time okay here we're gonna add our end line again alright sex and then again end line and again it's gonna be easy so we need to add a semicolon here so if I steal this program and run this build and run okay you guys can see here some garbage values are present so you know -2 some 1.80 etc etcetera.


garbage values so once we define a variable in our program you know the variables are going to contain some garbage values so so what we can do is we can initialize these variables to some values so we can initialize a variable when we define that variable or we can initialize them separately for

Example I

Can initialize this variable age here let's say 23 and to initialize a variable we can use the assignment operator which is an equal to symbol and I can initialize this variable average you know after defining it here let's say R is equal to twenty two point five two you know which is a floating point number that's why I'm going to use twenty two point five two and similarly if you want to initialize the sets you can do it here M and is it since it is a boolean. 

Type we can store true you know when we use true here a value of 1 will be stored in this is it variable and if you use false here a value of zero will be stored here so once we define and initialize a variable you know we can access the values stored in that variable here you know you can


see here we have used to see out function to just print out the values if I save this program and run this again build and run now you guys can see here 8:23 average twenty two point five two yeah average twenty two point five two then sets yum is it one instead of true we have one so now another thing that we can do is we can create a bunch of variable at a time for example if you want to create let's say three variables of type integer at that time you can combine them for example int Y a and then you need to separate them by a comma then B comma C and also if you want to initialize them here you can do that for example in a then B equal to 10 comma C etc etc. 

You know I'm gonna use another C or function here and I'm not a for the variable B and then I'm gonna add a new line I'm gonna sell this we'd build and run so now you guys can see you I have 10 so the value of B you know which is 10 here so another thing that you should keep in mind is that you know before using a variable in your program you need to define it so if I use a variable which is not defined in my program then the compiler is going to generate some errors for example here in our program we don't have a variable called B right now because you know I have removed that line now when we refer to that variable B you know the variable is not present in our program that's why the compiler is going to generate some errors so to demonstrate that I'm gonna save this build build and run so you guys can see here error B was not declared in this scope that's because you know B is not defined or declared in our program. 

All right the next thing is you know about the type modifiers so we can use the type modifiers with our basic data types

for example short int etcetera etcetera so what this is gonna do is it's gonna influence on the number of bytes to store the values for examples short int is smaller than in etcetera etcetera all right this is it guys this is about the variables how to create or define a variable how to use them how to store the values in them how to retrieve the values from them...

Post a Comment

0 Comments