ที่นี่เราจะเห็นข้อเท็จจริงที่น่าสนใจเกี่ยวกับการเขียนโปรแกรม C เหล่านี้เป็นเหมือนด้านล่าง
- บางครั้ง case label ของคำสั่ง 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: {
//case 3 block
}
}
} ผลลัพธ์
Hello World
-
array[index] สามารถเขียนเป็น index[array] เหตุผลคือการเข้าถึงองค์ประกอบอาร์เรย์โดยใช้เลขคณิตของตัวชี้ ค่าของอาร์เรย์[5] คือ *(อาร์เรย์ + 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 Jumps Over The Lazy Dog" หากเรารวมไฟล์ไว้หลังคำสั่ง printf เราสามารถพิมพ์เนื้อหาไฟล์นั้นได้
ตัวอย่าง
#include <stdio.h>
main() {
printf
#include "abc.txt" ;
} ผลลัพธ์
The Quick Brown Fox Jumps Over The Lazy Dog
- เราสามารถละเว้นอินพุตได้โดยใช้ %*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