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:

  • % flags field-width precision prefix type-identifier.

Type-identifiers

The type-identifiers are as follows:

  • d, i Signed integers
  • o Unsigned integers displayed in octal form.
  • u Unsigned integers in decimal form.
  • x Unsigned integers in hexadecimal form, and the hexadecimal characters a, b, c, d, e, and f printed in lowercase.
  • X Unsigned integer in hexadecimal form, and the hexadecimal characters A, B, C, D, E, and F printed in uppercase.
  • c Any value converted to unsigned char and displayed; c is used mainly for printing characters.
  • s The argument is converted to a character array and is printed; the last null in the string is not printed.
  • f Floating point.
  • e, E Floating point displayed in exponential form. It will have one digit to the left of the decimal point; the number of digits on the right side of the decimal point depends on the required precision.
  • g, G The value can be printed in floating point or exponential form. The exponential form is used if the exponent is less than –1 or if the exponent causes more places than required by the specified precision; the decimal point appears only if it is followed by a digit.
  • n This indicates to print the number of characters that are printed so far by printf.
  • p It indicates an additional argument pointer to void; the value of the pointer is converted to a sequence of characters.

Type prefixes

  • h It can appear before type indicators d, i, o, u, x, and X. It indicates that the value to be displayed should be interpreted as short; for example, short integer (hd) and short unsigned integer (hu).
  • l It can appear before type-identifiers d, i, o, u, x, and X. It indicates that the value to be displayed should be interpreted as long; for example, long integer (hd) and long unsigned integer (hu).
  • l, L Available for type-identifiers e, E, f, g, and G. It indicates that a value should be indicated as long double.

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:

  • a Alert
  • Produces a beep or flash; the cursor position is not changed.
  •  Backspace
  • Moves the cursor to the last column of the previous line.
  • f Form feed
  • Moves the cursor to start of next page.
  • New line
  • Moves the cursor to the first column of the next line.
  • Carriage Return
  • Moves the cursor to the first column of the current line.
  • Horizontal Tab
  • Moves the cursor to the next horizontal tab stop on the line.
  • v Vertical Tab
  • Moves the cursor to the next vertical tab stop on the line.
  • \
  • Prints \.
  • "
  • Prints "
  • %%
  • Prints %.

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.


C & Data Structures
C & Data Structures (Charles River Media Computer Engineering)
ISBN: 1584503386
EAN: 2147483647
Year: 2006
Pages: 232

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net