gpt4 book ai didi

C 预处理器流控制取决于函数参数

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:50 25 4
gpt4 key购买 nike

我想做的是这样的

#define TRIPLE_LOOP(code)\
//if there is something in code \
for(...) for(...) for(...) { code }\
//if code is empty then\
SOME_OTHER_CODE

这样

TRIPLE_LOOP(printf("muhahaha"))

将在输出的三重循环内产生 printf和

TRIPLE_LOOP()

将产生 SOME_OTHER_CODE这可能吗?

最佳答案

这几乎对我有用:)

#include <stdio.h>

#define NARGS2(_1, N, ...) N
#define NARGS(...) NARGS2(__VA_ARGS__, 1, 0)
#define TRIPLELOOP(...) \
do { \
if (NARGS(__VA_ARGS__)) { \
int i, j, k; \
for (i=0; i<2; i++) { \
for (j=0; j<2; j++) { \
for (k=0; k<2; k++) { \
__VA_ARGS__; \
} \
} \
} \
} else { \
printf("NO ARGS"); \
} \
} while (0)

int main(void) {
TRIPLELOOP(printf("haha"); puts("!"));
TRIPLELOOP();
}

它也是works at ideone .

关于C 预处理器流控制取决于函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com