Short answer would be - avoid macros if you can.
If you use gcc - you can run it with -E flag to get your code after preprocessor took care of it. Usually you want to get rid of #include parts, because you will get all the headers in one output file ;)
$ gcc -std=c99 -E kod.c
# 1 "kod.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "kod.c"
int main(void) {
int j, x = 3, y = 2;
printf("x / y" " =%f\n",((float)x / y));
j = (x / y);
printf("%f\n", j);
printf("%f\n", x / y);
return 0;
}
Heck if I know why it affects printf that way.
Also, compiled without the <stdio.h> header:
$ gcc -ansi -pedantic -Wall -Wextra -std=c99 kod.c && ./a.exe
kod.c: In function 'main':
kod.c:7:5: warning: implicit declaration of function 'printf'
kod.c:7:5: warning: incompatible implicit declaration of built-in function 'prin
tf'
kod.c:9:5: warning: format '%f' expects type 'double', but argument 2 has type '
int'
kod.c:10:5: warning: format '%f' expects type 'double', but argument 2 has type
'int'
x / y =1.500000
1.500000
1.500000
And with it:
$ gcc -ansi -pedantic -Wall -Wextra -std=c99 kod.c && ./a.exe
x / y =1.500000
1.500000
1.500000
Pretty strange if you ask me. I wouldn't bother tinkering with the macro more than I'd have to, so, my solution that still gives you the debug output of variable names using a macro AND a function, is as follows:
#include <stdio.h>
void dprint(float f, const char* msg) {
printf("%s = %f\n", msg, f);
}
#define dprint(x) dprint(((float)x), #x)
int main(void) {
int j, x = 3, y = 2;
dprint(x / y);
j = (x / y);
dprint(j);
printf("%f\n", j);
printf("%f\n", (float)x / y);
return 0;
}
And it runs much better, if you ask me:
$ gcc -ansi -pedantic -Wall -Wextra -std=c99 kod.c && ./a.exe
x / y = 1.500000
j = 1.000000
nan
1.500000
About inlining C functions:
Inline Functions In C