-1

In my code I had a macro:

#define TPS 1(or 0)
int main()
{
....
   if(var)
   {
#ifdef TPS
do something
#endif
   }
}

but now, I want to merge the if(var) with the macro so that I acheive:

int var=1;
#define TPS   (if(var))

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
#if TPS
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif
   printf("++a: %d\n", ++a);
   return 0;
 }

i.e. the block of code inside the macro conditionals should be present only if var=1 eg, for var=1:

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
   printf("++a: %d\n", ++a);
   return 0;
 }

and, for var=0:

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
   printf("++a: %d\n", ++a);
   return 0;
 }

How can I implement #define TPS to achieve this?

5
  • 1
    What exactly do you want to achieve at compilation time and at run time? Your question is not really clear, and makes me think you don't understand well the difference. Commented Jul 31, 2013 at 7:52
  • Notice that var has a value only at runtime. For the compiler, it is just a name of some static location containing an int. During compilation, var does not have any value, it has some value only at runtime! Commented Jul 31, 2013 at 8:04
  • @BasileStarynkevitch, I understand that, I updated the question, I hope it makes more sense now. I am want the preprocessor to replace TPS with some conditions in the code which can cover the entire #if #endif block Commented Jul 31, 2013 at 8:06
  • I believe you don't understand well how the C compiler (and its preprocessing) works. Commented Jul 31, 2013 at 8:08
  • Why exactly to you ask? And what real code do you have? Please explain much more what you want to achieve, and what should happen at compilation time, and at run time. Commented Jul 31, 2013 at 8:17

2 Answers 2

3

You cannot do what you are dreaming of.

Preprocessing is one of the earliest phase of the compiler (e.g. gcc). And your TPS looks like you want it to have the compilation behavior depends on runtime variable var. Conceptually, the compiler is first preprocessing your source. You can use gcc -C -E to get the preprocessed textual form.

At compilation time, a variable has a name and the compiler will find its location (but a variable does not have any value during compilation). At runtime, a variable has a location containing a value. Values don't exist at compilation time, so you can't use them in the preprocessing phase.

However, the preprocessing can be conditionnal, like

#if WANTPRINT
   printf("a: %d\n", a);
#endif

and then you could pass (or not) the -DWANTPRINT=1 flag to the compiler.

You could code

int var;
int main() {
  int a, b, c;
  a=1;b=2;c=3;
  if (var) {
    printf("a: %d\n", a);
    printf("b: %d\n", b);
    printf("c: %d\n", c);
  };
  printf("++a: %d\n", ++a);
  return 0;
}

BTW, perhaps you want to dynamically load some code at runtime? On Linux and most Posix systems you can call dlopen(3) and dlsym. You could even generate some C code in some (temporary) file, fork a process to compile it to a shared object, and dlopen that shared object, get a function pointer with dlsym then call it... See also this answer.

FWIW, Common Lisp has a very powerful macro system and is able to "compile" at runtime, and to do arbitrary computations at "compile-time". Actually, SBCL may generate good machine code while running....


Addenda

Perhaps you want to customize the behavior of the GCC compiler itself. Then you might consider using MELT (a domain specific language to extend GCC). But GCC don't enable customization of its preprocessing yet (but mostly of its middle-end, working on internal GCC representations like Gimple)

Sign up to request clarification or add additional context in comments.

3 Comments

Ok.. I think I should rephrase it, I am looking for something like a conditional something like: #define TPS do{some condition that checks the value & enables my macro}while(0)
@tarun27sh Are you looking for the #if, #ifdef, #else, #endif, etc. preprocessor directives?
Anything, if it can check my variable and then be TRUE or FALSE
0

Lose the var integer:

#define TPS 1
#if TPS == 1
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif

Just checking if it is defined can be done with #ifdef:

#define TPS
#ifdef TPS
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif

More here: http://en.wikipedia.org/wiki/C_preprocessor

2 Comments

No, I want the value #define TPS to be dependent on the variable var.
@tarun27sh as said in other answers, that's not possible because of the nature of the preprocessor. The preprocessor runs at compile time, you want to do stuff at runtime. Use a normal if for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.