如何在C中将字符串常量转换为预处理标记

dan*_*sdc 6 c c++ macros

我想使用宏将字符串常量转换为预处理令牌.例:


    // get the first character of marco argument to postfix of new data type.
    #define TYPE(typename) Prefix ## typename #typename[0]
    void main()
    {
        TYPE(int) a, b, c; // Prefixinti a, b, c;
        TYPE(float) x, y, z; // Prefixfloatf x, y, z;
        a = 3;
    }

在C/C++中有可能吗?
p/s:抱歉我的英语不好.
编辑

Bat*_*eba 1

即使使用 C++ 中的模板也是不可能的。顺便说一句,避免typename在 C 代码中使用,因为它是 C++ 中的关键字,因此您的 C 代码将很难移植。

也不void main()是严格意义上的便携;int main()代替使用。

  • `typename` 没有什么区别,因为预处理器在编译器之前运行。 (4认同)