문제

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

#define stringize(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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top