ตัวแปรใน C ผูกกับชนิดข้อมูล แต่ละชนิดใช้หน่วยความจำต่างกัน และรองรับการดำเนินการเฉพาะ
ชนิดข้อมูลพื้นฐาน
- int - จำนวนเต็ม
- char - ตัวอักษรเดี่ยว
- float - จำนวนจริงความแม่นยำเดียว (single precision)
- double - จำนวนจริงความแม่นยำคู่ (double precision)
ตารางชนิดข้อมูลรายละเอียด
| ชนิดข้อมูล | ไบต์ | ช่วงค่า |
|---|---|---|
| short int | 2 | -32,768 ถึง 32,767 |
| unsigned short int | 2 | 0 ถึง 65,535 |
| unsigned int | 4 | 0 ถึง 4,294,967,295 |
| int | 4 | -2,147,483,648 ถึง 2,147,483,647 |
| long int | 4 | -2,147,483,648 ถึง 2,147,483,647 |
| unsigned long int | 4 | 0 ถึง 4,294,967,295 |
| signed char | 1 | -128 ถึง 127 |
| unsigned char | 1 | 0 ถึง 255 |
| float | 4 | 1.2E-38 ถึง 3.4E+38 |
| double | 8 | 2.3E-308 ถึง 1.7E+308 |
รูปแบบไวยากรณ์
data_type variable_name;
ตัวอย่าง
#include <stdio.h>
int main() {
int a = 10;
char b = 'S';
float c = 2.88;
double d = 28.888;
printf("Integer datatype : %d\n", a);
printf("Character datatype : %c\n", b);
printf("Float datatype : %f\n", c);
printf("Double Float datatype : %lf\n", d);
return 0;
}ผลลัพธ์
Integer datatype : 10 Character datatype : S Float datatype : 2.880000 Double Float datatype : 28.888000