#include <stdio.h> #include <stdlib.h> #define calc(a,b) (a*b)/(a-b) void calculate(){ int a = 20, b = 10; printf("%f\n", calc(a+4,b-2));//output 0.00000 } what to do to print the actual answer, 4.83.
#define calc(a,b) ((a)*(b))/((a)-(b)) Can you spot the extra parentheses?
--> calc(a+4,b-2) resolves to ((a+4)*(b-2))/((a+4)-(b-2)). Correct.
Your solution without the extra parentheses:
--> calc(a+4,b-2) resolves to (a+4*b-2)/(a+4-b-2). Which is very different!
The problem here is with your datatypes which are ints, not with format-specifiers. Integer division is always truncated to the whole numbers. You should consider changing your variables to float instead of int.
Try this:
#include <stdio.h> #include <stdlib.h> #define calc(a,b) (a*b)/(a-b) void calculate(){ float a = 20.0f, b = 10.0f; printf("%f\n", calc(a+4,b-2));//output 0.00000 }
(24 * 8) / (24 - 8) = 192 / 16 = 12