Chapter 2: Data Types in C++
A data type specifies the kind of value a variable can store. It also determines:
- How much memory the variable uses
- What range of values it can hold
- What operations can be performed on it
For example:
- An age is stored as an integer.
- A price is stored as a floating-point number.
- A grade is stored as a character.
- A user's login status is stored as a boolean value.
Choosing the correct data type improves performance, memory usage, and code readability.
Why Do We Need Data Types?
Imagine a program that stores different kinds of information:
- Student age → Whole number
- Product price → Decimal number
- User name → Text
- Exam result → True or False
A single data type cannot efficiently represent all of these values. C++ provides different data types for different purposes.
Categories of Data Types
C++ data types can be grouped into three categories:
1. Fundamental (Built-in) Data Types
intfloatdoublecharboolvoid
2. Derived Data Types
- Arrays
- Pointers
- References
- Functions
3. User-Defined Data Types
structclassenumunion
In this chapter, we'll focus on the most commonly used built-in types.
Integer (int)
The int type stores whole numbers without decimal points.
Syntax
1int age = 21;
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int apples = 10; 8 9 cout << apples; 10 11 return 0; 12}
Output
110
Common Uses
- Age
- Quantity
- Score
- Year
- Number of students
Floating-Point (float)
The float type stores decimal numbers with single precision.
Syntax
1float price = 99.99f;
The
fsuffix indicates afloatliteral.
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 float temperature = 36.5f; 8 9 cout << temperature; 10 11 return 0; 12}
Output
136.5
Common Uses
- Temperature
- Height
- Weight
- Product price
Double (double)
The double type stores decimal numbers with double precision, offering greater accuracy than float.
Syntax
1double pi = 3.14159265358979;
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 double salary = 45250.75; 8 9 cout << salary; 10 11 return 0; 12}
Output
145250.75
float vs double
| Feature | float | double |
|---|---|---|
| Approximate Size | 4 bytes | 8 bytes |
| Precision | ~7 decimal digits | ~15 decimal digits |
| Use Case | Less memory | Higher accuracy |
For scientific calculations and financial applications, double is generally preferred.
Character (char)
The char type stores a single character enclosed in single quotes.
Syntax
1char grade = 'A';
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 char letter = 'Z'; 8 9 cout << letter; 10 11 return 0; 12}
Output
1Z
A char stores the character's ASCII (or compatible) code internally.
Boolean (bool)
The bool type stores one of two values:
truefalse
Syntax
1bool isLoggedIn = true;
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 bool passed = true; 8 9 cout << passed; 10 11 return 0; 12}
Output
11
By default,
trueis displayed as1andfalseas0.
To display the words instead:
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 bool passed = true; 8 9 cout << boolalpha << passed; 10 11 return 0; 12}
Output
1true
String (string)
The string type stores a sequence of characters (text).
To use string, include the <string> header.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string name = "Ankit"; 9 10 cout << name; 11 12 return 0; 13}
Output
1Ankit
Common Uses
- Names
- Addresses
- Messages
- Email IDs
Auto (auto)
The auto keyword lets the compiler determine the variable's type automatically based on its initializer.
Example
1auto age = 21; 2auto price = 99.95; 3auto grade = 'A'; 4auto name = string("Ankit");
The compiler deduces:
age→intprice→doublegrade→charname→string
Complete Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 auto city = string("Delhi"); 9 auto population = 20000000; 10 11 cout << city << endl; 12 cout << population; 13 14 return 0; 15}
The sizeof Operator
The sizeof operator returns the size (in bytes) of a data type or variable.
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << sizeof(int) << endl; 8 cout << sizeof(double) << endl; 9 cout << sizeof(char) << endl; 10 cout << sizeof(bool); 11 12 return 0; 13}
Sample Output
14 28 31 41
The exact sizes may vary depending on your compiler and platform.
Type Conversion
Implicit Conversion
The compiler automatically converts a smaller type to a larger compatible type.
1int marks = 90; 2double score = marks; 3 4cout << score;
Output
190
Explicit Conversion (Casting)
You can manually convert one type to another.
1double price = 99.95; 2 3int roundedPrice = static_cast<int>(price); 4 5cout << roundedPrice;
Output
199
Summary of Common Data Types
| Data Type | Description | Example |
|---|---|---|
int | Whole numbers | 25 |
float | Decimal numbers (single precision) | 25.5f |
double | Decimal numbers (double precision) | 25.5 |
char | Single character | 'A' |
bool | True or false | true |
string | Text | "Hello" |
auto | Compiler-deduced type | auto x = 10; |
Best Practices
- Use
intfor whole numbers. - Prefer
doubleoverfloatunless memory usage is critical. - Use
boolfor logical values. - Use
stringfor text instead of character arrays. - Use
autowhen the type is obvious from the initializer. - Choose the smallest suitable data type without sacrificing correctness.
Common Beginner Mistakes
Using char for Multiple Characters
Incorrect:
1char name = 'Ankit';
Correct:
1string name = "Ankit";
Forgetting the f Suffix for float
1float price = 99.99f;
Storing Decimal Values in int
Incorrect:
1int value = 12.75;
Correct:
1double value = 12.75;
Practice Exercises
Exercise 1
Create variables of different data types and print them.
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 int age = 22; 9 double salary = 45000.50; 10 char grade = 'A'; 11 bool passed = true; 12 string name = "Ankit"; 13 14 cout << name << endl; 15 cout << age << endl; 16 cout << salary << endl; 17 cout << grade << endl; 18 cout << boolalpha << passed; 19 20 return 0; 21}
Exercise 2
Print the size of different data types using sizeof.
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "int: " << sizeof(int) << " bytes" << endl; 8 cout << "double: " << sizeof(double) << " bytes" << endl; 9 cout << "char: " << sizeof(char) << " bytes" << endl; 10 11 return 0; 12}
Interview Questions
- What is a data type in C++?
- What is the difference between
floatanddouble? - When should you use
bool? - What does the
autokeyword do? - What is the purpose of the
sizeofoperator? - What is implicit type conversion?
- How is explicit type conversion performed in C++?
- Why is
stringpreferred over a character array for text?
Summary
In this chapter, you learned:
- What data types are and why they matter.
- How to use
int,float,double,char,bool,string, andauto. - The difference between
floatanddouble. - How to determine memory size with
sizeof. - How implicit and explicit type conversions work.
- Best practices and common mistakes when selecting data types.
In the next chapter, you'll learn Input and Output in C++, including cin, cout, endl, and getline(), allowing your programs to interact with users.