System ProgrammingLearn about Variables and Data Types in C++ programming

Learn about Variables and Data Types in C++ programming

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.

DataTYPE VariableNAME;

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:

x
NAME
_fullname
counter20
student_bench_number

Examples for “invalid” C/C++ identifiers:

1_counter
Percentage%
email@
for
class

Data Types
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:

Data Type Description
Bool Boolean (true or false)
Char Character
Int Integer (whole number)
Float Floating point number
Double DoA longer floating point number

Type Modifiers
Type modifiers alter the meaning of the basic data type, resulting in an almost-new type.

Type Length Range
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)

Example
To declare a variable named counter which will take integer values between 0 and 100:

char counter;

Example
To declare a variable named e to store the value is the base of the natural logarithm (e=2.718):

float e;

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.

Example
To declare three integer variables x, y, and z:

int x,y,z;

Assignment Statements
To assign a value to a declared variable, the assignment operator ‘=’ is used.

VARIABLE=VALUE;

Example
The following statement will assign the value 2.718 to the e variable declared above:

e=2.718;

Example
The following statement initializes the counter variable to 0.

counter=0;

Learn the Basics of C Programming Language

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:

char counter=0;

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.

Syntax

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

Example
The following program asks the student to enter his bench number, and responds with a greeting.

#include<iostream.h>
void main()
{
	int benchnum;
	cout << "Enter you Bench Number: ";
	cin >> benchnum;
	cout << "Welcome, Student no. " << benchnum << endl;
}

When executed, it should give the following output:
Output
 
In this program, I would like to explain one statement:

cin &gt;&gt; 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.

* * * * * *

Summary

  • 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!!!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -