Talk:C Programming/Preprocessor

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

It seems that an small has appeared int the #define paragraph :

#define ABSOLUTE_VALUE( x ) ( ((x) < 0) ? -(x) : (x) )
int x = -10;
int j = ABSOLUTE_VALUE( x++ );

--> x = -8 and not -7 as said, because althought the x++ is expanded 3 times, it is executed only 2 in the flow.

[edit] Calculated Includes and X-Macros

I asked this on the wikipedia discussion page and it was suggested that it be brought up here.

There is no explanation of this in this page, and what are some real world examples of how to use it - instead of the simple:

#define MACRO "someheader.h"
#include MACRO

Is it possible to combine X-Macro capabilities with calculated includes? How would you do it?

[edit] #define for autoincrementing variables

I am missing the preprocessor possibility, how to automaticly generate variables with incrementing "index", i.e. Var1, Var2, Var3, ... incrementing for every appearence of a special preprocessor directive. Finally the highest index can be used by a further directive. -- 91.96.72.228 (talk) 14:17, 27 February 2009 (UTC)

I'm not sure that's very relevant to this book. There's no built-in way to do what you ask, although __LINE__ combined with token pasting (##) can be used to get a similar effect in many cases. If the book doesn't mention __LINE__ and __FILE__, it should. -- James Dennett (talk) 19:55, 28 February 2009 (UTC)