Data type in C / C ++

While working with any programming language, you need to use various types of variables to store information. Variables, nothing but memory locations are reserved for saving values. That is, when you create a variable, you reserved some space in memory for that variable.

You can enjoy saving information of various data types (Data Type) such as Character, Wide Character, Integer, floating-point, double floating point, Boolean, . Based on the data type of a variable, the system will allocate memory and decide what can be stored in that reserved memory.

Primitive data types in C / C ++

The English name is Primitive Type, it can also be called the original data type, the data type is available in C / C ++. Besides these original data types, C / C ++ also provides user-defined data types. The following table lists 7 basic data types in C / C ++:

Data typeBoolean keyword bool Character char Integer int Float number Double double form No value void type Wide character wchar_t

Some basic types can be modified by using one or more of these modifiers:

  1. signed (stamped type)
  2. unsigned (type without accents)
  3. short
  4. long

The following table shows the variable type, the amount of memory it uses to store the value in memory, and the maximum and minimum values ​​that can be stored with those types of variables:

Type Bit width range of 1 byte -127 to 127 or 0 to 255 unsigned char 1 byte 0 to 255 signed char 1 byte -127 to 127 int 4 byte -2147483648 to 2147483647 unsigned int 4 byte 0 to 4294967295 signed int 4 bytes -2147483648 to 2147483647 short int 2 bytes -32768 to 32767 unsigned short int Range 0 to 65,535 signed short int Range -32768 to 32767 long int 4 bytes -2,147,483,647 to 2,147,483,647 signed long int 4 bytes Same as long int unsigned long int 4 bytes 0 to 4,294,967,295 float 4 bytes +/- 3.4e +/- 38 (~ 7 digits) double 8 bytes +/- 1.7e +/- 308 (~ 15 digits) long double 8 bytes +/- 1.7e +/- 308 (~ 15 digits) wchar_t 2 or 4 bytes 1 wide character

The size of the variables may differ from what is shown on the table, depending on the compiler and the computer you are using.

Below is an example that will show the exact size of the various data types on your computer.

 #include using namespace std; int main() { cout << "Kich co cua char la: " << sizeof(char) << endl; cout << "Kich co cua int la: " << sizeof(int) << endl; cout << "Kich co cua short int la: " << sizeof(short int) << endl; cout << "Kich co cua long int la: " << sizeof(long int) << endl; cout << "Kich co cua float la: " << sizeof(float) << endl; cout << "Kich co cua double la: " << sizeof(double) << endl; cout << "Kich co cua wchar_t la: " << sizeof(wchar_t) << endl; return 0; } 

This example uses endl , which inserts a newline character after each line, and the << operator is used to pass multiple values ​​to the screen. We also use the sizeof () operator to get the size of the various data types.

When the above code is compiled and executed, it gives the following result (results may vary depending on the compiler and the computer you are using).

 Kich co cua char la: 1 Kich co cua int la: 4 Kich co cua short int la: 2 Kich co cua long int la: 4 Kich co cua float la: 4 Kich co cua double la: 8 Kich co cua wchar_t la: 4 

Declare typedef in C / C ++

You can create a new name for an existing data type by using typedef in C / C ++. The following simple syntax is to define a new data type using typedef:

 typedef kieu_du_lieu ten_moi; 

The following example tells the compiler that it is another name for float:

 typedef float sothuc; 

Now, the following declaration is perfectly valid and will create a real variable called vantoc:

 sothuc vantoc; 

Enum enumeration type in C / C ++

The enum enumeration type declares an arbitrary type name and a set of 0 or multiple Identifiers that can be used as values ​​of that type. Each Enumerator is a constant that is of type enumeration.

To create an Enumeration, you use the enum keyword in C / C ++. The general form of enum enumeration is:

 enum ten_cua_enum { danh sach cac ten } danh_sach_bien; 

Here, ten_cua_enum is the listing name. The list of names is distinguished by commas.

For example, the following code defines a commodity enumeration name called sanpham and variable c is the type of sanpham . Finally, c is assigned the value of maytinh .

 enum sanpham { laptop, maytinh, dienthoai } c; c = maytinh; 

By default, in the list of names, the value of the first name is 0, the second name is 1 and the 3rd name is 2, . But you can give a name a specific value by Add an Initializer (initialization value). For example, in the following enumeration, maytinh will have a value of 50:

 enum sanpham { laptop, maytinh=50, dienthoai }; 

Here, dienthoai will have a value of 51 because each name will have a greater value of the previous name of 1.

According to Tutorialspoint

Previous post: Comment in C / C ++

Next lesson: Variable type in C / C ++

4 ★ | 1 Vote

May be interested

  • What is USB Type C? Type C cable models are widely usedWhat is USB Type C? Type C cable models are widely used
    what is usb type c? usb type c is currently used very commonly in many technology devices. therefore, the type c port plays a very important role.
  • Array (Array) in C / C ++Array (Array) in C / C ++
    c / c ++ programming language provides data structures called arrays, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • Data structure in C / C ++Data structure in C / C ++
    struct in c / c ++ arrays in c / c ++ allow you to define several types of variables that can hold the values ​​of several members of the same data type. but the structure is another type of data in the c / c ++ programming language, which allows you to combine other types of data.
  • Instructions for formatting data in ExcelInstructions for formatting data in Excel
    excel spreadsheets support a variety of data types for cells in the worksheet: numeric, percentage, date, time, fraction, etc., with different data, you should format the data type correctly. for that data box. so when calculating or processing data
  • Array in Language CArray in Language C
    the c programming language provides a data structure called an array, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
  • Array in PythonArray in Python
    arrays are a fundamental part of all programming languages, it is a collection of elements of a single data type, for example, integer arrays, string arrays. however, in pythong, there is no original array data structure. so we use python lists instead of arrays.
  • How to Use Type to Siri to Eliminate Public EmbarrassmentHow to Use Type to Siri to Eliminate Public Embarrassment
    you can use type to siri to skip awkward phone conversations while still getting the answers you need.
  • Instructions on how to format data in ExcelInstructions on how to format data in Excel
    instructions on how to format data in excel 2013. 1. format text. 1.1 font format, type, font size: - select the data to format - home - font - quickly select the font, font size, type as shown: where: + b (bold): type c
  • What is USB type A? What is the connectivity of USB type A?What is USB type A? What is the connectivity of USB type A?
    what is usb type a? what is the connectivity of usb type a? outstanding advantages of usb type a compared to other types.