Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ชนิดข้อมูล (Data Types) ในภาษา C

ตัวแปรใน C ผูกกับชนิดข้อมูล แต่ละชนิดใช้หน่วยความจำต่างกัน และรองรับการดำเนินการเฉพาะ

ชนิดข้อมูลพื้นฐาน

  • int - จำนวนเต็ม
  • char - ตัวอักษรเดี่ยว
  • float - จำนวนจริงความแม่นยำเดียว (single precision)
  • double - จำนวนจริงความแม่นยำคู่ (double precision)

ตารางชนิดข้อมูลรายละเอียด

ชนิดข้อมูลไบต์ช่วงค่า
short int2-32,768 ถึง 32,767
unsigned short int20 ถึง 65,535
unsigned int40 ถึง 4,294,967,295
int4-2,147,483,648 ถึง 2,147,483,647
long int4-2,147,483,648 ถึง 2,147,483,647
unsigned long int40 ถึง 4,294,967,295
signed char1-128 ถึง 127
unsigned char10 ถึง 255
float41.2E-38 ถึง 3.4E+38
double82.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