Data types in C++ define the type of data that a variable can store. They help the compiler understand how much memory to allocate and what kind of operations can be performed on the data.
Choosing the correct data type improves performance, memory usage, and program reliability.
Data types in C++ are broadly categorized into three main types:
In C++, primitive data types (also known as built-in data types) are the basic types provided by the language. These data types are used to store simple and fundamental values such as numbers, characters, and logical values.
They form the foundation of any C++ program because all variables are created using these data types. Understanding primitive types is essential before moving on to more advanced concepts.
Primitive data types directly store single values in memory. They are simple, efficient, and require less memory compared to complex data types. Each data type defines what kind of data a variable can hold and how much memory it will use.
Each primitive data type uses a specific amount of memory, which may vary depending on the system and compiler. For example, int typically uses 4 bytes, while char usually uses 1 byte. Choosing the correct data type helps optimize memory usage and improves program performance.
#include <iostream>
using namespace std;
int main()
{
int age = 20;
float marks = 85.5;
char grade = 'A';
bool passed = true;
cout << "Age: " << age << endl;
cout << "Marks: " << marks << endl;
cout << "Grade: " << grade << endl;
cout << "Passed: " << passed << endl;
return 0;
}
In the above program:
age is an integer variable storing a whole number.marks is a float variable storing a decimal value.grade stores a single character.passed stores a boolean value (true/false).In C++, derived data types are created using the basic (primitive) data types. These types allow programmers to handle more complex data structures and relationships in an efficient way. They extend the functionality of primitive types by combining or referencing them.
Derived data types are very useful when working with collections of data, memory management, or reusable code blocks.
Derived data types are formed by modifying or combining primitive data types. They provide flexibility and help in solving real-world programming problems more effectively.
#include <iostream>
using namespace std;
int main()
{
int arr[3] = {10, 20, 30}; // Array
int *ptr = &arr[0]; // Pointer
cout << "First element: " << *ptr << endl;
return 0;
}
arr is an array that stores three integer values.ptr is a pointer that holds the address of the first element of the array.*ptr is used to access the value stored at that address (dereferencing).In C++, user-defined data types are created by programmers to represent complex data in a more organized and meaningful way. Unlike primitive or derived data types, these types allow you to design your own data structures based on program requirements.
They are especially useful when dealing with real-world problems where a single variable is not enough to represent complete information.
User-defined data types are custom types built using existing data types. They allow grouping of different types of data under a single name, making the program more structured and easier to manage.
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
};
int main()
{
Student s1;
s1.name = "Rahul";
s1.age = 21;
cout << s1.name << " " << s1.age;
return 0;
}
Student is a structure that groups name and age into one unit.s1 is an object of type Student..) operator.| Type | Description | Example |
|---|---|---|
| Primitive | Basic data types provided by C++ | int, float |
| Derived | Created from primitive types | array, pointer |
| User-Defined | Created by programmer | struct, class |
Data types are a fundamental part of C++ programming. They define how data is stored and manipulated in a program. Understanding primitive, derived, and user-defined data types helps developers write efficient, structured, and error-free programs.