Constants, Data Types and Variables
Constants, Data Types and Variables
Computer Science
Constants
A constant is the quantity that does not change. This
quantity can be stored at a locations in the memory of
the computer.
A variable can be consider as a name given to the
location in memory where this constant is stored.
Three Basic Types of Constant
Integer Constants
Real Constants
String Constants
Computer Science
Integer Constant
Rules for Constructing Integer Constant
An integer constant must have at least one digit.
It must not have a decimal point.
It could be either negative or positive.
If no sign precedes an integer constant it is assumed
to be positive.
No commas or blanks are allowed in integer constant.
Example
426
-777
+20000
Computer Science
Character Constant
Rules for Constructing Character Constants
A character constant is either a single alphabet, a
single digit or a single special symbol enclosed within
Single inverted commas.
The maximum length of a character constant can be 1
character.
Example
a
5
=
G
Computer Science
Data Types
Computer Science
Variables
Variable is a location in memory, referenced by an
identifier, that contain a data value that can be changed.
Identifier: name given to a variable is known as identifier.
Rules for writing Identifier
The first character must be letter or underscore ( _ ).
You can use upper and lowercase letters and digits
from 1 to 9.
Identifier can be as long as you like but only the first
250 character are recognizable in C++ Compiler.
Computer Science
Integer Variables
Integer variables represent integer numbers like 1,
30,000 and -45.
Integer variables do not store real numbers.
Defining integer variables
The amount of memory occupied by integer types is
system dependent.
On 32-bit system like windows 98/XP an integer
occupies 4 bytes of memory.
This allows an integer variable to hold numbers in the
range from -2,147,483,648 to 2,147,483,647.
Computer Science
//define var1
//define var2, var3
//assign value to var1
//assign value to var2
//displaying the sum of var1 + 10
Character Variables
Type char stores integer that range in value from -128 to 127.
Variables of this type occupy only 1 byte of memory.
Character variables are sometime use to store number but
they are much more commonly used to store ASCII
characters.
Character Constants
Character constants use single quotation marks around a
character, like a and b.
When the C++ compiler encounters such a character
constant, it translates it into the corresponding ASCII code.
Computer Science
Computer Science
float
Type float stores numbers in the range of about
3.4 10 38 to 3.4 1038 .
It occupy 4 bytes of memory.
Double and Long Double:
Double and long double, are similar to float
except that they require more memory space
and provide a wider range of values and more
precision.
Computer Science
float
#include<iostream.h>
void main ( )
{
float rad, area;
float PI =3.14;
cout << Enter radius of circle;
cin >> rad;
area = PI * rad * rad;
cout <<Area is << area << endl;
}
Computer Science
Low Range
High range
Digits of
precision
Bytes of
Memory
Char
-128
127
n/a
short
-32,768
32,767
n/a
int
-2,147,483,648
2,147,483,647
n/a
long
-2,147,483,648
-2,147,483,648
n/a
float
3.4 10 38 3.4 10 38
1.7 10 308 1.7 10 308
15
double
Computer Science