Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Toán tử Stringize (#) và Token-pasting (##) trong C


Toán tử Stringize (#): tiền xử lý chuyển token thành chuỗi. Dùng trong định nghĩa macro.

Ví dụ

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

Kết quả

This is a string without double quotes

Toán tử Token Pasting (##): nối hai token thành một.

Ví dụ

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

Kết quả

2050