คำอธิบาย
Identifier (ตัวระบุ) เป็นหนึ่งใน Tokens ของภาษา C ใช้เพื่อระบุชื่อตัวแปร ค่าคงที่ ฟังก์ชัน อาร์เรย์ และข้อมูลผู้ใช้กำหนดเอง ไม่สามารถใช้ Keywords เป็น Identifier ได้ เพราะ Keywords สำรองไว้สำหรับการใช้งานเฉพาะ
กฎการตั้งชื่อ Identifier
- ชื่อต้องไม่ซ้ำกัน (Unique)
- ห้ามใช้ Keywords เป็นชื่อ
- ต้องขึ้นต้นด้วยตัวอักษร (A-Z, a-z) หรือ underscore (_)
- ห้ามมีช่องว่ (White space)
- ห้ามมีอักขระพิเศษ (Special characters)
- ประกอบด้วยตัวอักษร ตัวเลข และ underscore เท่านั้น
- มีนัยสำคัญเพียง 31 ตัวอักษรแรก
- Case-sensitive (แยกเล็ก-ใหญ่)
ตัวอย่างโปรแกรมนับสระและพยัญชนะ
#include <stdio.h>
int main() {
char str[100];
int i, vowels, consonants;
i = vowels = consonants = 0;
printf("\nEnter any String: ");
gets(str); // หมายเหตุ: gets() ไม่ปลอดภัย ควรใช้ fgets()
while (str[i] != '\0') {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
vowels++;
} else {
consonants++;
}
i++;
}
printf("\nNo of Vowels = %d", vowels);
printf("\nNo of Consonants = %d", consonants);
return 0;
}
ตัวระบุในโปรแกรมตัวอย่าง
str, i, vowels, consonants