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?
The
*in theprintfformat means that the field width used to print the character is taken from an argument ofprintf, in this case,xandy. The return value ofprintfis the number of characters printed. So it’s printing one' 'with a field-width ofx, and one with a field-width ofy, makesx + ycharacters in total.