The printf function uses its first argument to determine how many arguments will follow and of what types they are. The printf(“:%-15.10s:\n”, “Hello, world!”) statement prints the string, but it does the exact same thing as the previous statement, accept the “whitespace” is added at the end.
If the string is smaller the “empty” positions will be filled with “whitespace.” But it will only print a maximum of 10 characters, thus only part of new string (old string plus the whitespace positions) is printed. The printf(“:%15.10s:\n”, “Hello, world!”) statement prints the string, but print 15 characters.In this case the string is shorter than 15, thus the whole string is printed. The printf(“:%.15s:\n”, “Hello, world!”) statement prints the string, but print only 15 characters of the string.The string in this case is shorter than the defined 15 character, thus “whitespace” is added at the end (defined by the minus sign.) The printf(“:%-15s:\n”, “Hello, world!”) statement prints the string, but prints at least 15 characters.
If the string is smaller “whitespace” is added at the end.
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.
printf) which take a variable number of arguments. Note: %f stands for float, but C language has also a thing called “default argument promotions”.ĭefault argument promotions happen in variadic functions. Take a look at the following list: %i or %d There are many format specifiers defined in C. If the compiler that you’re using conforms to this standard then all the features and properties should be available to you. The behavior of printf is defined in the ANSI standard. The printf function is just a useful function from the standard library of functions that are accessible by C programs. The printf function is not part of the C language, because there is no input or output defined in C language itself. The topics covered are a little printf background, format specifiers and conversions, formatting of different types and format conversions of strings.
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.