A variable furnishes us with named capacity that our projects can control. Every variable in C++ has a particular type, which decides the size and format of the variable's memory; the scope of values that can be put away inside that memory; and the arrangement of activities that can be applied to the variable.
The name of a variable can be made out of letters, digits, and the underscore character. It must start with either a letter or an underscore. Upper and lowercase letters are particular in light of the fact that C++ is case-touchy −
There are various sorts of types in c++.
The name of a variable can be made out of letters, digits, and the underscore character. It must start with either a letter or an underscore. Upper and lowercase letters are particular in light of the fact that C++ is case-touchy −
There are various sorts of types in c++.
Sr.No | Type | Description |
---|---|---|
1 | bool | Stores either value true or false. |
2 | char | Typically a single octet (one byte). This is an integer type. |
3 | int | The most natural size of integer for the machine. |
4 | float | A single-precision floating point value. |
5 | double | A double-precision floating point value. |
6 | void | Represents the absence of type. |
7 | wchar_t | A wide character type. |
Declaring (Creating) Variables
To make a variable, you should indicate the type and assign out it a value:
Syntax: -
Where type is one of C++ types, (for example, int), and variable is the name of the variable, (for example, x or myName). The equivalent sign is utilized to relegate(assign) values to the variable.
Example: -
Make a variable called myNum of type int and assign it the value 15:
Example: -
You can also declare a variable without assigning the value, and assign the value later:
Example: -
Display Variables: - The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
Example: -
Syntax: -
type variable = value;
Where type is one of C++ types, (for example, int), and variable is the name of the variable, (for example, x or myName). The equivalent sign is utilized to relegate(assign) values to the variable.
Example: -
Make a variable called myNum of type int and assign it the value 15:
int myNum = 15;
cout << myNum;
cout << myNum;
Example: -
You can also declare a variable without assigning the value, and assign the value later:
int Num;
Num = 15;
cout << Num; //Num is 15
Num = 15;
cout << Num; //Num is 15
Note:
in the event that you assign a new value to a current variable, it will overwrite the past value.
in the event that you assign a new value to a current variable, it will overwrite the past value.
Example: -
int
Num =
15;
//Num is 15
Num = 10; //Num is 10
cout << Num; //Output of Num is 10
Num = 10; //Num is 10
cout << Num; //Output of Num is 10
Display Variables: - The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
Example: -
int
Age =
18;
cout << "I am " << Age << " years old." ;
cout << "I am " << Age << " years old." ;
ConversionConversion EmoticonEmoticon