c++ common data types

type of data

The meaning of data type existence: allocate appropriate memory space to variables

There are several ways to represent integer types in C++, the difference is that the memory space occupied is different:

short (short integer) Occupied space: 2 bytes

int (integer) Occupied space: 4 bytes

long (long integer) Occupied space: 4 bytes (windows)

long long (long long integer) Occupied space: 8 bytes

sizeof keyword

The sizeof operator can be used to obtain the size of classes, structures, unions, and other user-defined data types.

The syntax for using sizeof is as follows:

sizeof (data type)


using namespace std;

int main() {

       cout << "short The memory space occupied by the type is: " << sizeof(short) << endl;

       cout << "int The memory space occupied by the type is: " << sizeof(int) << endl;

       cout << "long The memory space occupied by the type is: " << sizeof(long) << endl;

       cout << "long long The memory space occupied by the type is: " << sizeof(long long) << endl;


       return 0;


There are two types of floating-point variables:

1. Single-precision float takes up space: 4 bytes, 7 significant figures

2. Double-precision double takes up space: 8 bytes with 15-16 significant digits

The number of significant digits displayed for floating-point values ​​can be controlled by using the setprecision operator

int main()


    float a = 12.257902012398877;

    double b = 12.257902012398877;

    const float PI = 3.1415926;         // constant definition

    cout << setprecision(15) << a << endl;  // There are only 6-7 significant digits, and the following ones are imprecise

    cout << setprecision(15) << b << endl;  // has 15-16 significant figures, so exactly

    cout << setprecision(15) << PI << endl;

    return 0;


Character variables are used to display a single character

Syntax: `char ch = 'a';

> Note 1: When displaying character variables, enclose the characters in single quotes instead of double quotes

> Note 2: There can only be one character in single quotes, not a string

- Character variables in C and C++ only occupy 1 byte.

- The character variable does not store the character itself in the memory, but puts the corresponding ASCII code into the storage unit

int main() {

       char ch = 'b';

       cout << ch << endl;

       cout << sizeof(char) << endl;

       //ch = "abcde"; //Error, double quotes are not allowed

       //ch = 'abcde'; // error, only one character can be quoted in single quotes

       cout << (int)ch << endl;  //View the ASCII code corresponding to the character a

       ch = 98; //You can directly use ASCII to assign values ​​​​to character variables

       cout << ch << endl;


       return 0;


escape character

Function: used to represent some ASCII characters that == cannot be displayed

Examples of escape characters "\", "\t", "\n"

int main() {

       cout << "\\" << endl;

       cout << "hi\tHello" << endl;

       cout << "aaaa\n" ;


       return 0;


If there is no \n here, the words "please press any key to continue" will be on the same line as aaaa

string type

Role: used to represent a string of characters

You can use these two ways of writing

1. C-style string: `char variable name[] = "string value"`

   int main() {

       char str1[] = "hello world";

       cout << str1 << endl;



       return 0;


2. C++ style string: `string variable name = "string value"`

  int main() {

       string str = "hello world";

       cout << str << endl;



       return 0;


For example, these two ways of writing are output hello world (note: C++ style string, you need to add the header file #include\<string>)

Boolean type bool

Role: Boolean data type represents true or false value

The bool type has only two values:

true --- true (essentially 1)

false --- false (essentially 0)

int main() {

       bool flag = true;

       cout << flag << endl; // 1

       flag = false;

       cout << flag << endl; // 0


       return 0;


Tags: C++ programming language

Posted by simn_stv on Tue, 20 Dec 2022 00:17:37 +0300