Send Close Add comments: (status displays here)
Got it!  This site "www.robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Debugging output using defined macros
by RS  admin@robinsnyder.com : 1024 x 640


1. Debugging output using defined macros
This page covers how to use the define in a macro form in C to get better control over the debugging output using a verbosity variable, here called show1.

2. Production code
Almost all production code has some form of diagnostic debugging output controlled by some verbosity switch.

At the operating system level, Windows, Linux, Android, etc, for example, create events that are saved for diagnostic use with ways to filter events to see what is happening.

3. Diagnostic output
The program inputs an integer variable (without prompt) that determines whether diagnostic output is shown according to the following values.

4. Without macros
Here is the code to use debugging variables as has been done to this point.

The program reads in the debugging output variable value.

Then it reads two integers, computes the sum, and outputs that sum. Here is the C code [#1]


5. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


6. Macro solution
To make the code more concise and readable, the following macros are introduced.

Note that it is not good style in a program to use single-line if statements with one statement. But here, the macro is doing the work and we never see that single-line if statement.

7. Call by text
The macro definition feature works with a call by text method. The three dots "..." are used to indicate consider all arguments provided as text for replacement.

The __VA_ARGS__ variable includes all the text provided as an argument to the macro.

Now how cleaner the code looks and how easy it is to change.

8. Change the macros
To automatically remove all debugging output code from the executable program, one can use the following defines.

Note: These defines can also be controlled at a meta level and from arguments provided from the command line.

9. Includes
Note that these debugging macro definitions could be put in a separate header file and then included so that the same definitions can be shared across many code files.

Note: If the compiler complains about a missing code file, one can create an empty one with a comment and, if necessary, one function or variable. Here is the C code [#4]


10. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


11. Efficiency
The use of this macro is very human efficient in programming. This type of debugging code is not in a time-critical place. So the effect is as if one wrote a separate if statement for each such output statement. But the computer is doing this work and makes it easier for the programmer.

12. Toggle output on and off
Finally, the program below toggles debug output on and off at the compile level using a preprocessor symbol SHOW1. Thus, all debug output in the generated/compiled code can be turned on or off in one place.

And, if necessary, the symbol SHOW1 can be set from the command line when the program is compiled. Notice now, if SHOW1 is not defined, it does not matter what the variable show1 contains, only production output is done.

13. Multi-platform code
Such symbols and a preprocessor, such is in C, C++, C#, etc., are often used in production code to include or not include certain code depending on the target platform, such is Windows, Linux, Apple, Android, etc. Here is the C code [#5]


14. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


15. Summary
The C (and C++) macro and directive preprocessor provide a powerful meta-programming way to control code generation in software projects at any level.

16. End of page

by RS  admin@robinsnyder.com : 1024 x 640