Các kiểu dữ liệu C ++

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 TypeMeaningSize (in Bytes)
intInteger2 or 4
floatFloating-point4
doubleDouble Floating-point8
charCharacter1
wchar_tWide Character2
boolBoolean1
voidEmpty0

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 and double are used to store floating-point numbers (decimals and exponentials).
  • The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float. 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 charbiế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 the char 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_tvà được char32_tgiới thiệu trong C ++ 11.

5. C ++ bool

  • The bool data type has one of two possible values: true or false.
  • 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 voidkiể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:

  1. signed
  2. unsigned
  3. short
  4. 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 TypeSize (in Bytes)Meaning
signed int4used for integers (equivalent to int)
unsigned int4can only store positive integers
short2used for small integers (range -32768 to 32767)
unsigned short2used for small positive integers (range 0 to 65,535)
longat least 4used for large integers (equivalent to long int)
unsigned long4used for large positive integers or 0 (equivalent to unsigned long int)
long long8used for very large integers (equivalent to long long int).
unsigned long long8used for very large positive integers or 0 (equivalent to unsigned long long int)
long double12used for large floating-point numbers
signed char1used for characters (guaranteed range -127 to 127)
unsigned char1used 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.









Gõ tìm kiếm nhanh...