Kiểu dữ liệu khai báo vị trí bộ nhớ/biến. Dữ liệu có nhiều dạng: số nguyên, thực, ký tự, véc-tơ... Trong máy tính, mọi dữ liệu đều mã hóa thành chuỗi bit 0/1. Số nguyên xử lý trong ALU, số thực trong FPU.
Kiểu dữ liệu dựng sẵn (Built-in)
int, float, char, unsigned int, unsigned char, long int, double, v.v.
Ví dụ khai báo
- char string, grade='A';
- int count, index=10;
- float average=6.9;
Giải thích: char/int/float là kiểu, string/grade/count/index/average là biến.
Chương trình in kích thước kiểu dữ liệu
#include <stdio.h>
int main() {
int x = 10;
char c;
printf("Size of variable x = %ld bytes\n", sizeof(x));
printf("Size of variable c = %ld byte\n", sizeof(c));
printf("Size of short is %ld bytes\n", sizeof(short));
printf("Size of int is %ld bytes\n", sizeof(int));
printf("Size of long is %ld bytes\n", sizeof(long));
printf("Size of float is %ld bytes\n", sizeof(float));
printf("Size of double is %ld bytes\n", sizeof(double));
printf("Size of long double is %ld bytes\n", sizeof(long double));
printf("Size of char is %ld bytes\n", sizeof(char));
printf("Size of void is %ld bytes\n", sizeof(void));
return 0;
}
Kết quả (thí dụ trên hệ 64-bit)
Size of variable x = 4 bytes Size of variable c = 1 byte Size of short is 2 bytes Size of int is 4 bytes Size of long is 4 bytes Size of float is 4 bytes Size of double is 8 bytes Size of long double is 16 bytes Size of char is 1 bytes Size of void is 1 bytes