Data Types in C++
In C++, data types are used to specify the type and size of data that can be stored in variables. Each data type has a different set of values it can represent and occupies a specific amount of memory.
Here are some of the commonly used data types in C++:
- int: Used to store whole numbers (e.g., 10, -5, 1000).
- float: Used to store floating-point numbers with a decimal part (e.g., 3.14, -0.5, 2.0).
- char: Used to store single characters (e.g., 'a', 'X', '@').
- bool: Used to store boolean values (either
true
orfalse
).
These data types can be used to declare variables and assign values to them. Here's an example:
1#include <iostream>
2using namespace std;
3
4int main() {
5 int age = 30;
6 float height = 180.5;
7 char grade = 'A';
8 bool isProgrammer = true;
9
10 // Output the values
11 cout << "My age is " << age << endl;
12 cout << "My height is " << height << endl;
13 cout << "My grade is " << grade << endl;
14 cout << "Am I a programmer? " << boolalpha << isProgrammer << endl;
15
16 return 0;
17}
The code above declares variables of different data types and assigns them values. It then outputs the values using the cout
statement. When using cout
to output boolean values, the boolalpha
manipulator is used to display true
or false
instead of 1
or 0
.
By understanding the different data types in C++, you can effectively store and manipulate various types of data in your programs.
Next, we will explore control flow in C++ and learn how to use conditional statements and loops to control the flow of execution in our programs.
xxxxxxxxxx
using namespace std;
int main() {
int age = 30;
float height = 180.5;
char grade = 'A';
bool isProgrammer = true;
cout << "My age is " << age << endl;
cout << "My height is " << height << endl;
cout << "My grade is " << grade << endl;
cout << "Am I a programmer? " << boolalpha << isProgrammer << endl;
return 0;
}