Assigning Values
Here is an example of assigning values to variables.
 | |  | | |
#include <iostream.h>
void main()
{
int x, y = 10;
x = 5;
int aNumber = 2147483643;
cout <<"Here is the value of x: " << x << "n";
cout <<"Here is the value of y: " << y << "n";
cout <<"Here is the value of aNumber: " << aNumber << endl;
} |
|  |
|
As you can see you can declare multiple integers on the same line by typing
This would declare a, b, c, and d all integer variables, meaning they can hold numbers. I declared 3 integer variables above, two on one line and one on its own line. To assign a value to a variable, you must use the assignment operator (=). So if you wanted to give y the value 30, you would type y = 30. You can declare and assign a value on the same line if you want. As you see I did that once above. You can also declare each variable on its own line and give it a value on another line; it’s all up to you. If you run the program you will see it printed all the values you assigned to the variables. Remember do not go over the limit of the integer. If you assigned a variable a value of 21,474,836,434,905 you would get an unexpected result or an error.
Type in C++ does not only refer to integers, it can also be used to define other variables. Each different type stores a different value. For instance the float stores decimals.
An example of the type float:
 | |  | | |
float x,y;
x = 3.4;
y = 46304.32; |
|  |
|
The above code would assign those values to the variables. It is not possible to assign decimals or characters to a different type. What I mean is that you can not create an integer variable and assign it a float value.Please Rate this Code:
Comments for: Assigning Values
There are NO user contributed comments for Assigning Values.
|
 |
|
 |