Vous êtes sur la page 1sur 14

VARIABLES

A Variable is symbolic name that can be given


different values. Variables are stored in particular
places in the computer ‘s memory. When a
variable is given a value, that value is placed in
the memory space occupied by the variable.
Rules for Naming Variables
• The first character must be a letter or underscore_ .
• Names can be as long as you like, but first 32
characters will be recognized.
• Both Upper and lower case letters can be used, but
compiler distinguishes between them.
• Digits can be used.
• Spaces cannot be used as a part of variable name.
• Variables must be defined before using them.
• Variables can be defined throughout a program, not
just at the beginning.
ASSIGNMENT STATEMENT

The statements
Val1 = 10;
Val2 = Val1 + 5;
Assigns values to the two variables. The
equal sign =, causes the value on R.H.S to
be assigned to the variable on the L.H.S.

In the first line, Val1 is given the value 10.


In the second line, the plus sign +, adds the
value of Val1 and 5. The result of this
addition is then assigned to Val2.
INTEGER VARIABLES

• Integer variables represent integer numbers


like 1, 12, 14000 etc, i.e. numbers having no
fractional part.
• In C++, the most commonly used is type int.
• It requires four bytes of memory space.
• It holds numbers in the range –2,147,483,648
to 2,147,483,647.
//intvars.cpp
//demonstrates integer variables
#include <iostream.h>

void main ( )
{
int var1, var2; // defines two variables
var1 = 20; // assign value
var2 = var1 + 10;
cout << “var1 + 10 is “; // output text
cout << var2; // output value
}
CHARACTER VARIABLES

• Another integer variable type is char.


• This type stores integer in the range of –128 to
127.
•It occupies one byte of memory.
•Character variables are mostly used to store ASCII
( American Standard Code for Information
Interchange) characters.
Character Constants use single quotation marks
around a character (unlike double quotes around a
string).
// characters.cpp
# include <iostream.h>

void main ( )
{
char var1 = ‘A’, var2 = ‘B’, var3 = ‘ ’;
cout << var1 <<var3 << var2;
}
INPUT with cin

# include <iostream.h>
void main ( )
{
int ftemp;
cout << “Enter temperature in
fahrenheit : “;
cin >> ftemp;
int ctemp = (ftemp-32) * 5 / 9;
cout << “Equivalent in Celsius is : “ <<
ctemp ;
}
INPUT with cin
•The cin statement causes the program to wait for
the user to type in a number.
•The keyword cin is an object in C++ to correspond
to the standard input stream. This stream represents
data coming from the keyboard.
•The >> is the extraction or get from operator. It
takes the value from the stream object (i.e. cin
object) on L.H.S and places it in the variable on its
R.H.S.
Note: The extraction (<<) and insertion (>>)
operators can be used repeatedly in a statement.
Floating Point Variables

• A floating point variable is used when a


fraction component is required.
• There are three kinds of floating point
variables in C++.
• type float, type double and type long
double.
Summary of Variable Types
type bytes of memory
Range
char 1 -128 to 127
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647
long 4 -2,147,483,648 to 2,147,483,647
float 4 3.4 x E-38 to 3.4 x E+38
double 8 1.7 x E-308 to 1.7x E+308
long double 10 3.4x E-4932 to 3.4x E-4932
Modifiers
• A modifier is used to alter the meanings of a
base type so that it fits more precisely as
required. C++ type modifiers are signed,
unsigned, long, and short.
• The unsigned types are used when the
quantities represented are always positive.
This allows them to represent numbers twice
as big as the signed type, i.e. to start at 0 and
include only positive numbers.
Unsigned Integer Type

• unsigned char 0 to 255 1


• unsigned short0 to 65,535 2
• unsigned int 0 to 4,294,967,295 4
• unsigned long 0 to 4,294,967,295 4
ESCAPE SEQUENCE or
BACKSLASH CHARACTER CONSTANT

In C++, backslash (\) causes an “escape” from the


normal way, the characters are interpreted.
Symbol Meaning Symbol Meaning
\a beep \” Double quotes
\r return \’ Single quote
\n newline \? ?
\t tab \\ \

Vous aimerez peut-être aussi