How Many Characters Output?

I don’t think I’ve ever used the return value from a printf() statement. Yes, the function has an int return value, which is the number of characters output. This value can be difficult to guess.

Here’s your sample code, which I use throughout this Lesson:

2022_02_26-Lesson.c

#include <stdio.h>

int main()
{
    int r;

    r = printf("Goodbye, cruel world!\n");

    printf("%d characters output\n",r);

    return(0);
}

The program outputs a string, then the number of characters output:

Goodbye, cruel world!
22 characters output

The string "Goodbye, cruel world!\n" contains 23 characters when you count the \n escape sequence as two characters. But the compiler sees it as a single character (the newline), which is what’s output. The printf() function accurately returns the number of characters output.

Now change Line 7 in the code to read:

r = printf("%d * %d = %d\n",400,500,400*500);

The format string contains 14 characters, of which 13 are single characters (remember that \n is one character). The output, however, is longer:

400 * 500 = 200000
19 characters output

A diversion C programming nerds like to play on each other is to guess the printf() statement’s return value. The format string can get quite complex, which thrills the nerds to no end:

r = printf("%*s\n",34,"Wow!");

The above printf() statement uses the * variable width item in the %s placeholder. The output is based on the second argument 34. So the string "Wow!" is output within a chunk of spaces 34 characters wide, right-justified. Then comes the newline.

Can you guess the value of variable r after the above statement executes?

[tick-tock…]

My first guess — impulsive and irrational — was 38. Wrong. I figured 34 spaces plus "Wow!" and I missed the newline. But this incorrect and hasty assumption forgot how the * wildcard works. Regardless, run the code to view the correct answer:

                              Wow!
35 characters output

If the * width is 34, the string output is always 34 characters long, plus one for the newline. The return value confirms the number of characters output.

As I wrote earlier, I don’t recall ever using the printf() return value in my code. I’ve never had a practical reason for its application. After all, it seems a bit tardy: If you’re concerned about outputting characters of a given width, the value returned from printf() is available only after the text is output.

The man page explains that most printf() family functions return the number of characters output, minus the trailing null character, \0. The snprintf() and vsnprintf() functions are different: They return the number of characters that would be output if the output size weren’t restricted.

While its return value offers the exact number of characters output, another feature of the printf() function allows you to monitor the number of characters output at a given position within the format string. This oddball is the %n conversion character — the use of which is fraught with danger and intrigue. I write more about this placeholder in next week’s Lesson.

Leave a Reply