6

on net: using printf to add two numbers(without using any operator) like following:

main()
{
    printf("Summ = %d",add(10,20))
    return 0;
}

int add(int x,int y)
{   
    return printf("%*d%*d",x,' ',y,' ');
}

Could anyone please explain, how this works:

return printf("%*d%*d",x,' ',y,' ');

Note: This fails when i call "sum" like following:

sum(1,1) or sum(3,-1) 
3
  • Do not know...Why this is not working for each and every input...for example "sum(1,1)"..Do I need to modify the code for each and every input.If this is the case, then this logic is wrong. Commented Aug 26, 2013 at 13:28
  • I would've gone with char dummy; return snprintf(&dummy, 1, "%*s%*s", x, "", y, ""); instead to avoid useless output. Commented Aug 26, 2013 at 13:44
  • Your function in the upper code is add. Then your question is about the function sum. Those are different functions. Commented Aug 26, 2013 at 14:26

3 Answers 3

8

There are two central concepts here:

  1. printf() returns the number of characters printed.
  2. The %*d format specifier causes 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 number).

So in effect the values being added are used as field widths, and printf() then returns the sum.

I'm not sure about the actual d formatting of the space character, at the moment. That looks weird, I would have gone 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 works and properly computes the sum as 19.

Sign up to request clarification or add additional context in comments.

5 Comments

@cHao Well sure, but of course I'd print it as a string too, with s. See the code. I just think this is clearer.
Yeah, saw the update. That makes more sense. :)
Well, ' ' is an int, so the %d formatting will print 32. And since the field width is a minimum (meaning that the output will not be truncated if it is larger than the specified width), add(1,1) will return 4. Your solution with %*s and "" is thus much better (although the trick won't work with negative numbers of course).
@Virgile That was kind of my point, but thanks for spelling it out. :) I made sum() take unsigned int args instead to make it clear it won't work with negative numbers, well spotted.
Thanks for response...Its not working when adding one "+ve" and one "-ve" number.
2

printf returns the number of printed characters.

Here, it is used in the add function to generate a string only composed of 10 + 20 spaces, by using the format string.

So the printf in the add function will return 30.

Then, this result is simply printed with printf (his main purpose).

Note: it might be evident, but this printf usage has to be avoided. It's very dirty as it generates useless outputs. Imagine: add(10000,10000)...

Comments

1

First, printf returns the number of characters it prints.

Second, in the format specifier %*d, * means the minimum number of characters to be printed, but the width is not from the format string itself, but from the additional argument.

With all together, the job is done, but it won't work well on small numbers like 1 because of %d in the format specifier, a better solution could be :

("%*c%*c", a, ' ', b,' ');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.