3 回答
TA贡献1757条经验 获得超7个赞
下面xstr定义的宏将在进行宏扩展后进行字符串化。
#define xstr(a) str(a)
#define str(a) #a
#define RECORDS_PER_PAGE 10
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " xstr(RECORDS_PER_PAGE) \
" LIMIT " xstr(RECORDS_PER_PAGE) ";"
TA贡献1829条经验 获得超6个赞
#include <stdio.h>
#define RECORDS_PER_PAGE 10
#define TEXTIFY(A) #A
#define _REQUEST_RECORDS(OFFSET, LIMIT) \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " TEXTIFY(OFFSET) \
" LIMIT " TEXTIFY(LIMIT) ";"
#define REQUEST_RECORDS _REQUEST_RECORDS(RECORDS_PER_PAGE, RECORDS_PER_PAGE)
int main() {
printf("%s\n", REQUEST_RECORDS);
return 0;
}
输出:
SELECT Fields FROM Table WHERE Conditions OFFSET %d * 10 LIMIT 10;
注意间接到_REQUEST_RECORDS以在对字符串进行字符串化之前评估参数。
TA贡献1788条经验 获得超4个赞
尝试双重转义你的报价
#define RECORDS_PER_PAGE 10
#define MAX_RECORD_LEN 10
/*... */
#define DOUBLEESCAPE(a) #a
#define ESCAPEQUOTE(a) DOUBLEESCAPE(a)
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " ESCAPEQUOTE(RECORDS_PER_PAGE) \
" LIMIT " ESCAPEQUOTE(RECORDS_PER_PAGE) ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
int main(){
char * a = REQUEST_RECORDS;
}
编译给我。令牌RECORDS_PER_PAGE将通过ESCAPEQUOTE宏调用进行扩展,然后将其发送到DOUBLEESCAPE引用中。
- 3 回答
- 0 关注
- 528 浏览
添加回答
举报