Question: 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
> 
> 
>

Answer: 
`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.
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`.