Vaibhav Agarwal Vaibhav Agarwal - 1 year ago 83
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:

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 Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download