Introduction to C++
VARIABLES ,DATA TYPES
AND CONSTANTS
Variables
 A variable is a memory location that is used to store
values .
 To understand what this means let us take an
example.
 Consider the memory of the computer to contain of
equal sized cells capable of storing data.
 These cells have unique addresses. Since we store
data in these we need to refer to them in our program.
 It is humanly impossible to remember all the
addresses
Memory locations having unique memory addresses
So We can name these Memory locations with some user defined names(identifier) like
we have named this one as age.
Here age is the name given to memory location AFXX011 and is called the variable age.
We shall see how to declare a variable discussing data types
age
How do we use Variables ?
 In order to be able to use the memory location as a
variable we need to declare it ie allocate memory to it.
 A variable can be declared using a data type and a
valid identifier.
 Let us first discuss data types.
What is a Data Type?
A data type defines a set of values
and the operations that can be
performed on that data.
Data Operations Data Type
Why do we need a data
type?
 As all of you must be aware that a computer is just a machine. It cannot
by itself distinguish between various types of Data. This means that it
cannot distinguish between the number 20 and the letter ‘A’ or the word
“good” . For the computer all of this is just a piece of data.
 It is the programmer who must tell the computer that 20 is a number, ‘A’
is a character and ‘good’ is a word. How do we do it?
 By using data types we can classify the different data for the computer
so that it can be stored and processed in a certain manner.
C++ Data Types : Categories
C++ Data
Types
In-Built
int, char,
float, void
User
Defined
Classes ,
structures
Derived
Arrays
The C++ data types can be categorized as
C++ Data Types: In-built
 C++ supports four basic data types :
Basic Data
types
int char float void
void
The void type has no values and no
operations. In other words, both the set
of values and the set of operations are
empty. Although this might seem unusual,
we will see later that it is a very
useful data type.
Characteristics of data types
 All data types have a certain size associated with
them. This essentially means that data of each type
has to be stored in a certain no of bytes.
 Each data type has a range of permissible values
associated with it which is also its domain.
 All data type have some modifiers to accommodate
various ranges for eg int data type has short long
int data type
Variables declared as integers are capable of storing whole numbers These
are numbers without decimal point.
There are three variants of integer type. These differ in size. The following is
a table showing the size and domain of integer data types.
Type modifier Size
(byte
s)
Min value Max value Sample data
(To Store)
short
int
Signed
Unsigned
2 -32768
0
32767
65535
Marks
Age
int signed
unsigned
2 -32768
0
32767
65535
long
int
signed
unsigned
4 -2,147,483,648
0
2,147,483,647
4,294,967,295
Population
Floating Point
A floating-point variable is capable of storing
a number with a fractional part. The C++
language supports three different sizes of
floating-point: float, double and long double.
Type Size
(byt
es)
Min value Max value Sample
data
(To Store)
float 4 3.4 E -38 3.4 E 38 Average
double 6 1.7 E -308 1.7 E 308 Huge
fractional
calculation
Long
double
10 3.4 E -4932 3.4 E 4932 Stellar
distances
Char data type
A char variable is capable of storing any
character on the keyboard. The ASCII code of
the character is stored.eg the symbol ‘+’, the
letter ‘A’ etc.
Type Size
(byt
es)
Domain Sample
data
(To Store)
char 1 Any character on the keyboard Symbol
operator
Variable declarations
A variable declaration specifies the type and the name of the
variable.
A variable has a type and it can contain only values of that type.
For example, a variable of the type int can only hold
integer values.
Syntax:
data type identifier ; //declaration
data type identifier = value; // initialization
Examples:
int age;
int no_of_books = 45;
char letter= 'y';
double price = 2493.14;
float temp = -24.5;
Variable declarations
 When initializing a constant or a variable of char type,
or when changing the value of a variable of char type,
the value is enclosed in single quotation marks.
Examples:
const char star = '*';
char letter = ‘D';
Declaration vs Initialization
 When a variable is given a value at the time of
declaration itself this is known as initialization
int num;
int num=89
 Variables are not automatically initialized. For
example, after declaration
int sum;
the value of the variable sum can be anything
(garbage).
 Thus, it is good practice to initialize variables when
they are declared.
Constant declarations
 Constants are used to store values that never change
during the program execution.
 Using constants makes programs more readable and
maintainable.
Syntax:
const data type identifier = value;
Examples:
const double rate = 7.8;
const int x= 45;

More Related Content

PPT
C++ data types
PPT
Data Handling
PPTX
Data Types and variables in C++.pptx
PPTX
2. Variables and Data Types in C++ proramming.pptx
PPTX
Csc240 -lecture_4
PPTX
Data types
PPTX
Concept of c data types
PPTX
Data types
C++ data types
Data Handling
Data Types and variables in C++.pptx
2. Variables and Data Types in C++ proramming.pptx
Csc240 -lecture_4
Data types
Concept of c data types
Data types

Similar to variablesfinal-170820055428 data type results (20)

PPTX
Concept Of C++ Data Types
 
PDF
C++ PPT IN NUMERICAL METHOD FOR M.Sc PHYSICS
DOCX
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
PDF
Data types in c++
PPTX
What are variables and keywords in c++
PPTX
Variables & Data Types.pptx
DOC
Data type
PPTX
Datatype in c++ unit 3 -topic 2
PPTX
COM1407: Variables and Data Types
PPT
CHAPTER-2.ppt
PPT
5-Lec - Datatypes.ppt
PPTX
Ch-3(b) - Variables and Data types in C++.pptx
PDF
array 2 (1)_merged.pdf
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PDF
POLITEKNIK MALAYSIA
PPTX
data types in C programming
PDF
cassignmentii-170424105623.pdf
PDF
Keywords, identifiers ,datatypes in C++
PPT
C language Unit 2 Slides, UPTU C language
Concept Of C++ Data Types
 
C++ PPT IN NUMERICAL METHOD FOR M.Sc PHYSICS
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
Data types in c++
What are variables and keywords in c++
Variables & Data Types.pptx
Data type
Datatype in c++ unit 3 -topic 2
COM1407: Variables and Data Types
CHAPTER-2.ppt
5-Lec - Datatypes.ppt
Ch-3(b) - Variables and Data types in C++.pptx
array 2 (1)_merged.pdf
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
POLITEKNIK MALAYSIA
data types in C programming
cassignmentii-170424105623.pdf
Keywords, identifiers ,datatypes in C++
C language Unit 2 Slides, UPTU C language
Ad

Recently uploaded (20)

PPTX
SUMMARY OF EAR, NOSE AND THROAT DISORDERS INCLUDING DEFINITION, CAUSES, CLINI...
PPTX
FORENSIC MEDICINE and branches of forensic medicine.pptx
PPTX
Indications for Surgical Delivery...pptx
PPTX
PSYCHIATRIC SEQUALAE OF HEAD INJURY.pptx
PPTX
Bacteriology and purification of water supply
PDF
neonatology-for-nurses.pdfggghjjkkkkkkjhhg
PPTX
المحاضرة الثالثة Urosurgery (Inflammation).pptx
PPTX
Communicating with the FDA During an Inspection -August 26, 2025 - GMP.pptx
PPTX
LIVER DIORDERS OF PREGNANCY in detail PPT.pptx
PPTX
This book is about some common childhood
PPTX
Approach to Abdominal trauma Gemme(COMMENT).pptx
PDF
Cranial nerve palsies (I-XII) - AMBOSS.pdf
PPT
BONE-TYPES,CLASSIFICATION,HISTOLOGY,FRACTURE,
PDF
Diabetes mellitus - AMBOSS.pdf
PPTX
ENT-DISORDERS ( ent for nursing ). (1).p
PDF
Nematodes - by Sanjan PV 20-52.pdf based on all aspects
PPTX
Acute Abdomen and its management updates.pptx
PDF
Biochemistry And Nutrition For Bsc (Nursing).pdf
PDF
Integrating Traditional Medicine with Modern Engineering Solutions (www.kiu....
PPT
ANTI-HYPERTENSIVE PHARMACOLOGY Department.ppt
SUMMARY OF EAR, NOSE AND THROAT DISORDERS INCLUDING DEFINITION, CAUSES, CLINI...
FORENSIC MEDICINE and branches of forensic medicine.pptx
Indications for Surgical Delivery...pptx
PSYCHIATRIC SEQUALAE OF HEAD INJURY.pptx
Bacteriology and purification of water supply
neonatology-for-nurses.pdfggghjjkkkkkkjhhg
المحاضرة الثالثة Urosurgery (Inflammation).pptx
Communicating with the FDA During an Inspection -August 26, 2025 - GMP.pptx
LIVER DIORDERS OF PREGNANCY in detail PPT.pptx
This book is about some common childhood
Approach to Abdominal trauma Gemme(COMMENT).pptx
Cranial nerve palsies (I-XII) - AMBOSS.pdf
BONE-TYPES,CLASSIFICATION,HISTOLOGY,FRACTURE,
Diabetes mellitus - AMBOSS.pdf
ENT-DISORDERS ( ent for nursing ). (1).p
Nematodes - by Sanjan PV 20-52.pdf based on all aspects
Acute Abdomen and its management updates.pptx
Biochemistry And Nutrition For Bsc (Nursing).pdf
Integrating Traditional Medicine with Modern Engineering Solutions (www.kiu....
ANTI-HYPERTENSIVE PHARMACOLOGY Department.ppt
Ad

variablesfinal-170820055428 data type results

  • 1. Introduction to C++ VARIABLES ,DATA TYPES AND CONSTANTS
  • 2. Variables  A variable is a memory location that is used to store values .  To understand what this means let us take an example.  Consider the memory of the computer to contain of equal sized cells capable of storing data.  These cells have unique addresses. Since we store data in these we need to refer to them in our program.  It is humanly impossible to remember all the addresses
  • 3. Memory locations having unique memory addresses
  • 4. So We can name these Memory locations with some user defined names(identifier) like we have named this one as age. Here age is the name given to memory location AFXX011 and is called the variable age. We shall see how to declare a variable discussing data types age
  • 5. How do we use Variables ?  In order to be able to use the memory location as a variable we need to declare it ie allocate memory to it.  A variable can be declared using a data type and a valid identifier.  Let us first discuss data types.
  • 6. What is a Data Type? A data type defines a set of values and the operations that can be performed on that data. Data Operations Data Type
  • 7. Why do we need a data type?  As all of you must be aware that a computer is just a machine. It cannot by itself distinguish between various types of Data. This means that it cannot distinguish between the number 20 and the letter ‘A’ or the word “good” . For the computer all of this is just a piece of data.  It is the programmer who must tell the computer that 20 is a number, ‘A’ is a character and ‘good’ is a word. How do we do it?  By using data types we can classify the different data for the computer so that it can be stored and processed in a certain manner.
  • 8. C++ Data Types : Categories C++ Data Types In-Built int, char, float, void User Defined Classes , structures Derived Arrays The C++ data types can be categorized as
  • 9. C++ Data Types: In-built  C++ supports four basic data types : Basic Data types int char float void
  • 10. void The void type has no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.
  • 11. Characteristics of data types  All data types have a certain size associated with them. This essentially means that data of each type has to be stored in a certain no of bytes.  Each data type has a range of permissible values associated with it which is also its domain.  All data type have some modifiers to accommodate various ranges for eg int data type has short long
  • 12. int data type Variables declared as integers are capable of storing whole numbers These are numbers without decimal point. There are three variants of integer type. These differ in size. The following is a table showing the size and domain of integer data types. Type modifier Size (byte s) Min value Max value Sample data (To Store) short int Signed Unsigned 2 -32768 0 32767 65535 Marks Age int signed unsigned 2 -32768 0 32767 65535 long int signed unsigned 4 -2,147,483,648 0 2,147,483,647 4,294,967,295 Population
  • 13. Floating Point A floating-point variable is capable of storing a number with a fractional part. The C++ language supports three different sizes of floating-point: float, double and long double. Type Size (byt es) Min value Max value Sample data (To Store) float 4 3.4 E -38 3.4 E 38 Average double 6 1.7 E -308 1.7 E 308 Huge fractional calculation Long double 10 3.4 E -4932 3.4 E 4932 Stellar distances
  • 14. Char data type A char variable is capable of storing any character on the keyboard. The ASCII code of the character is stored.eg the symbol ‘+’, the letter ‘A’ etc. Type Size (byt es) Domain Sample data (To Store) char 1 Any character on the keyboard Symbol operator
  • 15. Variable declarations A variable declaration specifies the type and the name of the variable. A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values. Syntax: data type identifier ; //declaration data type identifier = value; // initialization Examples: int age; int no_of_books = 45; char letter= 'y'; double price = 2493.14; float temp = -24.5;
  • 16. Variable declarations  When initializing a constant or a variable of char type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks. Examples: const char star = '*'; char letter = ‘D';
  • 17. Declaration vs Initialization  When a variable is given a value at the time of declaration itself this is known as initialization int num; int num=89  Variables are not automatically initialized. For example, after declaration int sum; the value of the variable sum can be anything (garbage).  Thus, it is good practice to initialize variables when they are declared.
  • 18. Constant declarations  Constants are used to store values that never change during the program execution.  Using constants makes programs more readable and maintainable. Syntax: const data type identifier = value; Examples: const double rate = 7.8; const int x= 45;