READ: Function printf The C Programming Language § 7.2 pp 125-6

The printf Function

  1. Our first function is the printf function. A function is a named piece of C code. We write programs that use functions to do work for us and maybe return a value that we will use in the main program.
  2. We use a function just like in a math class. We write its name followed by parentheses that contain values and formulas plugged into it. In computer science when we use a function, we say we have "called the function". The plug-ins are called the function's arguments.
  3. The formulas are evaluated, that is, C finds the value of each formula. Then the statements of the function run; they use the values of the function's arguments. Once the function has finished, the calling program (the one that used the function) goes on to its next statement.
  4. The purpose of function printf is to print its arguments.
  5. We will be writing our own functions later in this course.
  6. Name: The f in printf is for formatted printing.
  7. Usage: Normally function printf is used with one or more arguments. The statement printf("Hello, world!\n"); prints the characters of its argument:
    Hello, world!
    on the screen. The last character printed by function printf is the newline character ('\n'). That causes a movement down one line and back to the left. The next print will start in the line directly below Hello, world! right under the H.
  8. The first argument to printf is always a string. Often the first argument provides a template and formatting information for the other arguments. Consider a slight modification of the printf statement in first.c:
      printf("This is program %d its my %s!\n", 6+4, "tenth");
    
    There are three arguments separated by commas:
      "This is program %d its my %s!\n" 
      6+4 
      "tenth"
    
    What will be printed is the first string but with substitutions for %d and %s. %d and %s are conversion specifications. They control how the other arguments will be converted to characters that are put into the first argument. The %d specification requires that the value of the argument corresponding to it (argument: 6+4; value: 10) be an int and that the specification will be replaced by decimal digits corresponding to the value. So a '1' followed by '0' is put in place of %d. If the specification had been %o, the int 10 would have been converted to octal digits: a '1' followed by '2' and that would have been put in place of the %o. The %s specification requires that that the argument corresponding to it (the "tenth") be a string. The characters of the string are put in place of the %s.
  9. Other conversion specifications are %f which expects a floating point number and %c which expects an int. If the int is small enough, it is used as a character code and the character code is printed.
  10. Between the percent and the conversion letter can be nothing, or number, or a number followed by a decimal point followed by a second number, or a decimal point followed by a number.
    %5s
    %5.2f
    %.2f
    %5.2d
    
    The number before the decimal point (or if there is no decimal point) is the minimum field width. You are guarateed that the correspoinding argument will be printed in a field of at least that size. If the argument is small, it will be padded by blanks of zeros. The number coming after the decimal point is the precision. If the argument is a string, the precision is the maximum number of characters to be printed. If the argument is an int, it is the minimum number of digits to be printed If the argument is floating point, it is the number of digits to be printed after the decimal point.

Next: Homework 01