Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c - Adding two numbers without using operators

I found this following code for addition of two numbers without using the + operator.

code to add 3 and 4:

printf("%d",printf("%*c%*c",3,' ',4,' '));

Now printf() returns the number of characters in the result and %*c ignores the next character that it encounters. But still, I am not able to understand this code. Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

printf("%*c", n, c) prints the character c, n times. So the code prints 3 spaces followed by 4 spaces, and printf returns the number of characters printed, which is obviously 3 + 4, completing the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...