How does printf statement work




















No check is performed to verify that the number of marks in the format string and the number of remaining parameters are consistent. In case of an error, the behavior of printf is undetermined.

The marks in the format string must have the following structure fields in brackets are optional :. Each of the names parameter, flags, width, precision, length and type represents a set of possible values that are explained next. The format marks included as part of the first parameter passed to printf offer many possibilities.

It follows a description of some examples:. This ensures that the output reaches the minimum width. This is especially useful when printing out a column of values. For example, with the next code:. Align output: By default, all output is right-justified when using the minimum field width.

You can change this and force output to be left- justified. To do so, you need to prefix the minimum field specifier with the minus sign -. Specify precision: You can put a period. You can use the precision specifier to determine the number of decimal places for floating-point numbers, or to specify the maximum field width or length for integers or strings. We want to print the string char string[30]; with the printf function:. Home Skip to content Skip to navigation.

Systems Architecture. In the format string, a blank space follows the percent sign, not a type character, so printf will simply display the percent sign.

This effectively erases the blank space. The next character in the format string is the letter 'd', which printf writes in the place where it originally wrote the blank. We mentioned above that we can have more than one format specifier in a format string. If we do this, we will need one additional argument for each of the format specifiers. Furthermore, the arguments must appear in the same order as the format specifiers.

In the preceding example, the order of the additional parameters is wrong, so the output is plainly nonsensical. You must remember that printf does not understand English, so it cannot determine which argument belongs with which format specifier. Only the order of the parameters matters. The printf function substitutes the first of the additional arguments for the first format specifier, the second of the additional arguments for the second format specifier, and so on.

Because printf uses the additional parameters to give it the values to substitute for the format specifiers, it is essential that, as a programmer, you supply enough parameters. Since it has no value for the third format specifier, it will print a garbage value. We have no way to predict exactly what value it will display it may even coincidentally be the right value! The moral of the story is that if you see strange output when you are using format specifiers, one of the first things you should check is the order and number of the additional arguments.

You might also see unexpected output for one other reason. The type character in the format specifier determines completely how printf will display a value. If you include a format specifier with a particular type character in your format string and then give an argument of a different data type, printf will display the value of the argument using the syntax for values corresponding to the type character, not the syntax corresponding to the data type of the argument. In each example, printf makes an implicit type conversion of its argument to force it to agree with the data type that the type character specifies.

In some special cases, you can use this automatic conversion to your advantage, but most of the time, you will want to ensure that the data type of the argument matches the type character. The syntax template for a format specifier given above provides for several options. These options allow the programmer to control precisely how output will appear on the screen.

In other words, if you want to use multiple options, they must appear in the same order as they do in the syntax template.

Although the first option is flags , it is actually the least common, so we will leave its discussion for last. The width option is the option you will use most frequently.

This option is often called a field width. We usually use field widths to line up columns of data to form tables on the screen.

Printing things in tables makes output more readable. We can also use field widths for other reasons as well. Since they allow us to control exactly where a value will appear on a line of the screen, we can also use field widths when we want to "draw" with characters.

The value that the format specifier indicates is sometimes called a "field" so the field width controls how many columns the field will occupy on the screen. The value that we substitute for the italicized width in an actual format specifier can be one of two things.

Most often, it will be an integer. Notice that two blank spaces precede the digit '2'; this means the entire field two blanks and one non-blank character occupies three columns. In addition, notice that the blanks come before the digit.

We call this right justification. Right justification implies that items on a single row of the screen will line up along the right margin. The field width you specify is the minimum number of columns that output will occupy. If a complete representation of a value requires more columns, printf will print the whole value, overriding the field width.

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. By default, only negative numbers are preceded with a -ve sign.

Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow.

By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed. Left-pads the number with zeroes 0 instead of spaces, where padding is specified see width sub-specifier.

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces.

The value is not truncated even if the result is larger. The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.



0コメント

  • 1000 / 1000