您的位置:首页 > 运维架构

To find sum of two numbers without using any operator. Only use of printf() is allowed.

2014-11-30 14:05 429 查看
We can use printf() to find sum of two numbers as printf() returns the number of characters printed.

The width
field in printf() can be used to find the sum of two numbers. We can use ‘*’ which indicates the minimum width of output. For example, in the statement
“printf(“%*d”, width, num);”, the specified ‘width’ is substituted in place of *, and ‘num’ is printed within the minimum width specified. If number of digits in ‘num’ is smaller than the specified ‘wodth’, the output is padded with blank spaces(右对齐). If number
of digits are more, the output is printed as it is (not truncated).

solution:

Output:
Sum = 7

注意:Sum前面还会输出7个空格,因为add函数里面还会输出。如果想不输出前面的空格,可以用'\r'代替' ','\r'是使光标移到一行的开头。

Output:Sum = 7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐