0% found this document useful (0 votes)
62 views

Introduction To Computer Programming (ICP) : Lecture-7: Data Types

This document discusses different data types in C++ including integer, floating point, character, string, bool, and pointer data types. It provides examples of declaring and defining variables of each data type, and explains how values of each type are stored in memory. Integer types can hold whole numbers, characters represent single characters or small integers, strings are arrays of characters, floating point types store real numbers in a form of scientific notation, and bool variables represent true or false values stored as 0 and 1.

Uploaded by

Mubashar Fareed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Introduction To Computer Programming (ICP) : Lecture-7: Data Types

This document discusses different data types in C++ including integer, floating point, character, string, bool, and pointer data types. It provides examples of declaring and defining variables of each data type, and explains how values of each type are stored in memory. Integer types can hold whole numbers, characters represent single characters or small integers, strings are arrays of characters, floating point types store real numbers in a form of scientific notation, and bool variables represent true or false values stored as 0 and 1.

Uploaded by

Mubashar Fareed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture-7: Data Types

Introduction to Computer Programming


(ICP)
Muhammad Ahmad
Lecturer in CS
[email protected]
Today We will cover

• Data Types
• Different types of Data Types
Data Types
• There are many different types of data
• Variable are classified according to their data types.
• Which determines the kind of information that may be
stored in them.

• Examples of data types are: integer, floating point


unit number, character, string, and pointer
Integer Data Types

1A-
4
Integer Data Types
• Integer variables can hold whole numbers such as 12,
7, and -99.
Defining Variables

• Variables of the same type can be defined


- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
• Variables of different types must be in different
definitions
Integer Types in Program 2-10

This program has three variables: checking,


miles, and days
The char Data Type
The char Data Type

• Used to hold characters or very small integer values


• Usually 1 byte of memory
• Numeric value of character from the character set is
stored in memory:
CODE: MEMORY:
char letter; letter
letter = 'C';
67
Character Literals

• Character literals must be enclosed in single quote marks.


Example:

'A'
Character Literals in Program 2-13
Character Strings

• A series of characters in consecutive memory


locations:
"Hello"
• Stored with the null terminator, \0, at the end:
• Comprised of the characters between the " "

H e l l o \0
The C++ string Class
The C++ string Class
• Special data type supports working with strings
• #include <string>
• Can define string variables in programs:
string firstName, lastName;
• Can receive values with assignment operator:
firstName = "George";
lastName = "Washington";
• Can be displayed via cout
cout << firstName << " " << lastName;
The string class in Program 2-15
Floating-Point Data Types
Floating-Point Data Types

• The floating-point data types are:


float
double
long double

• They can hold real numbers such as:


12.45 -3.8

• Stored in a form similar to scientific notation


• All floating-point numbers are signed
Floating-Point Data Types
Floating-Point Data Types in Program
2-16
The bool Data Type
The bool Data Type

• Represents values that are true or false


• bool variables are stored as small integers
• false is represented by 0, true by 1:
bool allDone = true;
allDone finished
bool finished = false;
1 0
Boolean Variables in Program 2-17
Determining the Size of a Data Type
Determining the Size of a Data Type

The sizeof operator gives the size of any data type or


variable:
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytes\n";
cout << "Variable amount is stored in "
<< sizeof(amount)
<< "bytes\n";
Thank You!

You might also like