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_tSome basic types can be modified by using one or more of these modifiers:
- signed (stamped type)
- unsigned (type without accents)
- short
- 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 characterThe 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 ++
You should read it
- Data type in C #
- Data types in SQL Server
- Convert data types in C #
- Data type in C programming
- Press type in C
- Chromebooks can now warn users when connecting to an 'inappropriate' USB Type C cable
- Array (Array) in C / C ++
- What is USB Type C? Type C cable models are widely used
- TYPE function - Function that returns the data type of any value in Excel
- Query XML data from a table with XML data type
- Data types in Access 2016
- What is USB Type-C, Lightning, which has a superior advantage?
Maybe you are interested
6 ways to fix the error 'PowerPoint found a problem with the content'
Sample painting of Hanoi in my heart, simple landscape
PowerPoint 2016: Working with lists in PowerPoint
How to Paste an Image into a Selection in Photoshop
Europe develops satellite internet system to replace Starlink
Intel releases new driver update package with 6GHz optimization and many other improvements