Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ตัวดำเนินการ # และ ## ใน C


ในส่วนนี้เราจะดูตัวดำเนินการ Stringize (#) และ Token Pasting (##) ใน C ตัวดำเนินการ Stringize เป็น preprocessor operator ส่งคำสั่งให้คอมไพเลอร์แปลง token เป็นสตริง ใช้ในการนิยาม macro

ด้วย stringize operator เราสามารถแปลงข้อความเป็นสตริงได้โดยไม่ต้องใช้เครื่องหมายคำพูด

ตัวอย่าง

#include<stdio.h> #define STR_PRINT(x) #x main() { printf(STR_PRINT(This is a string without double quotes)); }

ผลลัพธ์

This is a string without double quotes

ตัวดำเนินการ Token Pasting เป็น preprocessor operator ส่งคำสั่งให้คอมไพเลอร์ต่อท้ายสอง token ให้เป็นสตริงเดียว ใช้ในการนิยาม macro

ตัวอย่าง

#include<stdio.h> #define STR_CONCAT(x, y) x##y main() { printf("%d", STR_CONCAT(20, 50)); }

ผลลัพธ์

2050