ข้อเท็จจริงน่าสนใจเกี่ยวกับ C programming:
- Case labels ของ switch บางครั้งวางใน if-else ได้
ตัวอย่าง
#include <stdio.h> main() { int x = 2, y = 2; switch(x) { case 1: ; if(y==5) { case 2: printf("Hello World"); } else case 3: { } } }ผลลัพธ์
Hello World
- array[index] เขียนเป็น index[array] ได้ เพราะเข้าถึงด้วย pointer arithmetic array[5] คือ *(array + 5) 5[array] คือ *(5 + array) เท่ากัน
ตัวอย่าง
#include <stdio.h> main() { int array[10] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110}; printf("array[5]: %d\n", array[5]); printf("5[array]: %d\n", 5[array]); }ผลลัพธ์
array[5]: 66 5[array]: 66
- ใช้ <: , :> แทน [ ] และ <%, %> แทน { }
ตัวอย่าง
#include <stdio.h> main() <% int array<:10:> = <%11, 22, 33, 44, 55, 66, 77, 88, 99, 110%>; printf("array[5]: %d\n", array<:5:>); %>ผลลัพธ์
array[5]: 66
- #include ใช้ในที่แปลกได้ ถ้า abc.txt มี "The Quick Brown Fox..." include หลัง printf จะพิมพ์เนื้อหาไฟล์
ตัวอย่าง
#include <stdio.h> main() { printf #include "abc.txt"; }ผลลัพธ์
The Quick Brown Fox Jumps Over The Lazy Dog
- ข้าม input ได้ด้วย %*d ใน scanf()
ตัวอย่าง
#include <stdio.h> main() { int x; printf("Enter two numbers: "); scanf("%*d%d", &x); printf("The first one is not taken, the x is: %d", x); }ผลลัพธ์
Enter two numbers: 56 69 The first one is not taken, the x is: 69