การพิมพ์สตริงโดยไม่ใช้เซมิโคลอน (;) ต้องทำความเข้าใจว่าการทำงานของ standard output และเหตุผลที่เซมิโคลอนถูกใช้งาน เซมิโคลอนเป็นตัวบ่งชี้สิ้นสุดคำสั่ง ในขณะที่ printf() เป็นฟังก์ชันของไลบรารี stdio ที่คืนค่าจำนวนตัวอักษรที่พิมพ์ออกมา เราสามารถใช้ค่าคืนนี้ร่วมกับโครงสร้างควบคุมที่ไม่ต้องการเซมิโคลอน เช่น if, switch, while หรือแมโคร เพื่อพิมพ์ข้อความได้
วิธีการต่างๆ
ใช้เงื่อนไข if
#include <stdio.h>
int main() {
if (printf("Tutorials Point")) { }
}
ใช้คำสั่ง switch
#include <stdio.h>
int main() {
switch (printf("Tutorials Point")) { }
}
ใช้ลูป while
#include <stdio.h>
int main() {
while (printf("Tutorials Point")) { }
}
ใช้แมโคร
#include <stdio.h>
#define OUT printf("Tutorials Point")
int main() {
switch (OUT) { }
}
ทุกวิธีข้างต้นจะพิมพ์ "Tutorials Point" ได้โดยไม่ต้องเขียนเซมิโคลอนในคำสั่งหลัก