Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!ucbvax!torolab6.vnet.ibm.com!mccrady From: mccrady@torolab6.vnet.ibm.com ("Don McCrady") Newsgroups: comp.lang.c Subject: Re: Question about assertion macro Message-ID: <9106031304.AA28761@ucbvax.Berkeley.EDU> Date: 3 Jun 91 13:11:34 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 35 >>From: greg@suntan.viewlogic.com (Gregory Larkin) >>I would like to construct an assertion macro so that I >>can print the exact condition that failed as a string. >> ASSERT(foo != NULL, "Unexpected NULL pointer"); >>Greg Larkin (ASIC Engineer) > #define ASSERT_ARG(relation, msg, action) \ > if ( !(relation) ) { \ > printf("ASSERTION FAILED: File %s Line %d %s %s\n",\ > __FILE__, __LINE__, "relation", msg);\ > action; \ > } /* if assertion failed */ > > should work in standard C; I'm not sure about ANSI C, though. > From: msh30@ruts.ccc.amdahl.com (Mark Hahn) No, this won't work in ANSI C... If you use an ANSI C preprocessor, you can do this easily with the "stringize" operator, #... Try this: #define ASSERT_ARG(relation, msg, action) \ if ( !(relation) ) { \ printf("ASSERTION FAILED: " #relation "File %s Line %d %s\n",\ __FILE__, __LINE__, msg); \ action(); \ } Better yet, if you have an ANSI compiler, you should have a header file called , which will do exactly what you want; that is, print the failing assertion, the file and line number, and call the abort() function to terminate the program. Enjoy. ++Don;