2

What is the use of width in the printf() statement? Why is the output 7 in the example below?

Code

int add(int x, int y)
{
    return printf("%*c%*c",  x, '\r',  y, '\r');
}

int main()
{
    printf("Sum = %d", add(3, 4));
    return 0;
}

Output

Sum = 7

6 Answers 6

4

A) this is a totally crazy thing to do with printf. But since you asked...

B) the %*c says to take the next two of printf's arguments, and interpret them as a width and a character. Print that character in a field of the specified width. In this case, print a field x wide containing the character \r. Same for y and \r. Printf returns the total number of characters printed, which in this case is x+y.

So the output is actually x-1 spaces, an \r character, y-1 spaces, another \r character (or are characters left-aligned?), followed by Sum = 7.

Sign up to request clarification or add additional context in comments.

Comments

3

printf returns the number of characters output. %*c means print a character and pad to the width specified by an argument. So printf("%*c", 3, '\r') means to print a carriage return with 2 spaces before it. printf("%*c%*c", n, '\r', m, '\r') therefore prints n + m characters - that's n - 1 spaces to pad the first \r, m - 1 spaces to pad the second \r, and the two carriage returns. That's why it returns 7.

A carriage return takes you back to the start of the line, so after printing those n + m characters the next thing printed will appear at the start of the line. That means "Sum = " overwrites the output of the first printf.

Comments

1

If successful printf returns the number of characters printed. So, your printfcall in main is printing the number of characters printed by the printf in the add function, because this is what add returns.

Comments

1

Consulting any manual shows that printf returns the number of characters that were printed, excluding the null terminator. %*c takes two arguments, the first being the field width (which is thus respectively 3 and 4), and the second being the actual character printed. In this case, the character is a carriage return (\r), which you probably can't see on your terminal. Since the two characters are printed with padding, a string of length 7 is printed, and 7 is returned.

Since you can't tell that spaces and carriage returns, it looks like nothing is printed at all -- try piping the output into a file or hexdump, though.

2 Comments

Precision would be .*, not just *. Field width is what is being specified here.
@CharlesBailey: Yes indeed, fixed. Thanks!
0

printf returns the number of characters printed. In this case you are printing 7 characters by specifying x and y as output lengths. If you change the return statement to:

return printf("%*c%*c\n",  x, '\r',  y, '\r'); 

The output will be 8, since a new line is also printed.

Comments

0

Given that the name of your function is add, I highly doubt this is what you meant to do.

printf() returns the number of characters printed, which in this case is apparently 7 characters. You are then returning the value returned by printf() and printing it.

Seems like an odd thing to do. What were you expecting?

2 Comments

Anyone who downvotes an answer without explanation is a gutless lamo!
Sir, it is injustice if you did downvote me thinking I have downvoted you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.