tags:

views:

88

answers:

7

Example =

#define a 100
#define b 200
main()
{
  int c=a+b;
}

After preprocrssing

Output-

#define a 100
main()
{
 int c=a+200;
}
A: 
#define a 100
#define b 200
main()
{
 #undef b 
 int c=a+b;
}
Preet Sangha
It should not increment line numbers.
not really :-) the #define would be preprocessed out, but he wants it in his output.
Peter Miehle
nope I'm confused
Preet Sangha
yes that's my question
A: 

No. What's your real goal? If you want to do, it's better to make some script to do that.

tisphie
A: 

under unix you can play with the unifdef command

Peter Miehle
A: 

If you don't want to preprocess a macro, just don't use it:

#define a 100
//#define b 200

int main()
{
    int c = a + 200;

    return 0;
}
Donotalo
A: 

The purpose of preprocessing is to process the preprocessing directives. I don't know of any way to keep a specific preprocessing directive, especially because the next step, compilation, doesn't know anything about these.

This is another reason why you should be careful when using macros -- when the name is found in the source, then it is substituted, no matter what:

#define a 100

main()
{
    int a; // Compilation error happens here
    ....
}
Secure
+1  A: 

You could try refactoring the macros to allow external configuration, i.e.

/* config.h */

/* set defaults for a and b */
#ifndef a
#define a 100
#endif

#ifndef b
#define b 200 
#endif

and

/* main.c */

#include "config.h"

int main(void) 
{
    int c = a + b;
}

Then you can set the macros externally when building, for instance like this:

$ gcc -E -Da=a main.c
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"


# 1 "config.h" 1
# 4 "main.c" 2

int main(void)
{
    int c = a + 200;
}
$ gcc -E -Db=b main.c
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"


# 1 "config.h" 1
# 4 "main.c" 2

int main(void)
{
    int c = 100 + b;
}

Now, of course you don't have to use a separate configuration header, but I'd recommend it from a maintenance perspective, it will make it easier to keep track of available configuration settings and their defaults.

Christoffer
Thanks for your reply.Can you please tell me How does it work? and Does Value of macro remains same?
@user470143 Um, with the external configuration you set the macro 'a' to be literal 'a'. Using a simpler preprocessor like unifdef or Coan with the suggested refactoring, you can do precisely the source code transformation you seek. (GCC preprocessor adds some extra stuff, as you can see in the output above)
Christoffer
One more problem I want to discuss is that I don't want to change number of lines in the C file.In this answer I have to make change #define a 100 into#ifndef a#define a 100#endifso line number in source code get increased.
Is that possible.
@user470143 Yes, use a separate configuration header. That way you will have the same line count in the _C_ file.
Christoffer
A: 

Regarding Peter Miehle's answer, unifdef can be used to partially process #if and #ifdef directives, but it cannot be used to selectively expand macros. See http://dotat.at/prog/unifdef

Tony Finch