Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert string constant to preprocessing token in C

Tags:

c++

c

macros

I want to convert string constant to preprocessing token using macro. Example:


    // 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;
    }

is it possible in C/C++?
p/s: sorry for my poor English.
edited

like image 663
dangkhoasdc Avatar asked May 16 '13 08:05

dangkhoasdc


1 Answers

#define TYPE(first_letter, rest) Prefix ## first_letter ## rest ## first_letter

typedef int TYPE(i,nt);
typedef float TYPE(f,loat);

int main(void)
{
  TYPE(i,nt) a, b, c; // Prefixinti a, b, c;
  TYPE(f,loat) x, y, z; // Prefixfloatf x, y, z;
}
like image 117
pmg Avatar answered Oct 11 '22 13:10

pmg