1. Debug Mark
Applicable preprocessing #define defines one or more debug tags, which are managed using #ifdef and #endif in the code. When the program is finished debugging, you just need to use the #undef tag and the debugging code disappears. Common debugging marks are debug, statement sequence:
#define DEBUG
#ifdef DEBUG
Debugging code
#endif
2. Debug mark during run
Turn debug marks on and off during program run. Can be achieved by setting a debug bool tag. This is more convenient for programs that run on the command line. For example, the following code:
#include <iostream>
#include <string>
using namespace Std;
BOOL Debug =false;
int main (int argc,char*argv[])
{
for (int i=0;i<argc;i++)
if (string (argv[i)) = = "--debug=on")
debug = true;
BOOL Go=true;
while (GO)
{
if (Debug)
{
Debugging code
}else {}
}
}
3. Convert variables and expressions to strings
But using string operators to implement the transformation output definition
#define PR(x) cout<<#x”=”<
4, the C language assert ()
The macro is in, and when using Assert, give him an argument, an expression that is interpreted as true. The preprocessor produces the code that tests the assertion, and if the assertion is not true, an error message is sent telling the assertion what it is and if it fails for a while, the program terminates.
#include< assert>using namsapce std;
int main(){ int i=100;
assert(i!=100);
//Fails}当调试完毕后在#include
前加入#define NDEBUG即可消除红产生的代码}