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

ตัวแปรและคำสำคัญในภาษา C

ตัวแปร (Variables)

ที่เก็บข้อมูล แต่ละตัวแปรใช้หน่วยความจำต่างกัน ชื่อตัวแปรต้องไม่ขึ้นต้นด้วยตัวเลข ประกอบด้วยอักษร ตัวเลข _ ได้

รูปแบบการประกาศ

type variable_name;

ประกาศหลายตัวแปร

type variable_name1, variable_name2, variable_name3;

ตัวอย่าง

#include <stdio.h>
int main() {
    char a1 = 'H';
    int b = 90, c = 150;
    float _d = 3.4;
    printf("Character value : %c\n", a1);
    printf("Integer value : %d\t%d\n", b, c);
    printf("Float value : %f", _d);
    return 0;
}

ผลลัพธ์

Character value : H
Integer value : 90  150
Float value : 3.400000

คำสำคัญ (Keywords)

คำสงวน 32 คำ มีความหมายเฉพาะ ช่วยให้ใช้งานฟีเจอร์ของ C ได้

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
continueforsignedvoid
doifstaticwhile
defaultgotosizeofvolatile
constfloatshortunsigned

ตัวอย่าง

#include <stdio.h>
int main() {
    char c = 'H';
    int b = 6, c;
    float _d = 7.6;
    if (b < 5)
        printf("Character Value : %c", a1);
    else
        printf("Float value : %f", _d);
    return 0;
}

ผลลัพธ์

Float value : 7.600000