The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For a , A , e , E , f and F specifiers: this is the number of digits to be printed after the decimal point by default, this is 6. For g and G specifiers: This is the maximum number of significant digits to be printed. For s : this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
The printf function formats and prints a series of characters and values to the standard output stream, stdout. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments. The format argument consists of ordinary characters, escape sequences, and if arguments follow format format specifications. The ordinary characters and escape sequences are copied to stdout in order of their appearance.
For example, the line:. When printf encounters the first format specification if any , it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. In this case, the display will be:.
Notice that because the field width was too small, the right justification fails; the two numbers do not line up along the right hand margin. A field width that requires fewer columns than the actual data value requires will always result in this failure. You may be wondering at this point how justification allows us to print tables.
So far, we have only put one value on each line, but if we print several values before each newline, we can use field widths to force them to line up. Notice in particular that the field widths always start from the last column printed.
At the beginning, the cursor is in the extreme upper left corner of the screen. The format string in the first printf statement specifies that the function should print the string "month" using 10 columns. Since "month" only requires 5 columns, printf precedes the string with five blanks. At this point, the cursor will be in the eleventh column. The next format specifier requires printf to write the string "day", also using 10 columns. The string "day" takes up three columns, so printf must precede it with seven blanks, starting from the current cursor position.
In other words, the column count begins just after the last letter of the string "month". In a situation like this, where we are printing only constants, we could simply embed the blanks in the format string and not bother with format specifiers or additional arguments. The sequence of statements:. In most situations, however, the additional arguments will be variables. As such, we cannot necessarily determine what values they will hold when the program executes. In particular, when we have numeric variables, we will not be able to predict whether the value of the variable will be a one-digit number or a 4-digit number.
Only by using field widths can we guarantee that the columns will line up correctly. Occasionally, when we write a program, we cannot even predict how large a field width we will need when a program executes. This implies that the field width itself needs to be a variable, for which the program will compute a value. Although it is rare for this situation to arise, it is worth mentioning how you can accomplish this. Just as was the case when we wanted to print the value of a variable, if we try to use a variable's name as a field width specifier, printf will simply print the name to the screen.
For example, assume that you have declared an integer variable named width and have somehow computed a value for it. To solve this problem, C uses an asterisk in the position of the field width specifier to indicate to printf that it will find the variable that contains the value of the field width as an additional parameter.
For instance, assume that the current value of width is 5. Notice that the order of the additional parameters is exactly the same as the order of the specifiers in the format string, and that even though we use the same value width twice as a field width, it must appear twice in the parameter list.
Precision specifiers are most common with floating point numbers. We use them, as you might expect from the name, to indicate how many digits of precision we want to print. Compilers have a default precision for floating point numbers. Quite often, we want to control this precision. A common example would be printing floating point numbers that represent monetary amounts. In this case, we will typically want just two digits after the decimal point.
You can see from the syntax template that a period must precede the precision specifier. The period really is a syntactic device to help the compiler recognize a precision specifier when no field width exists, but the choice of a period serves to help remind the programmer that it represents the number of places after a decimal point in a floating point number.
Just as for the field width specifier, the programmer may use a number or an asterisk as a precision specifier. The asterisk again indicates that the actual value of the precision specifier will be one of the additional parameters to the printf call. Notice that printf rounds the number when a precision specifier requires that some digits must not appear. They do have an effect when printing integers or strings, however.
When you use a precision specifier with integer data, one of two things may happen. If the precision specifier is smaller than the number of digits in the value, printf ignores the precision specifier. Here, the precision specifier is less than the number of digits in the value. With string data, the precision specifier actually dictates the maximum field width. In other words, a programmer can use a precision specifier to force a string to occupy at most a given number of columns.
It is fairly rare to use precision specifiers in this fashion, but one situation in which it can be useful is when you need to print a table where one column is a string that may exceed the field width.
In this case, you may wish to truncate the long string, rather than allow it to destroy the justification of the columns. Generally, when this happens, you will use both a field width specifier and a precision specifier, thus defining both the maximum and minimum number of columns that the string must occupy. If the value is less than ten characters long, printf will pad with leading blanks; if the value has more than ten characters, printf will print only the first ten.
Flags are fairly uncommon in format specifiers and although several flag options exist, you are most likely to use only two of them.
The first is a minus sign, which you will use in conjunction with a field width specifier. By default, whenever printf must pad output with blanks to make up a field width, the blanks precede the representation of the data. This results in right justification, as previously mentioned.
Take some time to learn printf in your language of choice, and use it when you need it. It's a powerful tool you won't regret having at your fingertips. How to use printf to format output Opensource. Get to know printf, a mysterious, flexible, and feature-rich alternative to echo, print, and cout. Image credits :. Get the highlights in your inbox every week.
Programming and development. What I learned while teaching C programming on YouTube. Sharing knowledge with others is often a great way to refresh and update your own expertise. Jim Hall Correspondent. Why I use Java. There are probably better languages than Java, depending on work requirements. But I haven't seen anything yet to pull me away. Chris Hermansen Correspondent. Why I still love tcsh after all these years.
Bash may be more popular, but tcsh has advantages that make it more appealing in certain cases. Seth Kenlon Red Hat.
0コメント