In C++, variables are used to store data values. Each variable has a data type, which determines the type of data that can be stored in the variable.
Common data types in C++ include:
- int (for storing integers)
- double (for storing floating-point numbers)
- char (for storing single characters)
- bool (for storing boolean values)
Here's an example that demonstrates the use of variables and data types in C++:
SNIPPET
1#include <iostream>
2using namespace std;
3
4int main() {
5 int age = 25;
6 double height = 1.75;
7 char gender = 'M';
8
9 cout << "My age is " << age << " years old.\n";
10 cout << "My height is " << height << " meters.\n";
11 cout << "My gender is " << gender << ".\n";
12
13 return 0;
14}
This program declares variables for age, height, and gender, assigns them values, and then uses the cout
statement to display the values.
When you run this program, you'll see the following output:
SNIPPET
1My age is 25 years old.
2My height is 1.75 meters.
3My gender is M.
By using variables and data types, you can store and manipulate different types of data in your C++ programs, allowing for more robust and flexible applications.
xxxxxxxxxx
14
using namespace std;
int main() {
int age = 25;
double height = 1.75;
char gender = 'M';
cout << "My age is " << age << " years old.\n";
cout << "My height is " << height << " meters.\n";
cout << "My gender is " << gender << ".\n";
return 0;
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment