开发者

is itoa alternative exist using compiler directive macros

I was wondering is there can be a way to stringize an integer variable using stringizing compiler directive. I tried using:

#define strin开发者_如何学运维gize(a) #a
#define h(a) stringize(a)
#define g(a,b) a##b
#define f(a,b) g(a,b) 

int main()
{
  int num = 1024;
  printf("%s=%s\n",stringize(h(f(1,2))), h(f(1,2)));  //1. h(f(1,2))=12
  printf("%s=%s\n",h(h(f(1,2))), h(f(1,2)));          //2. "12"=12
  printf("%s=%d\n", h(num),num);                      //num=1024
  return 0;
}

so as adding another level in stringize macro(#1) will make the substitution to happen first then placing it in code(#2), in similar way can variables be replaced at compile time with the values. I mean to say if var = value; then is there some way that some_macro(var) --> can stringize it into "value"?


There's no way of getting the value of a variable using the preprocessor - preprocessing (as its name suggests) takes place before compilation, and the variables do not exist at that stage.


No. The preprocessor is acting on tokens, it doesn't know about variables and their values. What would you want to get if the value was read from stdin?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜