

If the string is smaller “whitespace” is added at the end. The printf(“:%-10s:\n”, “Hello, world!”) statement prints the string, but prints at least 10 characters.The printf(“:%.10s:\n”, “Hello, world!”) statement prints the string, but print only 10 characters of the string.If the string is smaller the “empty” positions will be filled with “whitespace.” The printf(“:%15s:\n”, “Hello, world!”) statement prints the string, but print 15 characters.The printf(“:%s:\n”, “Hello, world!”) statement prints the string (nothing special happens.).Let’s take another look at a printf formatted output in a more application like example:Īs you can see, the string format conversion reacts very different from number format conversions. If there is no \n then a next printf command will print the string on the same line. After printing something to the screen you usually want to print something on the next line. In this case it represents a newline character.

The \n used in the printf statements is called an escape sequence. In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision). In the fourth printf statement we want to print a float.

Print the output with a width of three digits, but fill the space with 0. In the third printf statement we say almost the same as the previous one. The result is that two “space characters” are placed before printing the character. In the second printf statement we print the same decimal, but we use a width (%3d) to say that we want three digits (positions) reserved for the output. Let us take a look at an example of printf formatted output (that why you here, isn’t it?):Īs you can see in the first printf statement we print a decimal. That’s enough on that side step of variadic function and “default argument promotions”. If you actually needed to pass, for example, a char instead of an int, the function would have to convert it back. So for example, float parameters are converted to doubles, and char’s are converted to int’s.

#Freemat too many outputs to printf how to#
We will look at how to use format specifiers to print formatted output onto the screen. In this C programming language tutorial we take another look at the printf function.
