Line 1: Line 1:
<br>
+
[[Category:ECE264]] [[Category:Programming]] [[Category:C]]
  
= Debugging with Print Statements =
+
= [[ECE264]]: Debugging with Print Statements =
  
 
Although print statements are not a good way to debug code, they can be very useful for quick checks. &nbsp;However, it can be very annoy and tedious to add and delete multiple lines if they are just used for debugging. &nbsp;A quick and easy solution to this problem is to use #ifdef. &nbsp; #ifdef is very similart to an if statement; it looks to see if a variable is definded. &nbsp;If it is it will exicute the code following. &nbsp;If it is not defined it will go to the #else, if no #else follows, then nothing will happen if it is not defined.
 
Although print statements are not a good way to debug code, they can be very useful for quick checks. &nbsp;However, it can be very annoy and tedious to add and delete multiple lines if they are just used for debugging. &nbsp;A quick and easy solution to this problem is to use #ifdef. &nbsp; #ifdef is very similart to an if statement; it looks to see if a variable is definded. &nbsp;If it is it will exicute the code following. &nbsp;If it is not defined it will go to the #else, if no #else follows, then nothing will happen if it is not defined.
Line 19: Line 19:
 
To make DEBUG defined add -DDEBUG to the gcc statement when compiling. &nbsp;To skip over the #ifdef, complie with gcc like normal.
 
To make DEBUG defined add -DDEBUG to the gcc statement when compiling. &nbsp;To skip over the #ifdef, complie with gcc like normal.
  
<br>
+
----
 +
[[2011_Spring_ECE_264_Lu|Back to ECE264, Spring 2011, Prof. Lu]]
 +
 
  
 
<br> [[ECE264|Back to ECE264]]  
 
<br> [[ECE264|Back to ECE264]]  
  
 
[[Category:ECE264]]
 
[[Category:ECE264]]

Latest revision as of 05:42, 11 July 2012


ECE264: Debugging with Print Statements

Although print statements are not a good way to debug code, they can be very useful for quick checks.  However, it can be very annoy and tedious to add and delete multiple lines if they are just used for debugging.  A quick and easy solution to this problem is to use #ifdef.   #ifdef is very similart to an if statement; it looks to see if a variable is definded.  If it is it will exicute the code following.  If it is not defined it will go to the #else, if no #else follows, then nothing will happen if it is not defined.


Example code for debugging:

  1. ifdef DEBUG

printf("This will be printed.\n");

  1. endif


To make DEBUG defined add -DDEBUG to the gcc statement when compiling.  To skip over the #ifdef, complie with gcc like normal.


Back to ECE264, Spring 2011, Prof. Lu



Back to ECE264

Alumni Liaison

Questions/answers with a recent ECE grad

Ryne Rayburn