Data Types in C++
1. What is a Data Type?
A data type specifies the type of data that a variable can store.
For example:
int age = 20;
Here, int is the data type, age is the variable, and 20 is the value.
Data types help the compiler determine:
- What kind of data is being stored
- How much memory is required
- What operations can be performed on the data
2. Types of Data Types in C++
C++ data types can be broadly divided into:
Data Types │ ├── Fundamental Data Types │ ├── Integer │ ├── Floating Point │ ├── Character │ ├── Boolean │ └── Void │ ├── Derived Data Types │ ├── Array │ ├── Pointer │ ├── Reference │ └── Function │ └── User-Defined Data Types ├── Structure ├── Union ├── Enumeration └── Class
3. Fundamental Data Types
Fundamental data types are the basic types provided by C++.
3.1 Integer (int)
The int data type is used to store whole numbers without decimal values.
Example:
int age = 20; int marks = 85; int number = -10;
Example program:
#include <iostream> using namespace std; int main() { int age = 20; cout << "Age: " << age; return 0; }
Output:
Age: 20
3.2 Floating-Point (float)
The float data type is used to store numbers containing decimal values.
Example:
float price = 99.50; float height = 5.6;
Example:
#include <iostream> using namespace std; int main() { float price = 99.50; cout << "Price: " << price; return 0; }
Output:
Price: 99.5
3.3 Double (double)
double is also used to store decimal numbers. It generally provides more precision than float.
Example:
double salary = 50000.75; double pi = 3.1415926535;
Example:
#include <iostream> using namespace std; int main() { double pi = 3.1415926535; cout << pi; return 0; }
3.4 Character (char)
The char data type stores a single character.
A character is written inside single quotation marks.
Example:
char grade = 'A'; char symbol = '#'; char letter = 'X';
Example program:
#include <iostream> using namespace std; int main() { char grade = 'A'; cout << "Grade: " << grade; return 0; }
Output:
Grade: A
3.5 Boolean (bool)
The bool data type stores one of two logical values:
truefalse
Example:
bool passed = true; bool failed = false;
Example program:
#include <iostream> using namespace std; int main() { bool passed = true; cout << passed; return 0; }
Output:
1
By default, cout displays true as 1 and false as 0.
You can display the words using:
cout << boolalpha << passed;
Output:
true
3.6 Void (void)
void means no value.
It is commonly used with functions that do not return a value.
Example:
void display() { cout << "Hello"; }
The function display() does not return any value.
4. Integer Type Modifiers
C++ provides modifiers that can change the range or representation of integer types.
Common modifiers include:
shortlongsignedunsigned
Examples:
short int age = 20; long int population = 1000000; unsigned int marks = 100;
unsigned
An unsigned integer stores only non-negative values.
unsigned int age = 20;
It cannot store negative values.
5. Size of Data Types
The exact size of some C++ types can vary depending on the compiler and platform.
You can check the size using the sizeof operator.
Example:
#include <iostream> using namespace std; int main() { cout << "int: " << sizeof(int) << " bytes" << endl; cout << "float: " << sizeof(float) << " bytes" << endl; cout << "double: " << sizeof(double) << " bytes" << endl; cout << "char: " << sizeof(char) << " byte" << endl; cout << "bool: " << sizeof(bool) << " byte" << endl; return 0; }
On many modern systems, the output is commonly:
int: 4 bytes float: 4 bytes double: 8 bytes char: 1 byte bool: 1 byte
These sizes should not be assumed to be identical on every platform.
6. Derived Data Types
Derived data types are created from fundamental or other existing types.
6.1 Array
An array stores multiple values of the same type.
Example:
int marks[5] = {80, 75, 90, 85, 70};
Here, marks is an array containing five integers.
6.2 Pointer
A pointer stores the memory address of another variable.
Example:
int age = 20; int *ptr = &age;
Here:
agestores20&agegives the address ofageptrstores that address
6.3 Reference
A reference provides another name for an existing variable.
Example:
int age = 20; int &ref = age;
Here, ref refers to the variable age.
6.4 Function
A function is also considered a derived type in the C++ type system.
Example:
int add(int a, int b) { return a + b; }
The function add() returns an integer value.
7. User-Defined Data Types
User-defined data types allow programmers to create their own types.
7.1 Structure (struct)
A structure groups different types of data together.
Example:
struct Student { string name; int age; float marks; };
Usage:
Student s1; s1.name = "Anuradha"; s1.age = 20; s1.marks = 85.5;
7.2 Union (union)
A union allows different members to share the same memory location.
Example:
union Data { int number; float price; char grade; };
Only one member is generally intended to hold a meaningful value at a time.
7.3 Enumeration (enum)
An enumeration defines a set of named integral constants.
Example:
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday };
Usage:
Day today = Wednesday;
7.4 Class
A class is a user-defined type used extensively in object-oriented programming.
Example:
class Student { public: string name; int age; };
Creating an object:
Student s1; s1.name = "Anuradha"; s1.age = 20;
8. String in C++
C++ provides std::string for storing a sequence of characters.
To use it, include the <string> header.
Example:
#include <iostream> #include <string> using namespace std; int main() { string name = "Anuradha"; cout << name; return 0; }
Output:
Anuradha
string is a standard library class rather than a fundamental built-in data type.
9. Type Conversion
Type conversion means converting a value from one data type to another.
9.1 Implicit Conversion
The compiler automatically converts one type to another when appropriate.
Example:
int number = 10; double value = number;
Here, the integer 10 is automatically converted to double.
9.2 Explicit Conversion
The programmer explicitly converts a value.
Example:
double price = 99.99; int value = static_cast<int>(price);
The result is:
99
Example:
int a = 10; int b = 3; double result = static_cast<double>(a) / b;
This produces a decimal result instead of integer division.
10. const with Data Types
The const keyword makes a variable read-only after initialization.
Example:
const int MAX = 100;
This is not allowed:
MAX = 200;
Another example:
const double PI = 3.14159;
11. Example Using Different Data Types
#include <iostream> #include <string> using namespace std; int main() { int age = 20; float height = 5.5f; double salary = 50000.75; char grade = 'A'; bool passed = true; string name = "Anuradha"; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Height: " << height << endl; cout << "Salary: " << salary << endl; cout << "Grade: " << grade << endl; cout << "Passed: " << boolalpha << passed << endl; return 0; }
Output:
Name: Anuradha Age: 20 Height: 5.5 Salary: 50000.75 Grade: A Passed: true
12. Data Type Summary
| Data Type | Purpose | Example |
|---|---|---|
int | Whole numbers | int age = 20; |
float | Decimal numbers | float price = 10.5f; |
double | More precise decimals | double pi = 3.14159; |
char | Single character | char grade = 'A'; |
bool | True/false values | bool pass = true; |
void | No value | void display() |
string | Text | string name = "Anu"; |
array | Collection of same type | int a[5]; |
pointer | Stores an address | int *p; |
reference | Alias for a variable | int &r = x; |
struct | Groups different data | struct Student |
union | Shared memory members | union Data |
enum | Named constants | enum Day |
class | User-defined object type | class Student |
13. Important Points
- A data type determines what kind of value a variable can store.
- Fundamental types include
int,float,double,char,bool, andvoid. - Arrays, pointers, references, and functions are derived types.
- Structures, unions, enumerations, and classes are user-defined types.
stringis provided by the C++ standard library.- The
sizeofoperator can be used to determine the size of a type or object. - Type conversion can be implicit or explicit.
constcan be used to create read-only variables.
Conclusion
Data types are an essential part of C++ programming. They tell the compiler what kind of data a variable can store and help determine how that data is represented and operated on. Understanding data types is important before learning more advanced concepts such as arrays, pointers, structures, classes, and objects.