Domanda

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"?

È stato utile?

Soluzione

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.

Altri suggerimenti

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?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top