C++ Primary Data types
SESSION 3
Data Types
Data types specify the type of data a
variable can hold.
Helps the compiler allocate memory and
interpret values.
Categories of Data Types:
Primary (Fundamental)
Derived
User-defined
[Link] 2
Primary (Fundamental) Data Types
1. int – Integer
Used for: Storing whole numbers without any decimal
point.
Memory size: Typically 4 bytes (can store -
2,147,483,648 to 2,147,483,647)
2. char – Character
Used for: Storing a single character.
Memory size: 1 byte (stores ASCII characters)
Stored using: Single quotes ' '
[Link] 3
.3. float – Floating Point
Used for: Storing decimal numbers (single precision).
Memory size: 4 bytes
4. double – Double Precision Floating Point
Used for: Storing larger and more precise decimal
numbers.
Memory size: 8 bytes
5. void – No Value
Used for: Indicating no data type.
Commonly used in:
Functions that don’t return any value.
[Link] 4
.
Data Type Description Example Value Memory Size
int Whole numbers 42, -5 4 bytes
Single
char 'A', '9' 1 byte
characters
Decimal
float numbers (less 3.14f, -0.5f 4 bytes
precise)
Decimal (more
double 6.022e23 8 bytes
precise)
No value (used
void void 0 bytes
in functions)
[Link] 5
Type Modifiers
Type modifiers change the size or range of
the base data types like int and char.
🔸 Available Modifiers:
short
long
signed
unsigned
You can combine them with primary types like
int and char.
[Link] 6
.
🔸 short int
Smaller range than int.
Typically uses 2 bytes.
🔸 long int
Larger range than int.
Typically uses 4 to 8 bytes depending on the system.
🔸 unsigned int
Only stores non-negative numbers.
Doubles the upper limit of positive values.
🔸 signed int
Default modifier (can store both positive and negative values).
[Link] 7
Declaring Variables
🔸 What is a Variable?
A named storage location that holds data
which can be changed during program
execution.
Syntax:
data_type variable_name;
Eg: int age = 25;
[Link] 8
Constants
🔸 What is a Constant?
A value that cannot change during program execution.
🔸 How to Define a Constant:
Using const keyword:
const float PI = 3.14;
Using #define directive:
#define MAX_SCORE 100
[Link] 9
Types of Constants
1. Integer Constants
Whole numbers (positive or negative).
No decimal.
Eg: const int DAYS = 7;
2. Floating-Point Constants
Numbers with decimal point.
Eg: const float PI = 3.1415;
[Link] 10
.3. Character Constants
A character in single quotes.
Eg: const char GRADE = 'A';
4. String Constants
A sequence of characters inside double
quotes.
Eg: const char message[] = "Welcome to C++!";
[Link] 11
END
[Link] 12