Difference between revisions of "C programming/Debug"

From Teknologisk videncenter
Jump to: navigation, search
m (Created page with "=Simple= <source lang=c> #include <stdio.h> // debugging macros so we can pin down message origin at a glance #define WHERESTR "[file %s, line %d]: " #define WHEREARG __FILE__,...")
 
m
 
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
#define DEBUGPRINT(_fmt, ...)  DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
 
#define DEBUGPRINT(_fmt, ...)  DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
 
//...
 
//...
 
  
 
int main(main) {
 
int main(main) {
Line 20: Line 19:
  
 
Compile and run
 
Compile and run
<source lang=cli>
+
<source lang=bash>
 
[root@oldmars bin]#gcc debug.c -o debug
 
[root@oldmars bin]#gcc debug.c -o debug
 
[root@oldmars bin]#./debug
 
[root@oldmars bin]#./debug
Line 27: Line 26:
 
Done
 
Done
 
</source>
 
</source>
 +
 +
[[Category:C]]

Latest revision as of 16:01, 24 November 2020

Simple

#include <stdio.h>
// debugging macros so we can pin down message origin at a glance
#define WHERESTR  "[file %s, line %d]: "
#define WHEREARG  __FILE__, __LINE__
#define DEBUGPRINT2(...)       fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...)  DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
//...

int main(main) {
    int x = 9;
  printf("Real code....\n");
  DEBUGPRINT("I am a debug string...., x=%d\n", x);
  printf("Done\n");
return(0);
}

Compile and run

[root@oldmars bin]#gcc debug.c -o debug
[root@oldmars bin]#./debug
Real code....
[file debug.c, line 13]: I am a debug string...., x=9
Done