The printf Function

printf

Introduction

printf is used to display information on screen, that is, standard output. printf is a function that returns the number of characters printed by the printf function.

Program

#include main() { int i = 0; i=printf("abcde"); // A printf("total characters printed %d ",i); //B }

Explanation

  1. Here, five characters are printed by statement A. So, i will get the value 5.
  2. Statement B prints the value of i as 5.
  3. The general format for the printf statement has a first string argument followed by any additional arguments.
  4. In statement B, "total characters printed %d " is the first string argument.
  5. i is the second argument. You may have multiple arguments, but that depends on what value you have to print. For each additional argument you will have to include a placeholder. Each placeholder begins with %. In statement B, %d is the placeholder.
  6. For the second argument i, the placeholder is %d. So when you need an integer value, you have to use %d. The placeholders are given for each data type.
  7. For example, if you want to print i and j, you may have to use two placeholders. Any material in the first string argument, other than the placeholder and characters, represents the escape sequence. In this example, the escape sequence character is , which is not printed but acts as a directive. For example, the directive indicates that the next printing should be done on a new line.

Points to Remember

  1. printf is used to direct output to standard output format.
  2. printf is a function that returns the number of characters printed.

PLACEHOLDERS

Introduction

Placeholders are used to print values of arguments supplied in print. The directives in the placeholders control printing.

Program/Example

The general form of a placeholder is:

Type-identifiers

The type-identifiers are as follows:

Type prefixes

Field-width

  1. Field-width indicates the least number of columns that will be allocated to the output. For example, if you write %4d to i and the value of i is 10, then 4 columns are allocated for i and 2 blank are added on left side of value of i. So the output is bb10. Here, b indicates blank.
  2. If the value is more than the specified column, field-width is ignored and the number of columns used is equal to the number of columns required by the arguments. So if i is 12345 then 5 columns are used, even if %4d is specified.
  3. In any circumstance, the output width is not shortened, because of field-width.
  4. If you specify * instead of field-width then you have to specify additional arguments. For example,

    printf ("%*d ", 5, 20); // A printf ("%*d ", 20, 5); // B

    In A, 5 is substituted for * and it indicates putting the value 20 in 5 columns.

    In B, 20 is substituted for * and it indicates putting the value 5 in 20 columns.

Precision

  1. Precision indicates the minimum number of digits printed for type integers d, i, o, u, x, and X. For example,

    i. printf("%10.4d ", 35)

  2. Here 10 is the field-width and 4 is the precision, so 10 columns are used for the 4-digit output. To make 35 into 4 digits, two 0s are added to the left side to make it 0035. To print 0035 in 10 columns, blanks are added to make the output bbbbbb0035.
  3. For floating arguments, precision indicates how many digits are printed after decimal points. If precision is more than the number of digits on the right side of the decimal point, 0s are added to the right side.
  4. If precision indicates too few digits, then it is ignored and the number of digits are printed as necessary.

Flags

  1. Flag characters are used to give directives for the output. You can use multiple flag characters in any order.
  2. The flag characters are as follows:

    • Indicates that output is left justified.

      printf("%-10.4d ", 25)

    • It causes the number to be printed as 0025bbbbbb. Thus, blanks are added to the right side.
    • In the absence of a flag, it is printed as bbbbbb0025.
    • + Indicates that i number is printed using a sign character (+ or —).

      printf("%+d ", -25); printf("%+d ", 25);

    • It causes printing as

      -25 +25

    • Indicates a space for positive values so that positive values and negative values are aligned. For example,

      printf("% d ", 25); printf("% d", 25);

    • It causes printing in the form of

      b25 25

    • In the first case, blank is displayed.
    • − # Indicates that the value should be converted to another form before displaying. For example, for hexadecimal values you can indicate 0X; for the floating data type, # indicates that the decimal point should always be included in the output.
    • 0 Used with whole and real numbers, 0 causes 0s to be padded to complete the field width. If the precision is specified as 0, then this flag is ignored; if the 0 and – flags are both specified, the 0 flag is ignored.

Escape Sequence

Escape sequences are the special directives used to format printing. For example, indicates that the next printing should start from the first column of the next line. Following are the escape sequences:

Points to Remember

  1. printf returns the number of characters printed; if some error occurs then it returns a negative value.
  2. Formating of printf can be controlled by using flags, field-width, etc.

Категории