There are two main concepts here:
printf() returns the number of characters printed.- The format specifier
%*d forces printf() to read two integers from its arguments and use the first to set the field width used to format the second (as a decimal).
Thus, the values ββadded are used as the width of the field, and printf() then returns the sum.
I am not sure about the actual formatting of the d space character at the moment. This looks strange, I would go with an empty string instead:
static int sum(unsigned int a, unsigned int b) { return printf("%*s%*s", a, "", b, ""); } int main(void) { unsigned int a = 13, b = 6; int apb = sum(a, b); printf("%d + %d = %d?\n", a, b, apb); return 0; }
The above work and correctly calculates the sum as 19 .
source share