Introduction
Data types in C++ define the type of data that a variable can hold. Understanding the different data types is essential for effective programming, as it helps in choosing the appropriate type for the data you want to store and manipulate. This chapter will cover the basic data types in C++ with simple examples and their outputs.
Categories of Data Types
C++ data types can be categorized into:
- Integer Types
- Floating-Point Types
- Character Type
- Boolean Type
- Wide Character Type
Integer Types
int
The int
type is used to store whole numbers.
Example
#include <iostream>
using namespace std;
int main() {
int age = 25; // Declare an integer variable and initialize it
cout << "Age: " << age << endl; // Output the value of age
return 0;
}
Output
Age: 25
short
The short
type is used to store short integer values, typically smaller range than int
.
Example
#include <iostream>
using namespace std;
int main() {
short year = 2022; // Declare a short integer variable and initialize it
cout << "Year: " << year << endl; // Output the value of year
return 0;
}
Output
Year: 2022
long
The long
type is used to store long integer values, typically larger range than int
.
Example
#include <iostream>
using namespace std;
int main() {
long population = 7800000000; // Declare a long integer variable and initialize it
cout << "Population: " << population << endl; // Output the value of population
return 0;
}
Output
Population: 7800000000
long long
The long long
type is used to store very large integer values.
Example
#include <iostream>
using namespace std;
int main() {
long long distance = 150000000000LL; // Declare a long long integer variable and initialize it
cout << "Distance: " << distance << endl; // Output the value of distance
return 0;
}
Output
Distance: 150000000000
Floating-Point Types
float
The float
type is used to store single-precision floating-point numbers.
Example
#include <iostream>
using namespace std;
int main() {
float pi = 3.14f; // Declare a float variable and initialize it
cout << "Pi: " << pi << endl; // Output the value of pi
return 0;
}
Output
Pi: 3.14
double
The double
type is used to store double-precision floating-point numbers.
Example
#include <iostream>
using namespace std;
int main() {
double gravity = 9.81; // Declare a double variable and initialize it
cout << "Gravity: " << gravity << endl; // Output the value of gravity
return 0;
}
Output
Gravity: 9.81
long double
The long double
type is used to store extended precision floating-point numbers.
Example
#include <iostream>
using namespace std;
int main() {
long double precision = 1.123456789012345L; // Declare a long double variable and initialize it
cout << "Precision: " << precision << endl; // Output the value of precision
return 0;
}
Output
Precision: 1.12346
Character Type
char
The char
type is used to store single characters.
Example
#include <iostream>
using namespace std;
int main() {
char initial = 'A'; // Declare a char variable and initialize it
cout << "Initial: " << initial << endl; // Output the value of initial
return 0;
}
Output
Initial: A
Boolean Type
bool
The bool
type is used to store boolean values (true
or false
).
Example
#include <iostream>
using namespace std;
int main() {
bool isStudent = true; // Declare a bool variable and initialize it
cout << "Is Student: " << isStudent << endl; // Output the value of isStudent
return 0;
}
Output
Is Student: 1
(Note: true
is output as 1
and false
as 0
.)
Wide Character Type
wchar_t
The wchar_t
type is used to store wide characters, which are typically used for Unicode characters.
Example
#include <iostream>
using namespace std;
int main() {
wchar_t wideChar = L'?'; // Declare a wide char variable and initialize it
wcout << L"Wide Character: " << wideChar << endl; // Output the value of wideChar
return 0;
}
Output
Wide Character: ?
Conclusion
Understanding C++ data types is fundamental to effective programming. By knowing the different data types and their usage, you can store and manipulate data efficiently. This chapter covered the basic data types, including integer, floating-point, character, boolean, and wide character types. In the next chapter, we will delve into more advanced topics such as C++ constants and literals.