C++ Data Types
In C++, data types define the kind of data that can be stored in a variable or used in expressions. Data types determine the size of the variable, the range of values it can hold, and the operations that can be performed on it. Choosing the correct data type is crucial for writing efficient and type-safe programs.
This post will cover:
int
, float
, char
, etc.struct
, enum
, and class
).Primitive data types are the most basic types that come built into the C++ language. They directly map to the machine’s memory and are used to store simple values like numbers, characters, or boolean values.
Integer types are used to store whole numbers, both positive and negative. They do not store decimal points.
int
: The most commonly used integer type. It can store a range of integers depending on the system (typically 4 bytes or 32 bits).short
: A smaller integer type (typically 2 bytes).long
: A larger integer type (typically 4 or 8 bytes).long long
: An even larger integer type (typically 8 bytes).
#include <iostream>
using namespace std;
int main() {
int age = 25; // int variable
short count = 10; // short variable
long population = 7800000000L; // long variable
long long distance = 9876543210LL; // long long variable
cout << "Age: " << age << endl;
cout << "Count: " << count << endl;
cout << "Population: " << population << endl;
cout << "Distance: " << distance << endl;
return 0;
}
Floating-point types are used to store numbers with fractional parts, such as decimal values.
float
: Single precision floating-point number (typically 4 bytes).double
: Double precision floating-point number (typically 8 bytes).long double
: Extended precision floating-point number (typically 12 or 16 bytes).
#include <iostream>
using namespace std;
int main() {
float pi = 3.14f; // float variable
double e = 2.718281828459045; // double variable
long double goldenRatio = 1.6180339887L; // long double variable
cout << "Pi: " << pi << endl;
cout << "e: " << e << endl;
cout << "Golden Ratio: " << goldenRatio << endl;
return 0;
}
Character types are used to store individual characters. These can be any single character enclosed in single quotes.
char
: A single character (typically 1 byte).wchar_t
: A wide character (used for storing large character sets like Unicode, typically 2 or 4 bytes).char16_t
: A character type for UTF-16 encoded characters.char32_t
: A character type for UTF-32 encoded characters.
#include <iostream>
using namespace std;
int main() {
char grade = 'A'; // char variable
wchar_t symbol = L'Ω'; // wchar_t variable (wide character)
cout << "Grade: " << grade << endl;
wcout << "Symbol: " << symbol << endl; // Use wcout for wide characters
return 0;
}
The boolean type is used to represent truth values: true
or false
.
bool
: A type that holds either true
(1) or false
(0).
#include <iostream>
using namespace std;
int main() {
bool isValid = true; // bool variable
if (isValid) {
cout << "The value is valid." << endl;
} else {
cout << "The value is invalid." << endl;
}
return 0;
}
Derived data types are built using the primitive data types. These types help in organizing more complex data structures.
An array is a collection of variables of the same type that are stored in contiguous memory locations.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Array of integers
for (int i = 0; i < 5; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
A pointer is a variable that stores the memory address of another variable. It allows indirect access to the data.
#include <iostream>
using namespace std;
int main() {
int number = 42;
int* ptr = &number; // Pointer to the integer variable 'number'
cout << "Value of number: " << number << endl;
cout << "Address of number: " << ptr << endl;
cout << "Dereferenced value (value at address): " << *ptr << endl;
return 0;
}
A reference is an alias for an existing variable. It allows you to create an alternative name for the variable.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int& ref = num; // Reference to the variable 'num'
cout << "Value of num: " << num << endl;
cout << "Value through reference: " << ref << endl;
ref = 20; // Changing value through reference
cout << "Updated num: " << num << endl;
return 0;
}
In addition to primitive and derived data types, C++ allows you to create your own custom data types.
struct
A struct
is used to define a collection of variables (possibly of different types) under one name.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person person1 = {"Alice", 25}; // Struct variable
cout << "Name: " << person1.name << endl;
cout << "Age: " << person1.age << endl;
return 0;
}
enum
An enum
(short for enumeration) defines a set of named integer constants, making the code more readable and maintainable.
#include <iostream>
using namespace std;
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() {
Days today = Wednesday;
cout << "Today is day number: " << today << endl; // Output: 3
return 0;
}
class
A class
is a blueprint for creating objects, which can have attributes and methods. It's the core of object-oriented programming in C++.
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1;
car1.brand = "Toyota";
car1.year = 2020;
car1.display();
return 0;
}
Type modifiers allow you to modify the size or range of certain data types.
signed
: Specifies that the variable can hold both positive and negative values (default for most numeric types).unsigned
: Specifies that the variable can only hold non-negative values.long
: Used to increase the size of integer types (e.g., long int
).short
: Used to decrease the size of integer types (e.g., short int
).
#include <iostream>
using namespace std;
int main() {
unsigned int positiveNum = 42; // unsigned integer
signed int negativeNum = -10; // signed integer
cout << "Positive Number: " << positiveNum << endl;
cout << "Negative Number: " << negativeNum << endl;
return 0;
}