You can think of a variable as a garage for your home. You store your favorite things and valuable items like your car, clothes and etc.
You keep these things there in hopes to use them later right? Well, in c++ we use the term variable as a named storage location for holding data .
Variables allow you to store and work with data in the computer's memory.
Below is an example of assigning value to our defined variable
int apples;     // int represents the data, while apples is the name we are defining to the variable
A variable definition tells the compiler the variable's name and the type of data it will hold. Notice that the definition gives the data type first, then the name of the variable, and ends with a semicolon. This variable's name is number. The word int stands for integer, so number may only be used to hold integer numbers.
int apples = 5;    // We are assigning the number 5 to apples
This is an assignment statement, and the = sign is called the assignment operator. This operator copies the value on its right (5) into the variable named on its left (apples). This line does not print anything on the computer's screen. It runs silently behind the scenes, storing a value in RAM. After this line executes, number will be set to 5.
int Literals     string Literals
5     How many apples do we have?
24     How many students do we have?
104     In room number?
A literal is a piece of data written directly into a program's code. One of the most common uses of literals is to assign a value to a variable. In our example above we assigned the literal value 103 to the variable number. number = 103; Another common use of literals is to display something on the screen. The value of number is Literals can be characters, strings, or numeric values.
It is best practice to define your variables in a sensible related matter. As well as staying consistent will help you in the long run.
Make sure you follow these best practices to avoid mistakes and bad habits.
The first character used must be an alpahabet set of a,A through z,Z, or an underscore (_);
After the first character you may use digits O through 9, or underscores .
Uppercase and lowercase characters are distinct. This means Apples is not the same as apples.
Variable Name     Legal or Illegal
monthOftheYear     Legal
48People     Illegal
_48people     Legal
year2018     Legal
number#3     Illegal
From the examples above, you have been seeing the word intenger or int a lot. You may already have an idea what its use for, but there is actually eight different data types we can use for storing integers. They differ by how many bytes of memory they have for storing data and what range of values they can hold. The number of bytes a data type can hold is called its size. Typically, the larger the size a data type is, the greater the range of values it can hold.
Data Type | Size | Range | ||
---|---|---|---|---|
short int |
2 bytes | -32,768 to +32,767 | ||
unsigned short int |
2 bytes | 0 to +65,535 | ||
int |
4 bytes | -2,147,483,648 to +2,147,483,647 | ||
unsigned int |
4 bytes | 0 to 4,294,967,295 | ||
long int |
4 bytes | -2,147,483,648 to +2,147,483,647 | ||
unsigned long int |
4 bytes | 0 to 4,294,967,295 | ||
long long int |
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | ||
unsigned long long int |
8 bytes | 0 to +8,446,744,073,709,551,615 | ||
Lets go ahead and use those data types for our next application. Code ahead and follow this source code down below. You will see that we will be aligning the defined variables differently that have the same data type. We are simply seperating them with commas and closing the statement with the semicolon;
If you are writing a program that works with dollar amounts or precise measurements, you need a data type that allows fractional values. In programming terms, these are called floating point numbers. Follow me on to the next section to go over this topic. See you soon!
Quickly move through different sections of this topic by clicking down below!
Floats
Comments