Four commissioning tips in C ++ programming

xiaoxiao2021-03-06  22

Debug mark

Applicable preprocessing #define defines one or more debug marks, and manages the debugging section in the code. When the program finally debugged, just use the #undef tag, the debug code will disappear. Commonly used debug markers are Debug, statement sequence:

#define debug

#ifdef debug

Debug code

#ENDIF

2. Debug tag during operation

Turn the debug mark on and off during the program. It can be implemented by setting a debug BOOL tag. This is more convenient to run the command line.

Such as the following code

#include #include using namespace std; bool debug = false; int main (int Argc, char * argv [in) {for (int i = 0; i

3. Convert variables and expressions into strings

However, using a string operator to implement the conversion output definition

#define pr (x) cout << # x "=" << x << '/ n'

4.C language assert ()

This macro is in , when using Assert>, give him a parameter, that is, a judgment is a true expression. The preprocessor generates the code that tests the assertion. If the assertion is not true, then an error message tells the assertion and it will fail for a while, and the program will terminate.

#include using namsapce std; int main () {int i = 100; assert (i! = 100); // fails} Add #define nDebug before #include before debugging Generated code}

转载请注明原文地址:https://www.9cbs.com/read-42708.html

New Post(0)