นี่เป็นอีกปัญหาหนึ่งที่ยุ่งยาก ในโปรแกรมนี้ เราจะมาดูวิธีการพิมพ์สตริงโดยใช้ภาษา C โดยที่ไม่ใส่เครื่องหมายอัญประกาศ
ที่นี่เราใช้ฟังก์ชันมาโคร เรากำลังกำหนดฟังก์ชันมาโครเช่น
#define getString(x) #x
getString() เป็นฟังก์ชันมาโคร คืนค่า x โดยแปลงเป็นสตริง # ก่อน x แสดงว่าฟังก์ชันจะแปลง x เป็นสตริง
Input: Take one string without quote Output: Print that string into console
อัลกอริทึม
Step 1:Take a string without quote Step 2: Use macro function to print it into a string Step 3: End
โค้ดตัวอย่าง
#include<stdio.h>
#define getString(x) #x
//The # will convert x into a string
main() {
printf(getString(Hello World));
} ผลลัพธ์:
Hello World