Hey, i found quite a few methods to ADD TWO NUMBERS WITHOUT USING THE + OPERATOR
, but this one is extremely weird.

What does this printf statement mean ?
What is %*d ?????

#include< iostream >
using namespace std;
int main()
{
int a,b,sum;
scanf("%d",&a);
scanf("%d",&b);
sum=printf("%*d%*d",a,a,b,b);
printf("\n");
printf("%d\n",sum);
system("pause");
return 0;
}

All i know is that printf returns the number of characters printed .

Edited 5 Years Ago by tubby123: n/a

The asterisk sets your field width to the corresponding argument. So the field width of the first number is the value of a and the field width of the second number is the value of b . Since that number of characters will be printed (with spaces for padding), printf() will return the sum of the field widths, which is the sum of a and b .

It's a clever solution, but falls flat with values of 0 because the field width does not truncate values. If you print 0 with a field width of 0, one character still gets printed. So the sum will always be at least 2.

oh ma'am... hands down .... :)
what a reply, thanks a lot.
But i have just 1 doubt, why are we printing the value of a and b twice ?

sum=printf("%*d%*d",a,a,b,b);

Once again, thanks for the ssum solution

But i have just 1 doubt, why are we printing the value of a and b twice ?

You're not. Think of %*d as specifying a pair; you need two arguments to fill in the field width and the display value. Consider this instead as a less confusing example:

printf("'%*d'\n", 5, 123);

The display value is 123, while the field width is five characters. The output will be

'  123'

Now back to your code, the first a is specifying the field width while the second is specifying the display value. Likewise with the two b 's.

> Hey, i found quite a few methods to ADD TWO NUMBERS WITHOUT USING THE + OPERATOR
Do any of them actually work over the full range of integers the same as regular addition would?

This method fails miserably on all negative numbers.

%d is used within quotes..to tell the compiler theres a integer value to be pllaced there either for input stream or output stream....

n printf is a fuction in header file <iostream.h> to print some output on console.....it considers from right to left while printing..(helpful to know when u r using more than one increment/decrement operator within a single printf statement))

Comments
Wrong info, chat lingo

n printf is a fuction in header file <iostream.h> to print some output on console.....it considers from right to left while printing..(helpful to know when u r using more than one increment/decrement operator within a single printf statement))

ALL of this is BS.

First, printf isn't in iostream.h, it is in stdio.h (or cstdio if you're using c++)

As for expression evaluation order, go and read this:
http://www.daniweb.com/software-development/c/threads/210533
It's a list of WILDLY different results from running the SAME code on DIFFERENT compilers, all caused by writing broken code which has undefined and unspecified behaviours.

hey salem,
1)sum = a- ~b -1
2)could also work along these lines
sum= a^b; (from the truth table)
carry=a&b;
so keep adding the sum and carry, until carry becomes 0.

This article has been dead for over six months. Start a new discussion instead.