top/contents search

10. C Preprocessor

10.1 I'm trying to define a few simple little function-like macros such as

	#define square(x) x * x
but they're not always working.

10.2 Here are some cute preprocessor macros:

	#define begin	{
	#define end	}
With these, I can write C code that looks more like Pascal. What do y'all think?

10.3 How can I write a generic macro to swap two values?

10.4 What's the best way to write a multi-statement macro?

10.5 What's the difference between using a typedef or a #define for a user-defined type?

10.5b What's the difference between

	const MAXSIZE = 100;
and
	#define MAXSIZE 100

10.6 I'm splitting up a program into multiple source files for the first time, and I'm wondering what to put in .c files and what to put in .h files. (What does ``.h'' mean, anyway?)

10.7 Is it acceptable for one header file to #include another?

10.8a What's the difference between #include <> and #include "" ?

10.8b What are the complete rules for header file searching?

10.9 I'm getting strange syntax errors on the very first declaration in a file, but it looks fine.

10.10 I'm using header files which accompany two different third-party libraries, and they are ``helpfully'' defining common macros such as TRUE, FALSE, Min(), and Max(), but the definitions clash with each other and with definitions I'd already established in my own header files. What can I do?

10.10b I'm #including the right header file for the library function I'm using, but the linker keeps saying it's undefined.

10.11 I'm compiling a program, and I seem to be missing one of the header files it requires. Can someone send me a copy?

10.12 How can I construct preprocessor #if expressions which compare strings?

10.13 Does the sizeof operator work in preprocessor #if directives?

10.14 Can I use an #ifdef in a #define line, to define something two different ways, like this?

	#define a b \
	#ifdef whatever
		c d
	#else
		e f g
	#endif

10.15 Is there anything like an #ifdef for typedefs?

10.16 How can I use a preprocessor #if expression to tell whether a machine's byte order is big-endian or little-endian?

10.17 I'm getting strange syntax errors inside lines I've #ifdeffed out.

10.18 I inherited some code which contains far too many #ifdef's for my taste. How can I preprocess the code to leave only one conditional compilation set, without running it through the preprocessor and expanding all of the #include's and #define's as well?

10.19 How can I list all of the predefined identifiers?

10.20 I have some old code that tries to construct identifiers with a macro like

#define Paste(a, b) a/**/b
but it doesn't work any more.

10.21 I have an old macro

#define CTRL(c) ('c' & 037)
that doesn't seem to work any more.

10.22 Why is the macro

	#define TRACE(n) printf("TRACE: %d\n", n)
giving me the warning ``macro replacement within a string literal''? It seems to be expanding
	TRACE(count);
as
	printf("TRACE: %d\count", count);

10.23 How can I use a macro argument inside a string literal in the macro expansion?

10.24 I'm trying to use the ANSI ``stringizing'' preprocessing operator `#' to insert the value of a symbolic constant into a message, but it keeps stringizing the macro's name rather than its value.

10.25 I've got this tricky preprocessing I want to do and I can't figure out a way to do it.

10.26 How can I write a macro which takes a variable number of arguments, or use the preprocessor to ``turn off'' a function call with a variable number of arguments?

10.27 How can I include expansions of the __FILE__ and __LINE__ macros in a general-purpose debugging macro?


top

contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North