Data Types in C++
Whenever you create a variable in C++, you need to tell the compiler what kind of value it will store. This is done using a data type. A data type defines the type of data a variable can hold, such as a number, a character, or text.
For example, if you want to store a person’s age, you would use an integer data type. If you want to store a decimal value like a product price, you would use a floating-point data type. Choosing the correct data type helps the compiler manage memory efficiently and prevents many programming errors.
Common C++ Data Types
The table below shows some of the most commonly used data types in C++.
| Data Type | Description | Example |
|---|---|---|
int | Stores whole numbers | 25 |
float | Stores decimal numbers | 15.75 |
double | Stores large decimal values with higher precision | 125.456789 |
char | Stores a single character | 'A' |
bool | Stores true or false | true |
string | Stores text or a sequence of characters | "CoderMantra" |
C++ Data Types with Examples
The following program shows how different data types are used in C++.
#include <iostream>
using namespace std;
int main() {
int age = 21;
float height = 5.8;
char grade = 'A';
bool passed = true;
string name = "Rahul";
cout << age << endl;
cout << height << endl;
cout << grade << endl;
cout << passed << endl;
cout << name;
return 0;
}
Output
21
5.8
A
1
Rahul
In this example, every variable stores a different type of value. This allows the program to handle numbers, characters, logical values, and text correctly.
Primitive Data Types in C++
The basic built-in data types provided by C++ are known as primitive data types. These include:
intfloatdoublecharboolvoid
These data types are the foundation of C++ programming and are used in almost every program you write.
Choosing the Right Data Type
Using the correct data type makes your program more efficient and easier to understand.
- Use
intfor whole numbers. - Use
floatordoublefor decimal values. - Use
charfor a single character. - Use
boolwhen the answer is onlytrueorfalse. - Use
stringto store words or sentences.
Choosing the appropriate data type also helps reduce memory usage and improves program performance.
Key Points
- Data Types in C++ define the kind of value a variable can store.
- Every variable must be declared with a data type.
int,float,double,char, andboolare commonly used data types.- Primitive data types are built into the C++ language.
- Selecting the right data type makes programs more efficient and readable.
Frequently Asked Questions
Q1. What are data types in C++?
Data types in C++ define the type of data a variable can store, such as integers, characters, floating-point numbers, and boolean values.
Q2. What are the main data types in C++?
The most common C++ data types include int, float, double, char, bool, and void. Each serves a different purpose depending on the type of data being stored.
Q3. Why are data types important in C++?
Data types help the compiler allocate memory efficiently, validate operations, and ensure that variables store the correct kind of data.
Q4. What is the difference between float and double in C++?
Both store decimal numbers, but double provides higher precision and can represent a larger range of values than float.
Q5. How do I choose the right data type in C++?
Choose a data type based on the kind of value you need to store. For example, use int for whole numbers, double for precise decimal values, char for single characters, and bool for true or false values.
Q6. Can I change the data type of a variable in C++?
No. Once a variable is declared with a specific data type, its type cannot be changed. If you need to store a different type of data, you must declare a new variable with the appropriate data type.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
