For example I'd like to make a debug macro which prints a code string before attempting to execute it. I suppose it should look something like that
#define TRACE(string) printf("Trying to execute: %s\n",\"string\"); \
string
...
void foo() {
printf("1\n");
}
void bar() {
printf("2\n");
}
int main() {
...
foo();
TRACE(bar(););
...
}
With expected output
...
1
Trying to execute: bar();
2
...
Well, THIS is not how one does it: compiler complains about illegal syntax. Is there a way to do it at all?
You need to use stringification using #
:
#define TRACE(string) printf("Trying to execute: %s\n",#string); \
string
Full example:
#include <stdio.h>
#define TRACE(string) printf("Trying to execute: %s\n",#string); \
string
void foo() {
printf("1\n");
}
void bar() {
printf("2\n");
}
int main() {
foo();
TRACE(bar(););
}
output:
1
Trying to execute: bar();
2
live example on ideone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With