字符串变量中使用的宏定义

Macro definitions used in the string variable

本文关键字:宏定义 变量 字符串      更新时间:2023-10-16

我想实现这个:

#include <stdio.h>
#define STR_HELLO_LEN 10
#define str(opt) STR_###opt##_LEN
int main(int argc, char **argv)
{
    char *tmp = "hello";
    int y=str(tmp);  // y = STR_HELLO_LEN,but is STR_tmp_LEN
    return(0);
}

y = STR_HELLO_LEN,但STR_tmp_LEN

这是不可能的。所有宏都在编译时解析。"hello"只在运行时真正存在。