Data type in C / C ++

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.

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

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile