CPP Type: In the world of C++ programming, C++ type play a vital role in defining the kind of information a program can handle. From numbers to characters to complex structures and collections, every piece of data needs a proper type.
This article will take you through each one in detail and in a simple manner. Whether you are a beginner or brushing up your skills, understanding C++ data types is the first step towards writing efficient and reliable programs.
What are CPP Type/C++ Data Types?
A CPP type is a way of telling the computer what kind of data you want to store in a variable, and how much memory it should reserve for it. A data type defines the type of data a variable can hold, like numbers, characters, or true/false values.
Imagine you have a box labelled “Age” and you want to store the value 25 in it. The data type will define that this box can only hold numbers and will also define the space to reserve in memory for it. The common data types in C++ are int, float, char, bool, and double.
C++ type specify the type of data that a variable can hold. They are mainly classified into:
- Primary Data Types (Built-in)
- Derived Data Types
- User-Defined Data Types
- Modifiers
Read More: Concept of OOPs In C++ Programming Language 2025
Primary (Built-in) Data Types
The most basic and commonly used data C++ type is called primary (or fundamental) data types. Think of it like this: as in real life, you have different boxes to store clothes, books, or toys — in programming, you have different data types to store different kinds of values. The five primary C++ data types are mentioned below:
1. int (Integer)
The int (Integer) stores whole numbers. It can be positive, negative, or zero. It typically takes 4 bytes in most modern systems, 4 bytes that is 32 bits. The int value ranges from -2,147,483,648 to 2,147,483,648. It is used when we need to store whole numbers, like age, count, number of people, and other similar data.
Example of Integer
Example of int C++ Type |
---|
int age = 25;
int numberOfStudents = 50; int temperature = -10; |
2. float (Floating Point Number)
The float is used to store decimal numbers or numbers with fractional parts. It is called “floating point” because the decimal point can “float”, means it can appear anywhere in the number. It also takes 4 bytes of space in the memory that is equivalent to 32 bits.
It ranges approximately from 3.4e-38 to 3.4e+38 with the positive and negative ends. It is used when we need to store numbers with decimals that don’t need a lot of precision, like prices, weights, simple measurements, and other similar data.
Example
Example of float C++ Type |
---|
float price = 5.75;
float distance = 12.345; float temperature = -10.5; |
3. double (Double Precision Floating Point Number)
The double is like a float, but it is used when you need more precision and a larger range. The word double means double the precision of float. It takes about 8 bytes in the memory that is equivalent to 64 bits. It approximately ranges from 1.7e-308 to 1.7e+308 with the positive and negative ends. It is used when we need to store very large numbers or very precise decimal values, like scientific measurements or calculations with high accuracy.
Example
Example of double C++ Type |
---|
double pi = 3.14159265358979;
double massOfEarth = 5.972e24; |
4. char (Character)
The char is used to store a single character like a letter, number or symbol. Characters are always written in single quotes (‘ ‘). It takes only 1 byte of space in the memory that is equivalent to 8 bits. It stores an ASCII value from 0 to 255. It is used when we need to store a single character, like grade, letter, or symbol.
Read More: C++ Game Development: How to Build C++ Gaming Platform?
Example
Example of char C++ Type |
---|
char grade = ‘A’;
char symbol = ‘#’; char digit = ‘7’; |
5. bool (Boolean)
The bool is used to store true or false values. It is useful for decisions or conditions in programs like yes/no or on/off. It also takes a space of 1 byte in the memory that is equivalent to 8 bits, though it only require 1 bit. The possible values of bool, include true or false. It is used when we need to represent a yes/no situation or test whether a condition is true or false.
Example
Example of bool C++ Type |
---|
bool isSunny = true;
bool isAlive = false; bool hasPassed = true; |
Derived Data Types in C++
These are the data types that are made from the primary data types in C++. They help you store multiple values or addresses or even create structured ways of grouping data. We have arrays, pointers, references, and functions as the derived data types in C++. Let us examine each one of them:
1. Array
An array is a collection of values of the same C++ type stored in contiguous memory locations. The index of an array starts from 0. An array is used when we need to keep values together in a contiguous fashion.
Example
int marks[5] = {90, 85, 88, 92, 95};
cout << marks[0]; // prints 90 |
2. Pointer
A pointer is a variable that stores the memory address of another variable. It is used to store the memory location of a variable and then perform operations on it.
Example
int number = 10;
int* ptr = &number; // &number is the address of number cout << *ptr; // prints 10 (value at that address) |
3. Reference
A reference is a nickname for a variable, like an alias. The changes made to the reference variable will affect the original variable too.
Example
int a = 10;
int& b = a; // b is a reference to a b = 20; cout << a; // prints 20 |
4. Function.
A function is a block of code that performs a specific task. A function can have parameters and a return type. It is one of the most frequently used elements in C++ type or any programming language.
Example
int add(int a, int b) {
return a + b; } cout << add(5, 3); // prints 8 |
User-Defined Data Types
These are the custom data types created by programmers using existing data types, it may be primary or derived. They allow you to group different types of data together and give them a new name. Structure, Union, Enumeration, and Class are the examples of user-defined data types in C++. Let us have a look at them in detail.
1. Structure (struct)
The struct is used to group different data types under one name. The members are stored separately in memory.
Example
struct Student {
int id; char name[50]; float marks; }; Student s1 = {1, “Suraj”, 85.5}; cout << s1.name; // prints Suraj |
2. Union (union)
The union is like a struct but shares the same memory space for all its members. In a union, only a member can hold a value at a time.
Example
union Data {
int i; float f; char str[20]; }; Data d; d.i = 10; cout << d.i; // prints 10 |
3. Enumeration (enum)
The enum is used to assign names to integer constants, making code more readable and understandable.
Example
enum Color { RED, GREEN, BLUE };
Color c = GREEN; cout << c; // prints 1 (since RED=0, GREEN=1, BLUE=2) |
4. Class (class)
A class is a blueprint for creating objects. A class can contain variables and functions referred to as data members and member functions respectively.
Example
class Car {
public: string brand; int speed; void display() { cout << “Brand: ” << brand << “, Speed: ” << speed; } }; Car c1; c1.brand = “Tesla”; c1.speed = 250; c1.display(); // prints Brand: Tesla, Speed: 250 |
Learn C++ Programming With PW Skills
Become proficient in Data structures and C++ programming with PW Skills Decode DSA With C++ Course. Engage in an interactive learning environment with top industry experts guiding you throughout the course. This course is designed for people who want to build their career in the tech industry.
Engage in interactive learning using recorded videos and practice and master the fundamentals of C++ with concepts of data structures and algorithms in C++. Prepare for job interviews and competitive programming with pwskills.com
C++ Type FAQs
Q1. What are the different data types in C++?
Ans. In C++, data types specify the type of data that a variable can hold. They are mainly classified into:
Primary Data Types (Built-in)
Derived Data Types
User-Defined Data Types
Modifiers
Q2. What are data types?
Ans. A data type is a way of telling the computer what kind of data you want to store in a variable and how much memory it should reserve for it. A data type defines the type of data a variable can hold, like numbers, characters, or true/false values.
Q3. What are primitive and derived data types in C++?
Ans. The most basic and commonly used data types in C++ are called primary (or fundamental) data types. The derived data types are the data types that are made from the primary data types in C++.
Q4. What is a class in C++?
Ans. A class is a blueprint for creating objects. A class can contain variables and functions referred to as data members and member functions respectively.