Vaibhav Agarwal Vaibhav Agarwal - 3 months ago 7
C Question

add two numbers without using operators

I was searching for how to add two numbers without using ('+'/'++') and went through
link. But, I also found this solution:

#include<stdio.h>
int add(int x, int y);

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

int main()
{
printf("Sum = %d", add(3, 4));
return 0;
}


Can somebody explain what's happening in add function?

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

The * in the printf format means that the field width used to print the character is taken from an argument of printf, in this case, x and y. The return value of printf is the number of characters printed. So it's printing one ' ' with a field-width of x, and one with a field-width of y, makes x + y characters in total.