Variables
in C++
In any programming we typically do lots
of calculations. The result of these calculations is stored in computers
memory. Like human memory, the computers memory also consists of cells. The
calculated values are stored in these memory cells. To make the retrieval and
usage of these values easy these memory cells are given names. Since the value
stored in each location may change the names given to these locations are
called variable names.
Data
Types
When using variable we store the data in
computers memory, we need to define its type that is format that what we want
to store in. So, that computer can handle the data actions that we will perform
after storing. There are following types of data:
(i)
Char (Character)
It is smallest
data type in C++. It allocates 1 bytes (8 bits) memory in 32 bits computers to
store the char value within the range
(a) Signed char (-128 to 127)
(b) Unsigned char (0 to 255)
(ii) Short
It allocates 2
bytes (16 bits) memory in 32 bits computers to store the values within the
range
(a) Signed short (-32768 to 32767)
(b) Unsigned short (0 to 65535)
(iii) Long
It allocates 4
bytes (32 bits) memory in 32 bits computers to store the values within the
range
(a) Signed long (-2147483648 to 2147483647)
(b) Unsigned long (0 to 4294967295)
(iv) Int (Integer)
It allocates 4
bytes (32 bits) memory in 32 bits computers to store the integer values within
the range
(a) Signed long (-2147483648 to 2147483647)
(b) Unsigned long (0 to 4294967295)
Note: Only int
changes the allocated space as per computer (32 bits or 64 bits computers)
(v) Float (Floating Point)
It allocates 4
bytes (32 bits) of memory in 32 bits computers to store the floating numbers of
7 digits (excluding decimal).
(vi) Double
It allocates 8
bytes (64 bits) of memory in 32 bits computers to store the floating numbers of
15 digits (excluding decimal).
(vii) Long Double
It allocates 10
bytes (80 bits) of memory in 32 bits computers to store the floating numbers of
19 digits (excluding decimal).
(viii) Bool
It is short of
Boolean. It can take values in true/false. It allocates 1 byte (8 bits) of
memory. This data type is only compatible with ANSI C compilers.
(ix) wchar_t
It is short of
wide character. This data type is designed as to handle the international
marked characters. It allocates 2 bytes (16 bits) of memory. This data type is
also only compatible with ANSI C compilers.
Declaration
of Variables
To use variables in C++ programming
language we have to declare it first. To declare any variable first we use its
data type name and then write the variable name. Variable name should be
communicable and understandable that is variable name can describe its nature
and properties. For example, to store student mark we should use std_mark and
to store student address use std_add. Variable name cannot consist space and
also cannot start with numeric values. Foe example
|
Right
|
Wrong
|
|
int x;
int mymobnum;
int mark2;
|
int 1;
int 2mymobnum;
int 2mark;
|
To declare three variables we can write
int
x, y, z;
instead of writing
int
x;
int
y;
int
z;
Initialisation
of variables
It is the process to assign the value in
the variable, which is declared we can initialise the value at the time of
variable declaration also. For example,
int
x=10;
OR
int
x;
x=10;
One another way to declare the variable
and initialise the value in it is to use the open and close parenthesis. For
example,
int
x(10);
Scope
of variables
To use any variable in C++ programming,
variable should be declared first. Without declaration of variable we cannot
use in program. There are two types of variable scopes.
(i)
Local Variables
The variable
which is declared for local use like in any function, loop or even only in main
function is known as local variables.
(ii)
Global Variables
The variable
which is declared for global use in program is known as global variables.
Global variables can be used anywhere in program and no need to declare
everywhere.
Example on scope of variables:
#include<iostream.h>
int sum; //global variable
int num1, num2; //global variable
main()
{
int multi; //local
variable
multi=num1*num2;
cout<<multi;
}
Constants
It is an expression that has fixed value.
They are divided into many categories:
(i)
Integer Numbers
Numbers within
the range -2147483648 to 2147483647 are constant integers. The numbers like
octal and hexadecimal are also integer constants.
(ii)
Floating Point Numbers
The number
which has exponents is known as floating point numbers. For example,
3.14159
6.02e23=6.02*1023
1.6e-19=1.6*10-19
(iii) Character
Strings
The numbers which has nun-numerical value is known as string.
Strings are always covered with single quote or double quote. For example,
'z'
'p'
"Hellow Guys"
"Hello, how are you?"
(iv) Escape Sequence
The character which is only used to work with strings are known as
escape sequence. Escape sequences are always used with inverted slash (\). For
example,
|
Escape
Sequences
|
Description
|
|
\n
|
New line
|
|
\r
|
Carriage return
|
|
\t
|
Tab
|
|
\v
|
Vertical
tabulation
|
|
\b
|
Backspace
|
|
\f
|
Page feed
|
|
\a
|
Alarm
|
|
\'
|
Single quote
|
|
\"
|
Double quote
|
|
\?
|
Question mark
|
|
\\
|
Single slash
|
Usages of escape
sequences:
printf("Hellow\nhow are you?");
printf("Are you good\?");
(v) Defined Constants
For to use any variable global we use the defined constants. Even in
another linked file usages the defined constants. For example,
#define PI 3.14
#define newline '\n'
#define height 30
(vi) Declared Constants
One another way to create any variable constant permanently is to
use const prefix before. For example,
const int height=100;
const int salary=12000;
Note: Continue in next part.
HAVE A HAPPY CODING!