Path: utzoo!utgpu!watmath!clyde!att!pacbell!ames!mailrus!wasatch!cs.utexas.edu!sm.unisys.com!rdahp!geoff From: geoff@rdahp.UUCP (Geoff Walsh) Newsgroups: comp.lang.c Subject: preprocessor directives in macros Message-ID: <122@rdahp.UUCP> Date: 2 Dec 88 03:25:56 GMT Reply-To: geoff@rdahp.UUCP (Geoff Walsh) Distribution: na Organization: R & D Associates, Marina del Rey, CA Lines: 60 Being a new user on the net, I'm not sure whether this has been discussed before. If so, please let me know. I'm trying to conditionally include diagnostic messages in my code that I can turn on or off at compile time. Also, I'd like to be able to set the level and type of message. I can't quite get the compiler to do what I want, so I assume its not portable to a compiler that does it. One way that does work is: #define TESTA 0 #define TESTB 6 #define TESTC 3 ... etc. #define diagprintf(TestCriteria,TestLevel,Fmt) \ if (TestCriteria >= TestLevel) printf Fmt then do: some code diagprintf (TESTB, 8, ("B condition is %d\n", bcond)); some more code This works and most compiler optimizers are smart enough to recognize dead code [if (TESTB (==6) >= 8)] and throw away the code for the rest of the expression. (The if test value can be determined at compile time.) However, the compiler will usually leave the unreferenced string: "B condition is %d\n" in the string pool. This isn't a problem for debugging, but for the final release of the code, I don't want all these messages in the final executable, even though the dead code is removed (i.e. by setting all TEST? above to zero). I tried to use the preprocessor with something like: #define diagprintf(TestCriteria,TestLevel,Fmt) \ #if (TestCriteria >= TestLevel)\ printf s\ #endif to get the preprocessor to eliminate dead code AND the strings associated with Fmt, but the compiler didn't like this. I get an error message like "Expected formal macro parameter" with one compiler, and " # operator should be followed by a macro argument name" with another, and the compiles fail. An examination of the preprocessed output shows that the preprocessor gets a bit mixed up. Other C compilers tested with each produce different, but still not the desired, results. (Both MS-D*S and U*IX C compilers were used. The compilers were Microsoft C 5.1, HP-UX 6.2 cc, gcc, and HP cc68020 cross compiler.) The questions are: 1. Is there any portable, standard way to do something like this with the preprocessor? 2. Is this legit in ANSI C and do I need to just wait for the next version of my compiler? If not, is there an ANSI C way? -- Geoff Walsh, R & D Associates, Marina del Rey, CA rdahp!geoff, rdahp!geoff@sm.unisys.com