Professional Documents
Culture Documents
Lec 04 Datatypes in C++
Lec 04 Datatypes in C++
Principles
Data Types in C++
Programming
26th March, 2024
2
Data Types Introduction
• It is a classification of specific types of data by a
—Certain value
—Certain types of Mathematical
—Logical Operations
• Depends on the command of the programming
language such as letters, lowercase, uppercase,
numbers, punctuation, and many more.
• Data types are Essential for good programming:
— Determine the type, size, and behaviour of variables
— Enable proper data manipulation
3
C++ Data Types
• Three categories of C++ Data
Data Types
User
Primitive Derived
defined
4
C++ Data Types
• Primitive Data Type • User-defined Data Type
—Integer —Class
—Character —Structure
—Boolean —Union
—Floating Point —Enumeration
• Derived Data Type —Typedef defined
—Function Datatype
—Array
—Pointer
—Reference
5
C++ Primitive Data Types
• Computers are designed to process Data (users,
devices)
• Data is represented as a sequence of bits in memory
• The sequence of bits may represent
— An instruction,
— A numeric value,
— A character,
— A portion of an image,
— A portion of Voice/Video data
6
C++ Primitive Data Types
• Three categories of simple data
7
C++ Primitive Data Types
Primitive types
integral floating
unsigned
Primitive Data Types in C++
• Integral Type
— Represent whole numbers and their negatives
— Declared as int, short, or long
• Character Type
— Represent single characters
— Declared as char
• Boolean Type
— Has two values true/false
— Declared as bool
• Floating Types
— Represent real numbers with a decimal point
— Declared as float, or double
— Scientific notation where e (or E) stand for “times 10 to
the ” (.55-e6)
Samples of C++ Data Values
int sample values
4578 -4578 0
bool values
true false
12
bool Data Type
• bool type
13
char Data Type
14
Floating-Point Data Types
• C++ uses scientific notation to represent real
numbers (floating-point notation)
15
Floating-Point Data Types
(continued)
• float: represents any real number
—Range: -3.4E+38 to 3.4E+38
• Memory allocated for the float type is 4 bytes
• double: represents any real number
—Range: -1.7E+308 to 1.7E+308
• Memory allocated for double type is 8 bytes
• On most newer compilers, data types double
and long double are same
16
C++ Data Type String
• A string is a sequence of characters
enclosed in double quotes
• Boolean
true
false
• Pointer
– nullptr ← preferred literal
–0
– NULL (must #include cstdlib)
Variables