#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strcmp()
typedef enum {FALSE, TRUE} bool; // user-defined typename
// Debug flags aren't necessarily global:
bool debug = FALSE;
int main(int argc, char* argv[])
{
int i;
for(i = 0; i < argc; i++)
{
if(strcmp(argv[i], "--debug=on") == 0)
{
debug = TRUE;
break; // out of for()
}
}
bool go = TRUE;
char reply[10];
while(go)
{
if(debug)
{
// Debugging code here
printf("Debugger is now on!\n");
}
else
{
printf("Debugger is now off.\n");
}
printf("Turn debugger [on/off/quit]: ");
scanf("%s", reply);
if(strcmp(reply, "on") == 0) {debug = TRUE;} // Turn it on
else if(strcmp(reply, "off") == 0) {debug = FALSE;} // Off
else if(strcmp(reply, "quit") == 0) {break;} // Out of while(), end program
}
return 0;
}
/*
// g++ dynamic_debug_flags.c // error: redefining built-in type `bool'
// gcc OK with used-defined typename `bool':
gcc dynamic_debug_flags.c -o dynamic_debug_flags
./dynamic_debug_flags
Debugger is now off.
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: q
Debugger is now off.
Turn debugger [on/off/quit]: quit
./dynamic_debug_flags --debug=on
Debugger is now on!
Turn debugger [on/off/quit]: q
Debugger is now on!
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: quit
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
// Debug flags aren't necessarily global:
bool debug = false;
int main(int argc, char* argv[])
{
for(int i = 0; i < argc; i++)
{
if(string(argv[i]) == "--debug=on")
{
debug = true;
break; // out of for()
}
}
bool go = true;
string reply;
while(go)
{
if(debug)
{
// Debugging code here
cout << "Debugger is now on!" << endl;
}
else
{
cout << "Debugger is now off." << endl;
}
cout << "Turn debugger [on/off/quit]: ";
cin >> reply;
if(reply == "on") {debug = true;} // Turn it on
else if(reply == "off") {debug = false;} // Off
else if(reply == "quit") {break;} // Out of while(), end program
}
return 0;
}
/*
g++ DynamicDebugFlags.cpp -o DynamicDebugFlags
./DynamicDebugFlags
Debugger is now off.
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: q
Debugger is now off.
Turn debugger [on/off/quit]: quit
./DynamicDebugFlags --debug=on
Debugger is now on!
Turn debugger [on/off/quit]: q
Debugger is now on!
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: quit
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
#include <stdio.h> // for printf()
// print `A' as an int:
#define P(A) printf("%s: %d\n", #A, A) // no semicolon here
int main()
{
int a = 1, b = 2, c = 3;
P(a); P(b); P(c); // semicolons
P(a + b); // here
P((c - a)/b);
return 0;
}
/*
g++ stringizing_expressions.c -o stringizing_expressions
./stringizing_expressions
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
#define P(A) cout << #A << ": " << (A) << endl // no semicolon here
int main()
{
int a = 1, b = 2, c = 3;
P(a); P(b); P(c); // semicolons
P(a + b); // here
P((c - a)/b);
return 0;
}
/*
g++ StringizingExpressions.cpp -o StringizingExpressions
./StringizingExpressions
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
Exercise 3-31. Modify StringizingExpressions.cpp (see ch3-StringizingExpressions) so that P(A) is conditionally #ifdefed to allow the debugging code to be automatically stripped out by setting a command-line flag. You will need to consult your compiler’s documentation to see how to define and undefine preprocessor values on the compiler command line.
#include <stdio.h> // for printf()
// Conditional debugging:
#ifdef debug
#define P(A) printf("%s: %d\n", #A, A) // no semicolon here
#else
#define P(A) // nothing
#endif
int main()
{
int a = 1, b = 2, c = 3;
P(a); P(b); P(c); // semicolons
P(a + b); // here
P((c - a)/b);
return 0;
}
/*
gcc cond_debug.c -o cond_debug
./cond_debug
gcc cond_debug.c -o cond_debug -D debug // define
./cond_debug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
gcc cond_debug.c -o cond_debug
./cond_debug
gcc cond_debug.c -o cond_debug -D debug=0
./cond_debug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
gcc cond_debug.c -o cond_debug -U debug // undef
./cond_debug
gcc cond_debug.c -o cond_debug -D debug=1
./cond_debug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// Conditional debugging:
#ifdef debug
#define P(A) cout << #A << ": " << (A) << endl // no semicolon here
#else
#define P(A) // nothing
#endif
int main()
{
int a = 1, b = 2, c = 3;
P(a); P(b); P(c); // semicolons
P(a + b); // here
P((c - a)/b);
return 0;
}
/*
g++ CondDebug.cpp -o CondDebug
./CondDebug
g++ CondDebug.cpp -o CondDebug -D debug // define
./CondDebug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
g++ CondDebug.cpp -o CondDebug
./CondDebug
g++ CondDebug.cpp -o CondDebug -D debug=0
./CondDebug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
g++ CondDebug.cpp -o CondDebug -U debug // undef
./CondDebug
g++ CondDebug.cpp -o CondDebug -D debug=1
./CondDebug
a: 1
b: 2
c: 3
a + b: 3
(c - a)/b: 1
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
//#define NDEBUG
#include <assert.h> // for assert() debug macro
int main()
{
int i = 100;
assert(i != 100);
return 0;
}
/*
gcc assert.c -o assert
./assert
assert: assert.c:8: main: Assertion `i != 100' failed.
Aborted (core dumped)
gcc assert.c -o assert -D NDEBUG
./assert
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
//#define NDEBUG
#include <cassert> // for assert() debug macro
int main()
{
int i = 100;
assert(i != 100);
return 0;
}
/*
g++ Assert.cpp -o Assert
./Assert
Assert: Assert.cpp:8: int main(): Assertion `i != 100' failed.
Aborted (core dumped)
g++ Assert.cpp -o Assert -D NDEBUG
./Assert
*/