Trong hướng dẫn này, chúng ta sẽ tìm hiểu về các kiểu dữ liệu cơ bản như int, float, char, v.v. trong lập trình C ++ với sự trợ giúp của các ví dụ.
Trong C ++, kiểu dữ liệu là khai báo cho các biến. Điều này xác định loại và kích thước của dữ liệu được liên kết với các biến. Ví dụ,
int age = 13;
Ở đây, tuổi là một biến của loại int
. Có nghĩa là, biến chỉ có thể lưu trữ số nguyên 2 hoặc 4 byte.
Các kiểu dữ liệu cơ bản của C ++
Bảng dưới đây cho thấy các kiểu dữ liệu cơ bản, ý nghĩa và kích thước của chúng (tính bằng byte):
Data Type | Meaning | Size (in Bytes) |
---|---|---|
int | Integer | 2 or 4 |
float | Floating-point | 4 |
double | Double Floating-point | 8 |
char | Character | 1 |
wchar_t | Wide Character | 2 |
bool | Boolean | 1 |
void | Empty | 0 |
Bây giờ, chúng ta hãy thảo luận chi tiết hơn về các kiểu dữ liệu cơ bản này.
1. C ++ int
- The
int
keyword is used to indicate integers. - Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
- For example,
int salary = 85000;
2. C ++ float và double
float
anddouble
are used to store floating-point numbers (decimals and exponentials).- The size of
float
is 4 bytes and the size ofdouble
is 8 bytes. Hence,double
has two times the precision offloat
. To learn more, visit C++ float and double. - For example,
float area = 64.74;
double volume = 134.64534;
Như đã đề cập ở trên, hai kiểu dữ liệu này cũng được sử dụng cho cấp số nhân. Ví dụ,
double distance = 45E12 // 45E12 is equal to 45*10^12
3. C ++ ký tự
- Keyword
char
is used for characters. - Its size is 1 byte.
- Characters in C++ are enclosed inside single quotes
' '
. - For example,
char test = 'h';
Lưu ý: Trong C ++, một giá trị số nguyên được lưu trữ trong một char
biến thay vì chính ký tự. Để tìm hiểu thêm, hãy truy cập các ký tự C ++ .
4. C ++ wchar_t
- Wide character
wchar_t
is similar to thechar
data type, except its size is 2 bytes instead of 1. - It is used to represent characters that require more memory to represent them than a single
char
. - For example,
wchar_t test = L'ם' // storing Hebrew character;
Chú ý ký tự L trước dấu ngoặc kép.
Lưu ý: Ngoài ra còn có hai kiểu ký tự có kích thước cố định khác char16_t
và được char32_t
giới thiệu trong C ++ 11.
5. C ++ bool
- The
bool
data type has one of two possible values:true
orfalse
. - Booleans are used in conditional statements and loops (which we will learn in later chapters).
- For example,
bool cond = false;
6. C ++ void
- The
void
keyword indicates an absence of data. It means “nothing” or “no value”. - We will use void when we learn about functions and pointers.
Lưu ý: Chúng ta không thể khai báo các biến void
kiểu.
Công cụ sửa đổi loại C ++
Chúng tôi có thể sửa đổi thêm một số kiểu dữ liệu cơ bản bằng cách sử dụng các công cụ sửa đổi kiểu. Có 4 công cụ sửa đổi kiểu trong C ++. Họ đang:
signed
unsigned
short
long
Chúng tôi có thể sửa đổi các kiểu dữ liệu sau bằng các công cụ sửa đổi ở trên:
int
double
char
Danh sách các loại dữ liệu được sửa đổi trong C ++
Data Type | Size (in Bytes) | Meaning |
---|---|---|
signed int | 4 | used for integers (equivalent to int ) |
unsigned int | 4 | can only store positive integers |
short | 2 | used for small integers (range -32768 to 32767) |
unsigned short | 2 | used for small positive integers (range 0 to 65,535) |
long | at least 4 | used for large integers (equivalent to long int ) |
unsigned long | 4 | used for large positive integers or 0 (equivalent to unsigned long int ) |
long long | 8 | used for very large integers (equivalent to long long int ). |
unsigned long long | 8 | used for very large positive integers or 0 (equivalent to unsigned long long int ) |
long double | 12 | used for large floating-point numbers |
signed char | 1 | used for characters (guaranteed range -127 to 127) |
unsigned char | 1 | used for characters (range 0 to 255) |
Hãy xem một vài ví dụ.
long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short d = 3434233; // Error! out of range
unsigned int a = -5; // Error! can only store positive numbers or 0
Các kiểu dữ liệu có nguồn gốc
Các kiểu dữ liệu có nguồn gốc từ các kiểu dữ liệu cơ bản là các kiểu dẫn xuất. Ví dụ: mảng, con trỏ, kiểu hàm, cấu trúc, v.v.
Chúng ta sẽ tìm hiểu về các kiểu dữ liệu dẫn xuất này trong các bài hướng dẫn sau.