Variable:- A variable is a symbol that represents a storage location in the computer’s memory.

The information that is stored in that location is called the value of the variable. One common

way for a variable to obtain a value is by an assignment. This has the syntax.


Variable = expression;

the expression is evaluated and then the resulting value is assigned to the variable. The equal

sign “=” is the assignment operator in C++.i.e a=b+c;

but note that a variable must be declared in C++. The syntax is

type name initializer;

type is one of C++ data types (to be discuss later) name is the name of the variable, and initialize

is an optional initialization clause.

A variable name is any valid identifier. a valid identifier is a sequence of one or more letters,

digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part

of an identifier. Only letters, digits and single underscore characters are valid. In addition,

variable identifiers always have to begin with a letter. They can also begin with an underline

character (_ ), but in some cases these may be reserved for compiler specific keywords or

external identifiers, as well as identifiers containing two successive underscore characters

anywhere. In no case can they begin with a digit.

Examples of variables: name, dept, school, amount, price, age, a, c, x, C_k, etc

Note that C++ is case sensitive- upper case and lower case letters and lower case letters are

different. So b1 and B1 are different identifiers. The length of an identifier is not limited ,

although for some compilers only the first 32 characters of an identifier are significant (the rest

are ignored).


Another rule that you have to consider when inventing your own identifiers is that they cannot

match any keyword of the C++ language nor your compiler’s specific ones, which are reserved

keywords. The standard reserved keywords are:


asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete,

do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto,

if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register,

reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template,

this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void,

volatile, wchar_t, while

Additionally, alternative representations for some operators cannot be used as identifiers since

they are reserved words under some circumstances:

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq




In order to use a variable in C++, we must first declare it specifying which data type we want it

to be. The syntax to declare a new variable is to write the specifier of the desired data type (like

int, bool, float…) followed by a valid variable identifier. For example:

int age;

char dept;

float scholarship;

If you are going to declare more than one variable of the same type, you can declare all of them

in a single statement by separating their identifiers with commas. For example:

int x, y, z;

The integer data types char, short, long and int can be either signed or unsigned depending on

the range of numbers needed to be represented. Signed types can represent both positive and

negative values, whereas unsigned types can only represent positive values (and zero). This can


be specified by using either the specifier signed or the specifier unsigned before the type name.


For example:

unsigned short int classlevel;

signed int accountbalance;

By default, if we do not specify either signed or unsigned most compiler settings will assume the

type to be signed, therefore instead of the second declaration above we could have written:

int accountbalance;

with exactly the same meaning (with or without the keyword signed)

An exception to this general rule is the char type, which exists by itself and is considered a

different fundamental data type from signed char and unsigned char, thought to store characters.

You should use either signed or unsigned if you intend to store numerical values in a char-sized


short and long can be used alone as type specifiers. In this case, they refer to their respective

integer fundamental types: short is equivalent to short int and long is equivalent to long int. The

following two variable declarations are equivalent:

Short year;

Short int year;

Finally, signed and unsigned may also be used as standalone type specifiers, meaning the same

as signed int and unsigned int respectively. The following two declarations are equivalent:

unsigned nextyear;

signed int nextyear;




All the variables that we intend to use in a program must have been declared with its type

specifier and their scope begin where they are declared.

A variable can be either of global or local scope. A global variable is a variable declared in the

main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.

The scope of local variables is limited to the block enclosed in braces ({}) where they are

declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function. In the example above, this means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and vice versa.




When declaring a regular local variable, its value is by default undetermined. But you may want

a variable to store a concrete value at the same moment that it is declared. In order to do that, you

can initialize the variable. There are two ways to do this in C++:


The first one, known as c-like initialization, is done by appending an equal sign followed by the

value to which the variable will be initialized:

type identifier = initial_value ;

For example, if we want to declare an int variable called c initialized with a value of 2 at the

moment in which it is declared, we could write:

int c = 2;

The other way to initialize variables, known as constructor initialization, is done by enclosing

the initial value between parentheses (()):

type identifier (initial_value) ;

For example:

int c (2) ;

Both ways of initializing variables are valid and equivalent in C++.




Variables that can store non-numerical values that are longer than one single character are known

as strings.

The C++ language library provides support for strings through the standard string class. This is

not a fundamental type, but it behaves in a similar way as fundamental types do in its most basic


A first difference with fundamental data types is that in order to declare and use objects

(variables) of this type we need to include an additional header file in our source code: <string>

and have access to the std namespace.

// my first string

#include <iostream>


#include <string>

using namespace std;

int main ( )


string dept = “Mathematical Sciences department”;

cout << dept;

return 0;


As you may see in the previous example, strings can be initialized with any valid string literal

just like numerical type variables can be initialized to any valid numerical literal. Both

initialization formats are valid with strings:

string dept = “Mathematical Science Department”;

string dept (“Mathematical Science Department”);

Strings can also perform all the other basic operations that fundamental data types can, like being

declared without an initial value and being assigned values during execution:

// my first string

#include <iostream>

#include <string>

using namespace std;

int main ()


string dept;

dept = ” Welcome to mathematical sciences and IT”;

cout << dept << endl;

dept = “Welcome to Mathematical Sciences and IT,Temporary site”;

cout << dept << endl;

return 0;





A constant is any expression that has a fixed value. They are divided

Literals are the most obvious kind of constants. Literals are text representation and/ or number

that do not change. They are used to express particular values within the source code of a

program. We have already used these previously to give concrete values to variables or to

express messages we wanted our programs to print out.

Literal constants or constants are divided into Integer numbers, Floating-Point Numbers,

Characters, Strings and Boolean Values.


Integer Numbers

They are numerical constants that identify integer decimal values. Notice that to express a

numerical constant we do not have to write quotes (“) nor any special character. There is no

doubt that it is a constant: whenever we write 1776 in a program, we will be referring to the

value 1776.

In addition to decimal numbers (those that we already know), C++ allows the use of octal

numbers (base 8) and hexadecimal numbers (base 16) as literal constants. If we want to express

an octal number we have to precede it with a 0 (a zero character). And in order to express a

hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the

following literal constants are all equivalent to each other:

75 0113 0x4b

All of these represent the same number: 75 (seventy-five) expressed as a base-10 numeral, octal

numeral and hexadecimal numeral, respectively.



Floating Point Numbers

They express numbers with decimals and/or exponents. They can include either a decimal point,

an e character (that expresses “by ten at the Xth height”, where X is an integer value that follows

the e character), or both a decimal point and an e character:

3.14159 // 3.14159

6.02e23 // 6.02 * 1023

1.6e-19 // 1.6 *10-19

3.0 // 3.0

These are four valid numbers with decimals expressed in C++. The first number is PI, the second

one is the number of Avogadro, the third is the electric charge of an electron (an extremely small

number) -all of them approximated- and the last one is the number three expressed as a floatingpoint

numeric literal.

The default type for floating point literals is double. If you explicitly want to express a float or a

long double numerical literal, you can use the f or l suffixes respectively, but some do accept

float or long double without the letter f or l.

3.14159L // long double

6.02e23f // float


Character and string

There also exist non-numerical constants, like:



“Hello world”

“How do you do?”

The first two expressions represent single character constants, and the following two represent

string literals of several characters. Notice that to represent a single character we enclose it


between single quotes (‘) and to express a string (which generally consists of more than one

character) we enclose it between double quotes (“).

When writing both single character and string literals, it is necessary to put the quotation marks

surrounding them to distinguish them from possible variable identifiers or reserved keywords.

Notice the difference between these two expressions:



x alone refer to a variable whose identifier is x, whereas ‘x’ (enclosed within single quotation

marks) refer to the character constant ‘x’.

Note that String literals can extend to more than a single line of code by putting a backslash sign

(\) at the end of each unfinished line.

“string expressed in \

two lines”

About Prince Idowu Oyewumi

FULL NAME : PRINCE MUHAMMED S. OYEWUMI BIO.: Graphics Designer, I.T Expert & Photograaphy Expert Edu Qual. Bsc, Diploma, Nim Follow us Twitter: @mdee_coded Facebook: @https://www.facebook.com/pxclusive/ Instagram: @mdee_Cool Tel.: +2348085570405

Check Also

Download SpeedLight Browser.apk

loading... SpeedLight is a Web browser that is smaller, faster, and in some ways more …

Leave a Reply

Your email address will not be published. Required fields are marked *