what are the primitive types of C++?

发布时间 2023-03-23 22:41:57作者: SpacetimeCoding

In C++, there are several primitive data types, which are also known as fundamental or built-in data types. These include:

  1. Integer types: Used to represent whole numbers. The most common integer types in C++ are int, short, long, and long long.

  2. Floating-point types: Used to represent real numbers with fractional parts. The two main floating-point types in C++ are float and double.

  3. Character types: Used to represent characters. The two main character types in C++ are char and wchar_t.

  4. Boolean type: Used to represent true or false values. The boolean type in C++ is bool.

  5. 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.

  6. Enumeration types: Used to define a set of named constants. Enumeration types in C++ are defined using the enum keyword.

  7. 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

*/