-
Hajipur, Bihar, 844101
Data types in C++ define the kind of data a variable can hold and how much memory it occupies. Every variable you declare must have a data type because C++ is a statically typed language, meaning data types are checked at compile time.
In simple terms, a data type tells the compiler what type of value a variable will store — an integer, a character, a floating-point number, or a boolean. Choosing the correct data type is important for both performance and accuracy of your program.
C++ provides different kinds of data types, which can be grouped into three main categories:
Primitive (Basic) Data Types
Derived Data Types
User-defined Data Types
Let’s understand each category in detail.
These are the fundamental data types built into the language. They include:
| Data Type | Description | Size (in bytes) | Example |
|---|---|---|---|
int |
Stores whole numbers (no decimal) | 4 | int age = 25; |
float |
Stores numbers with decimals | 4 | float price = 99.5; |
double |
Stores large decimal numbers with double precision | 8 | double distance = 4578.245; |
char |
Stores a single character | 1 | char grade = 'A'; |
bool |
Stores true or false values | 1 | bool isPass = true; |
void |
Represents the absence of a value | — | Used in functions with no return value |
Each data type has a specific purpose. For instance, you’d use int for counting, float or double for precise calculations, char for letters, and bool for logical conditions.
#include <iostream>
using namespace std;
int main() {
int age = 21;
float height = 5.4;
double weight = 56.789;
char grade = 'A';
bool isStudent = true;
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Weight: " << weight << endl;
cout << "Grade: " << grade << endl;
cout << "Student: " << isStudent;
return 0;
}
Output:
Age: 21
Height: 5.4
Weight: 56.789
Grade: A
Student: 1
In C++, boolean values display as 1 for true and 0 for false when printed.
Derived data types are built from the basic ones. These include:
Arrays – A collection of elements of the same type.
Example: int marks[5];
Pointers – Variables that store memory addresses.
Example: int *ptr;
Functions – Blocks of code that return values.
Example:
int add(int a, int b) {
return a + b;
}
References – Alternate names for existing variables.
Example:
int x = 10;
int &ref = x;
Derived data types make C++ flexible and efficient when working with large or complex data.
C++ allows you to create your own custom data types using:
Structure (struct)
Used to group different types of data.
struct Student {
int id;
string name;
float marks;
};
Union (union)
Similar to structures but shares memory for all members.
Enumeration (enum)
Used to define named constants.
enum Day { Sunday, Monday, Tuesday };
Class
A blueprint for creating objects (part of Object-Oriented Programming).
These user-defined types help in building real-world data models and applications.
C++ allows you to modify the size and range of primitive data types using type modifiers. These are:
signed
unsigned
short
long
They are used with data types like int and double to control the storage range.
| Data Type | Range |
|---|---|
short int |
-32,768 to 32,767 |
unsigned short int |
0 to 65,535 |
long int |
-2,147,483,648 to 2,147,483,647 |
unsigned long int |
0 to 4,294,967,295 |
#include <iostream>
using namespace std;
int main() {
short int smallNum = 32767;
unsigned int positiveNum = 45000;
long int bigNum = 2000000000;
cout << "Short Int: " << smallNum << endl;
cout << "Unsigned Int: " << positiveNum << endl;
cout << "Long Int: " << bigNum;
return 0;
}
Type modifiers are particularly useful in system-level programming where memory efficiency and data range matter.
Sometimes, you may need to convert one data type into another. This process is called type casting. It can be done manually (explicit casting) or automatically (implicit casting).
int a = 5;
double b = a; // int automatically converted to double
double price = 99.99;
int rounded = (int)price;
Type casting is helpful when performing arithmetic operations between mixed data types.
To know how much memory a data type takes, C++ provides the sizeof() operator.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
This command helps in writing portable code that works across different systems.
In C++, you can declare constants using the const keyword. A constant’s value cannot be changed after initialization.
Example:
const float PI = 3.1416;
If you try to modify PI, the compiler will generate an error.
C++ data types define the type and size of data stored in variables. They can be categorized into primitive, derived, and user-defined types. Primitive types like int, float, and char form the base, while derived and user-defined types add flexibility. You can modify their range with type modifiers and convert between them using casting. Understanding data types is fundamental to writing efficient and bug-free C++ programs.
What are data types in C++ and why are they important in programming?
Write a C++ program that declares and displays values of int, float, double, char, and bool data types.
What is the difference between primitive, derived, and user-defined data types in C++? Give one example of each.
Explain the purpose of type modifiers in C++. How do they affect the range of values?
Write a C++ program that uses the sizeof() operator to display the size of all basic data types.
What is type casting? Write an example showing both implicit and explicit type casting in C++.
What happens if you store a decimal value in an integer variable? Explain with a short example.
Create a struct named Employee with fields id, name, and salary. Write a C++ program to display employee details.
Write a C++ program that declares a const variable and shows what happens if you try to modify it.
What is the difference between float and double in terms of precision and storage?