Sei sulla pagina 1di 15

++Programming in C

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

Floating Point Constant / Real


Constants
Rules for Constructing Real Constant
A real constant must have at least one digit.
It must have a decimal point.
It could be either positive or negative
Default sign is always positive.
No commas or blanks are allowed in real constant.
Example
+300.25
221.005
-19845.0
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

A computer Program operates on data and produce an output.


In C++, each data must be of specific data type.
The data type determines how the data is represented in the
computer and kind of processing the computer can perform on it.
There are two kind of data types.
Built-in data type
User define data type

Built-in Data Type


The are already defined by C++.
int ( For working with integer numbers)
float (For working with real numbers having decimal points)
char ( for working with character data)

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

Integer Variables Example


#include<iostream.h>
void main ( )
{
int var1;
int var2, var3;
var1 = 20;
var2 = var1 + 10;
cout<<Result =;
cout<<var2<< endl;
}
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

Character Variable Example


#include<iostream.h>
void main ( )
{
char charvar1 = A;
char charvar2 = \t;
cout << charvar1;
cout << charvar2;
charvar1 = B;
cout << charvar1;
}
Computer Science

//define char variable


// display character
//reassigning charvar1 to B

Floating point Types


Floating point variables represent number
with a decimal place e.g. 3.14787 or 10.2
There are three kind of floating point
variables in C++
float
double
long double

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

Variable Type Summary


Keyword

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

Potrebbero piacerti anche