In C++, there are several primitive data types, which are also known as fundamental or built-in data types. These include:
-
Integer types: Used to represent whole numbers. The most common integer types in C++ are int, short, long, and long long.
-
Floating-point types: Used to represent real numbers with fractional parts. The two main floating-point types in C++ are float and double.
-
Character types: Used to represent characters. The two main character types in C++ are char and wchar_t.
-
Boolean type: Used to represent true or false values. The boolean type in C++ is bool.
-
Void type: Used to indicate a lack of a return value. The void type in C++ is used for functions that do not return a value.
-
Enumeration types: Used to define a set of named constants. Enumeration types in C++ are defined using the enum keyword.
-
Pointer types: Used to store memory addresses. Pointer types in C++ are defined using the * operator.
These primitive types are used to build more complex data structures and functions in C++. It's important to understand these types and how they work to write efficient and effective code in C++.
#include <iostream>
using namespace std;
int main() {
cout << sizeof(bool) << endl;
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;
cout << sizeof(long long) << endl;
cout << sizeof(char *) << endl;
return 0;
}
// Outputs
/*
1
2
4
4
4
8
8
8
8
*/