Thứ Ba, Tháng Ba 11, 2025
spot_img
HomeData types in C/C++

Data types in C/C++

INT C++ is just a data type in C++. The article will tell you all about C++ data types.

C++ is one of the most popular programming languages ​​today. When using it, you must have knowledge about data types.

You may like to save information of diverse data types such as Character, Wide Character, integer, floating-point, double floating point, Boolean,… Based on the data type of a variable, the system allocates memory and decides what can be kept in that reserved memory.

Primitive data types in C/C++

The English name is Primitive Type, also known as the original data type, a data type available in C/C++. Besides these native data types, C/C++ also provides user-defined data types. The following table lists 7 basic data types in C/C++:

Data type Keywords
Boolean bool
Characters char
Integer int
Real number float. float
Real number of Double form double
Type has no value void. void
Wide character type wchar_t

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

  • signed (signed style)
  • unsigned (unsigned type)
  • short
  • long. 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 held with those variable types:

Type Bit width Value range
char 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 bytes -2147483648 to 2147483647
unsigned int 4 bytes 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 Similar to long int
unsigned long int 4 bytes 0 to 4,294,967,295
float. 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 in the table, depending on the compiler and computer you are using.

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

#include  
using namespace std; 
int main() 
{ 
   cout 

This example uses endlwhich inserts a newline character after each line, and the sizeof() operator to get the size of various data types.

When the above code is compiled and executed, it gives the following result (the result may vary depending on the compiler and 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

Declaring typedef in C/C++

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

typedef kieu_du_lieu ten_moi;

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

typedef float sothuc;

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

sothuc vantoc;

Enum enumeration type in C/C++

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

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

enum ten_cua_enum { danh sach cac ten } danh_sach_bien;

Here, name_cua_enum is the enumeration type name. List of names separated by commas.

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

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

By default, in a list of names, the value of the first name is 0, the second name is 1, and the third name is 2, etc. But you can give a name a specific value by add an Initializer (initial 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 value of 1 greater than the previous name.

Datatype Modifier

As the name suggests, datatype modifiers are used with existing data types to modify the length of data of a specific type.

Data types editors available in C++ include:

  • Signed – Signed
  • Unsigned – Not signed yet
  • Short – Short
  • Long – Long

The table below summarizes the edited sizes and ranges of available data types when combined with the appropriate type editor:

Data Type

Size (in bytes)

Range

short int

2

-32,768 to 32,767

unsigned short int

2

0 to 65,535

unsigned int

4

0 to 4,294,967,295

int

4

-2,147,483,648 to 2,147,483,647

long int

4

-2,147,483,648 to 2,147,483,647

unsigned long int

4

0 to 4,294,967,295

long long int

8

-(2^63) to (2^63)-1

unsigned long long int

8

0 to 18,446,744,073,709,551,615

signed char

1

-128 to 127

unsigned char

1

0 to 255

float. float

4

-3.4×10^38 to 3.4×10^38

double

8

-1.7×10^308 to 1.7×10^308

long double

12

-1.1×10^4932 to 1.1×10^4932

wchar_t

2 or 4

1 character width

Note: The above values ​​may vary by compiler.

In short, when starting to code in any programming language, the first thing you need to understand is what data types are. Once you understand how to use data types in C++, you will quickly learn how to use them when developing applications. Basically, you need to remember the following about data types in C++:

  • C++ has 3 data types: primitive, abstract and derived.
  • Primitive data types include integer, floating-point, character, boolean, double floating-point, valueless or void, and wide character.
  • Abstract or user-defined data types include class, enum, union, structure, typedef.
  • Derived data types include arrays, functions, pointers, and references.
  • Other data editors are short, long, signed and unsigned to which you can apply data types such as int, double, char…

Previous lesson: Comments in C/C++

Next lesson: Variable types in C/C++

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments