In this article, we are going to start diving into the depths of the C++ language. Variables will be the subject of our today’s talk. So, are you ready to go down?! OK man, let’s go!!
What are Variables?
The first question that emerges is: What are variables?!
Variables are named memory segments, used inside computer programs to store different types of data. For a variable to be used in C/C++, it MUST be declared first. A variable declaration is like a notification for whom it may concern (in our case, the memory manager, and the compiler) that a variable named x is going to be used. When declared, a segment (location) in memory is reserved for that variable for future read and write operations. That is what the memory manager does. Later on, after the program is written, it should be compiled to convert the human-readable source code into machine-readable code. When the compiler encounters a variable x declaration statement, it takes into account that a variable x is going to be used somewhere in the rest of the program.
So, take it as rule, and put it as earring in your ears (as the Egyptian saying tells):
A declaration statement must precede the first appearance of a variable in a C/C++ program. If not, the compiler will fire a syntax error.
When you assign a value to a variable using the assignment operator ‘=’, behind the scene, you are actually making a write operation to the memory segment reserved for that variable. Similarly, when you read the value of a variable, you are actually executing a sub-system read operation to the contents of the memory segment for that variable.
Is it clear now?! Great!! Let’s move to another point.
Variable Declaration Syntax
To declare a variable, you have to specify two pieces of information:
- First: the data type of the variable.
- Second: the name of the variable.
The variable name could be any C/C++ valid identifier:
- A name starting with either a lowercase, uppercase letter, or underscore.
- Followed by zero or more letters, underscores, or digits.
- Distinguishing the letter case (case sensitive).
- No special characters allowed.
- Mustn’t be C/C++ reserved word.
Examples for valid C/C++ identifiers:
Examples for “invalid” C/C++ identifiers:
One may ask: Why must we specify a data type for each variable?
The answer is simple: Specifying the data type on variable declaration tells the memory manager how much of memory space should be allocated for that variable.
The following table lists the basic data types supported in the C++ language:
|Bool||Boolean (true or false)|
|Int||Integer (whole number)|
|Float||Floating point number|
|Double||DoA longer floating point number|
Type modifiers alter the meaning of the basic data type, resulting in an almost-new type.
|unsigned char||8 bits||0 to 255|
|Char||8 bits||-128 to 127|
|Enum||16 bits||-32,768 to 32,767|
|unsigned int||16 bits||0 to 65,535|
|short int||16 bits||-32,768 to 32,767|
|Int||16 bits||-32,768 to 32,767|
|unsigned long||32 bits||0 to 4,294,967,295|
|Long||32 bits||-2,147,483,648 to 2,147,483,647|
|Float||32 bits||3.4 * (10**-38) to 3.4 * (10**+38)|
|Double||64 bits||1.7 * (10**-308) to 1.7 * (10**+308)|
|long double||80 bits||3.4 * (10**-4932) to 1.1 * (10**+4932)|
To declare a variable named counter which will take integer values between 0 and 100:
To declare a variable named e to store the value is the base of the natural logarithm (e=2.718):
Multiple Variables Declarations
C and C++ are nice enough to let you declare more than one variable in one step, given that they are all of the same type. The variable names should be separated by colons.
To declare three integer variables x, y, and z:
To assign a value to a declared variable, the assignment operator ‘=’ is used.
The following statement will assign the value 2.718 to the e variable declared above:
The following statement initializes the counter variable to 0.
Combining Declaration and Initialization
C and C++ allow the developer to combine the variable declaration and initialization in one step. So, the above two statements used to declare and initialize counter to 0 could be replaced by only one statement:
Prompting User for Input
Another way for assigning a value to variable is via accepting user input. C++ has a predefined object called cin that represents the standard input stream (characters typed at the keyboard). An operator ‘>>’ called the extraction operator is used with cin to accept input and then assign it to a variable on its right side.
cin >> VARIABLE;
This statement suspends program execution until the user enters input. When the user types the required input and presses Enter, the input is assigned to VARIABLE.
As with cout, using the cin object requires inserting (including) the header file iostream.h
The following program asks the student to enter his bench number, and responds with a greeting.
cout << "Enter you Bench Number: ";
cin >> benchnum;
cout << "Welcome, Student no. " << benchnum << endl;
cin >> benchnum;
This statement causes the program to wait for the user input. When entered, the input is validated (to make sure it is of the proper type, which is “Integer” in our case) before being assigned to the integer variable benchnum.
* * * * * *
- Variables are named locations (segments) in memory, used to store data when executing computer programs.
- Before using a variable, it must be declared.
- To declare a variable, you have to specify its type, and its name.
- C++ has a set of basic data types, and another set of type modifiers that alter the meaning of the basic data types.
- A Variable could be declared and initialized in one statement.
- Assigning values to variables is done using the assignment operator ‘=’.
- The cin object with the >> operator are used to obtain input from the user.
The next article will talk about Operators. So, see you there!!!