开发者

is itoa alternative exist using compiler directive macros

开发者 https://www.devze.com 2023-03-06 12:34 出处:网络
I was wondering is there can be a way to stringize an integer variable using stringizing compiler directive.

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

精彩评论

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