In C++, primitive types refer to the basic data types that are built into the language and are not derived from any other data type. These data types are used to represent basic values that are used in calculations and other computations.
Here are some examples of the primitive data types in C++:
1. Integer (int): An integer variable holds a whole number that can be either positive, negative, or zero. For example:
int age = 25;
2. Floating-point (float/double): Floating-point variables are used to represent decimal numbers. Float variables can handle up to 7 decimal digits while double variables can handle up to 15 decimal digits. For example:
float salary = 5000.50;
double pi = 3.14159265359;
3. Boolean (bool): Boolean variables can only hold two values, true or false. For example:
bool isMarried = true;
4. Character (char): A character variable holds a single character, enclosed in single quotes (''). For example:
char grade = 'A';
5. Void (void): Void is a special type used to indicate that a function does not return any value.
void calculateAge(int birthYear) {
int age = 2021 - birthYear;
cout << "Your age is: " << age << endl;
}
These are just a few examples of the primitive data types in C++. By using these basic data types effectively, you can create powerful algorithms and programs.