NEW C PRIMER PLUS SECOND EDITION
CHAPTER 3 & 4 NOTES
Copyright (C) 1993 by The Waite Group
Copyright (C) 1997 by Linda Buck


 1. #define
    -------
    Form:
        #define NAME value

    Meaning:
    Before your program is compiled, the preprocessor substitutes value
    for every occurence of NAME.

    Use:
    (1) Readability
        A symbolic name makes the meaning of a constant clear.
    (2) Easy Alterability
        If you use a define and the value of a constant changes, you
        only need to change the define.

    Examples:
    (1) #define TWO 2
        sum = num + TWO;  /* sum = num + 2; */
    (2) /*** error ***/
        #define TWO = 2
        sum = num + TWO;  /* sum = num + = 2; */
    (3) /*** error ***/
        #define TW0 2;
        sum = TWO + num;  /* sum = 2; + num; */

    Note:
    Parts of a program within double quotation marks are immune to
    substitution.

    Example:
    #define DOZEN 12
    printf("DOZEN = %d\n", DOZEN);  /* printf("DOZEN = %d\n", 12); */

 2. Example 3-4.1
    -------------
    Program:
    /* fathm_ft.c -- converts fathoms to feet */
    #include <stdio.h>
    #define FEET_PER_FATHOM 6
    int main(void)
    {
      int fathoms, feet;

      printf("This program converts fathoms to feet.\n");
      fathoms = 2;
      feet = fathoms * FEET_PER_FATHOM;
      printf("There are %d feet in %d fathoms.\n", feet, fathoms);
      return 0;
    }

    Output:    
    This program converts fathoms to feet. 
    There are 12 feet in 2 fathoms.

 3. Fundamental Data Types
    ----------------------
        Family      Description               Types
    --------------  ------------  -----------------------------
    character       character     char
    integer         whole number  short, int, long
                                  unsigned short, unsigned int,
                                  unsigned long
    floating-point  real number   float, double, long double

 4. Constants
    ---------
    (1) Character
        (a) A single letter, digit, or punctuation mark contained
            between single quotation marks.
            valid char: 'A'  'a'  '1'  '.'
            invalid char: A  "A"
        (b) Escape Sequences:
            '\a' - alert
            '\b' - backspace
            '\f' - form feed
            '\n' - newline
            '\r' - carriage return
            '\t' - horizontal tab
            '\v' - vertical tab
            '\\' - backslash
            '\'' - single quote
            '\"' - double quote
        (c) ASCII code -- See ASCII Table p.665
            character  base 10  base 8   base 16
            esc           27    '\033'   '\x1b'
            del          127    '\0177'  '\X7F'

    (2) Integer
        A number without a decimal point, exponent, or comma.
        valid int: 20  -20
        invalid int: 20.0  2E1  1,000
        valid long: 20l  20L
        valid int in octal: 025
        valid int in hexadecimal: 0x1a  0X1A

    (3) Floating-Point
        Notation:
           Number      Scientific Notation  Exponential Notation
        -------------  -------------------  --------------------
                                    9
        1,000,000,000         1.0x10                1.0e9
                                    5
        123,000              1.23x10               1.23e5
                                    2   
        322.56             3.2256x10             3.2256e2
                                    -5
        0.000056              5.6x10                5.6e-5

        Form:
          signed digits with a decimal point   e or E  signed exponent

        Note:
        (a) You can omit positive signs.
        (b) You can omit a decimal point (2E5) or an exponential part
            (19.28), but not both at the same time.
        (c) You can omit a fractional part (3.E16) or an integer part
            (.45E-6), but not both at the same time.
        (d) You cannot use spaces or commas in a floating-point
            constant.

        Examples:
        valid float: 2.3f  9.11E9F
        valid double: 3.14159  .2  -1.56E+12  2.87e-3  4E16  .8E-5
        invalid double: 100  E+12  1.56 E3
        valid long double: 54.3l  4.32e4L

 5. Declarations
    ------------
    All variables must be declared before they are used.

    Form:
        type name1=const1, name2=const2, ... ;
            where (1) type = char
                             short
                             int
                             long
                             unsigned short
                             unsigned int
                             unsigned long                           
                             float
                             double
                             long double
                  (2) name = 1-31 characters
                             first character = letter
                             rest = letter, digit, underscore
                      Distinguishes between upper and lower case.
                  (3) The =const part is optional.
                      const = constant of appropriate type

    Meaning:
    For each variable in the list:
    (1) Allocate the appropriate storage.
    (2) Associate the given name with this space.
    (3) Associate the given type with this space.
    (4) If an initial value is given, initialize the space to the given
        value.

    Examples:
    char ch;
    char letter = 'a';    /* letter initialized to 'a' */
    int num1, num2;
    int i, j = 0, k = 0;  /* i not initialized
                             j and k initialized to 0 */
    double x, y, z;
    double rate = 0.15;   /* rate initialized to 0.15 */

 6. Storage
    -------
        Type         Storage  Description             Range
   ----------------  -------  ----------- ------------------------------
   char               8 bits  ASCII code         -128 to 127
   short             16 bits                   -32768 to 32767
   int(PC)           32 bits              -2147483648 to 2147483647
   long              32 bits              -2147483648 to 2147483647
   unsigned short    16 bits                        0 to 65535
   unsigned int(PC)  32 bits                        0 to 4294967295
   unsigned long     32 bits                        0 to 4294967295
   float(PC)         32 bits  exponent &      -1.0E38 to -1.0E-37,
                              fraction               0.0,
                                              1.0E-37 to 1.0E38,
                                          ~6 significant decimal digits
   double(PC)        64 bits  exponent &     -1.0E308 to -1.0E-307,
                              fraction               0.0,
                                             1.0E-307 to 1.0E308,
                                          ~15 significant decimal digits
   long double(PC)   80 bits  exponent &    -1.0E4932 to -1.0E-4931,
                              fraction               0.0,
                                            1.0E-4931 to 1.0E4932,
                                          ~18 significant decimal digits

   Example:
   Character  Storage - See ASCII Table p.665
   ---------  -------
     '0'         48
     '1'         49
     ...        ...
     '9'         57
     'A'         65
     'B'         66
     ...        ...
     'Z'         90
     'a'         97
     'b'         98
     ...        ...
     'z'        122

 7. Overflow, Underflow, Round-Off Error
    ------------------------------------
    (1) Integer Overflow
        If an expression evaluates to an integer greater than can be
        stored, the result will be incorrect and no error message is
        given.
    (2) Floating-Point Overflow
        Overflow occurs when the floating-point result of a calculation
        has a positive exponent that requires too many digits to store.
        Some systems will give an incorrect answer, others will generate
        an error.
    (3) Floating-Point Underflow
        Underflow occurs when the result of a floating-point calculation
        has a negative exponent that requires too many digits to store.
        The standard for C is to go silently to zero.
    (4) Floating-Point Round-Off Error
        Round-off error occurs when the floating-point result of a
        calculation has too many significant digits to store.  The extra
        trailing digits are thrown away.

 8. Character Strings
    -----------------
    (1) Strings
        (a) A string is a series of characters terminated by the null
            character ('\0').
        (b) A string constant is a series of characters enclosed by
            double quotation marks.
        (c) The null character is automatically put at the end of string
            constants.
        (d) A string variable is an array of type char.
        (e) String variables must always be dimensioned one more than
            the maximum length to allow for the terminating null
            character.
        (f) When a string is read in the null character is automatically
            put at the end of the string.
        (g) C does not do array bounds checking.
    (2) strlen()
        The strlen() function counts the number of characters in a
        string.  The terminating null character is not included in the
        count.  You must add the following line after #include <stdio.h>
        whenever you use this function.
            #include <string.h>

    Examples:
    char first[21], middle[21], last[21];
    char greeting[14] = "Good morning!";

 9. Example 3-4.2
    -------------
    Program:
    /* len_name.c -- finds the length of a name */
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
      char name[41] = "John Doe";
      int length;

      printf("This program finds the length of a name.\n");
      length = strlen(name);
      printf("The name John Doe has %d characters in it.\n", length);
      return 0;
    }

    Output:
    This program finds the length of a name.
    The name John Doe has 8 characters in it.

10. printf()
    --------
    (1) Form:
            printf(control-string, item1, item2, ... )
                where control-string = a character string describing how
                                       the items are to be printed
                      item1 = first item (constant, variable,
                              expression) to be printed
                      item2 = second item (constant, variable,
                              expression) to be printed
                and return value = (a) number of characters printed
                                   (b) if error, negative number
    (2) A control-string contains two forms of information.
        (a) characters that are printed
        (b) conversion specifications
    (3) The following is a list of the conversion-specifiers used with
        printf().
        Conversion
        Specifier                   Output
        ----------  ---------------------------------------
           %c       single character
           %d       signed decimal integer
           %e       floating-point number, e-notation
           %E       floating-point number, E-notation
           %f       floating-point number, decimal notation
           %g       use %f or %e, whichever is shorter
           %G       use %f or %E, whichever is shorter
           %i       signed decimal integer
           %o       unsigned octal integer
           %p       a pointer
           %s       character string
           %u       unsigned decimal integer
           %x       unsigned hexadecimal integer, using hex digits 0-f
           %X       unsigned hexadecimal integer, using hex digits 0-F
    (4) A conversion specification can be modified by inserting
        modifiers between the % and the defining conversion character.
        If you use more than one modifier, they should be in the same
        order as they appear in the following table.
        Modifier                          Meaning
        --------   -----------------------------------------------------
        flag       The five flags (-,+,space,#,and 0) are described in
                   the following table.  Zero or more flags may be
                   present.
        *          Use the field width given by the next item in the
                   list.
        digit(s)   The minimum field width.  A wider field will be used
                   if the printed number or string won't fit in the
                   field.
        .digit(s)  Precision.  For %e, %E, and %f conversions, the
                   number of digits to be printed to the right of the
                   decimal.  For %s conversions, the maximum number of
                   characters to be printed.  For integer conversions,
                   the minimum number of digits to appear; leading zeros
                   are used if necessary to meet this minimum.  Using
                   only . implies a following zero, so %.f is the same
                   as %.0f.
        h          Used with an integer conversion to indicate a short
                   int or unsigned short int value.
        l          Used with an integer conversion to indicate a long
                   int or unsigned long int.  Used with a floating-point
                   conversion to indicate a double value.
        L          Used with a floating-point conversion to indicate a
                   long double value.

        Flag                            Meaning
        ----   ---------------------------------------------------------
        -      The item is left-justified; it's printed beginning at the
               left of the field.
        +      Signed values are displayed with a plus sign if positive
               and a minus sign if negative.
        space  Signed values are displayed with a leading space (but no
               sign) if positive and with a minus sign if negative.  A +
               flag overrides a space.
        #      Use an alternative form for the conversion specification.
               Produces an initial 0 for the %o form and an initial 0x
               or 0X for the %x and %X forms.  For all floating-point
               forms, # guarantees that a decimal-point character is
               printed, even if no digits follow.  For %g and %G forms,
               it prevents trailing zeros from being removed.
        0      For numeric forms, pad the field width with leading zeros
               instead of with white spaces.  This flag is ignored if a
               - flag is present or if, for an integer form, a precision
               is specified.

    Note:
    (1) Be sure the conversion specifications and list items match in
        number and type.
    (2) There are 2 ways to split a long string.
        (a) Use more than one printf() statement.
        (b) Use one quoted string followed by another quoted string,
            separated only by whitespace.
    (3) If you wish to print out the " character, use \".
        If you wish to print out the \ character, use \\.
        If you wish to print out the % character, use %%.

11. Example 3-4.3
    -------------
    Program:
    /* printf1.c -- printf() with '\n' */
    #include <stdio.h>
    int main(void)
    {
      /* 3 printf()'s generate 1 line of output */
      printf("red ");
      printf("orange ");
      printf("yellow\n");

      /* 1 printf() generates 3 lines of output */
      printf("green\nblue\nviolet\n");
      return 0;
    }

    Output:
    red orange yellow
    green
    blue
    violet

12. Example 3-4.4
    -------------
    Program:
    /* printf2.c -- printf() with %c, %s, %d, %ld, %f, and %lf */
    #include <stdio.h>
    int main(void)
    {
      char ch = 'a';
      char str[8] = "abc def";
      int i_num = 2;
      long l_num = 4L;
      float f_num = 6.5f;
      double d_num = 8.5;

      /* char with %c */
      printf("ch = '%c'\n", ch);

      /* string with %s */
      printf("str = \"%s\"\n", str);

      /* int with %d */
      printf("i_num = %d\n", i_num);

      /* long with %ld */
      printf("l_num = %ld\n", l_num);

      /* float with %f */
      printf("f_num = %f\n", f_num);

      /* double with %lf */
      printf("d_num = %lf\n", d_num);
      return 0;
    }

    Output:
    ch = 'a'
    str = "abc def"
    i_num = 2
    l_num = 4
    f_num = 6.500000
    d_num = 8.500000

13. Example 3-4.5
    -------------
    Program:
    /* printf3.c -- printf() with %lf, %le, and %lE */
    #include <stdio.h>
    int main(void)
    {
      double num1 = 27.5, num2 = 4.75e+1, num3 = 6.75E+1;

      /* a double value can be printed with %lf, %le, or %lE */
      printf("num1 = %lf = %le = %lE\n", num1, num1, num1);
      printf("num2 = %lf = %le = %lE\n", num2, num2, num2);
      printf("num3 = %lf = %le = %lE\n", num3, num3, num3);
      return 0;
    }

    Output:
    num1 = 27.500000 = 2.750000e+001 = 2.750000E+001
    num2 = 47.500000 = 4.750000e+001 = 4.750000E+001
    num3 = 67.500000 = 6.750000e+001 = 6.750000E+001

14. Example 3-4.6
    -------------
    Program:
    /* printf4.c -- printf() with %d */
    #include <stdio.h>
    int main(void)
    {
      int num = 123;

      /* no field width: minimum field width */
      printf("\"%d\"\n", num);

      /* field width too small: minimum field width */
      printf("\"%2d\"\n", num);

      /* field width too large: right justify with leading blanks */
      printf("\"%5d\"\n", num);

      /* field width too large and 0 flag: right justify with leading
                                           zeros */
      printf("\"%05d\"\n", num);
      return 0;
    }
      
    Output:
    "123"
    "123"
    "  123"
    "00123"

15. Example 3-4.7
    -------------
    Program:
    /* printf5.c -- printf() with %lf */
    #include <stdio.h>
    int main(void)
    {
      double num = 12.3456;

      /* %lf: decimal notation */
      /* no field width: minimum field width, 6 digits to the right of
                         the decimal point */
      printf("\"%lf\"\n", num);

      /* %.0lf: minimum field width, no digits to the right of the
                decimal point, no decimal point, with rounding */
      printf("\"%.0lf\"\n", num);

      /* %.2lf: minimum field width, 2 digits to the right of the
                decimal point, with rounding */
      printf("\"%.2lf\"\n", num);

      /* field width too small: minimum field width */
      printf("\"%2.2lf\"\n", num);

      /* exact field width */
      printf("\"%5.2lf\"\n", num);

      /* field width too large: right justify with leading blanks */
      printf("\"%8.2lf\"\n", num);
      return 0;
    }

    Output:
    "12.345600"
    "12"
    "12.35"
    "12.35"
    "12.35"
    "   12.35"

16. Example 3-4.8
    -------------
    Program:
    /* printf6.c -- printf() with %le and %lE */
    #include <stdio.h>
    int main(void)
    {
      double num = 12.3456;

      /* %le: e-notation */
      /* no field width: minimum field width, 6 digits to the right of
                         the decimal point */
      printf("\"%le\"\n", num);

      /* %.0le: minimum field width, no digits to the right of the
                decimal point, no decimal point, with rounding */
      printf("\"%.0le\"\n", num);

      /* %.3le: minimum field width, 3 digits to the right of the
                decimal point, with rounding */
      printf("\"%.3le\"\n", num);

      /* %lE: E-notation */
      /* field width too small: minimum field width */
      printf("\"%1.3lE\"\n", num);

      /* exact field width */
      printf("\"%10.3lE\"\n", num);

      /* field width too large: right justify with leading blanks */
      printf("\"%13.3lE\"\n", num);
      return 0;
    }

    Output:
    "1.234560e+001"
    "1e+001"
    "1.235e+001"
    "1.235E+001"
    "1.235E+001"
    "   1.235E+001"

17. Example 3-4.9
    -------------
    Program:
    /* printf7.c -- printf() with %s */
    #include <stdio.h>
    int main(void)
    {
      char str[11] = "abcdefghij";

      /* no field width: minimum field width */
      printf("\"%s\"\n", str);

      /* field width too small: minimum field width */
      printf("\"%5s\"\n", str);

      /* field width too large: right justify with leading blanks */
      printf("\"%15s\"\n", str);

      /* field width too large and - flag: left justify with trailing
                                           blanks */
      printf("\"%-15s\"\n", str);

      /* %8.3s: field width = 8, maximum characters = 3 */
      printf("\"%8.3s\"\n", str);
      return 0;
    }

    Output:
    "abcdefghij"
    "abcdefghij"
    "     abcdefghij"
    "abcdefghij     "
    "     abc"

18. Example 3-4.10
    --------------
    Program:
    /* printf8.c -- printf() with * modifier */
    #include <stdio.h>
    int main(void)
    {
      int width, precision;
      int i_num = 123;
      double d_num = 12.3456;

      /* %*d: use the field width given by the next item in the list */
      width = 5;
      printf("\"%*d\"\n", width, i_num);

      /* %*.*lf: use the field width and precision given by the next two
         items in the list */
      width = 8; precision = 2;
      printf("\"%*.*lf\"\n", width, precision, d_num);
      return 0;
    }

    Output:
    "  123"
    "   12.35"

19. Example 3-4.11
    --------------
    Program:
    /* printf9.c -- a printf() list item may be a constant, variable, or
                    expression */
    #include <stdio.h>
    int main(void)
    {
      int num = 4;

      printf("two = %d, num = %d, sum = %d\n", 2, num, 2+num);
      return 0;
    }

    Output:
    two = 2, num = 4, sum = 6

20. Example 3-4.12
    --------------
    Program:
    /* printf10.c -- the number of format specifications and the number
                     of items in the printf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num1 = 2, num2 = 4, num3 = 6, num4 = 8;

      /*** error ***/
      /* too many items in printf() list */
      printf("num1 = %d\n", num1, num2);

      /*** error ***/
      /* too few items in printf() list */
      printf("num3 = %d, num4 = %d\n", num3);
      return 0;
    }

    Output:
    num1 = 2
    num3 = 6, num4 = 131

21. Example 3-4.13
    --------------
    Program:
    /* printf11.c -- the type of the format specifications and the type
                     of the items in the printf() list must match */
    #include <stdio.h>
    int main(void)
    {
      char ch = 'a';
      char str[2] = "A";
      int i_num = 2;
      int d_num = 4.5;

      /*** error ***/
      /* char with %s */
      printf("ch = %s(string format)\n", ch);

      /*** error ***/
      /* string with %c */
      printf("str = %c(char format)\n", str);

      /*** error ***/
      /* int with %lf */
      printf("i_num = %lf(double format)\n", i_num);

      /*** error ***/
      /* double with %d */
      printf("d_num = %d(int format)\n", d_num);

      /*** error ***/
      /* two doubles with %d and %lf */
      printf("d_num = %d(int format) = %lf(double format)\n", d_num,
        d_num);
      return 0;
    }

    Output:
    ch =  %c(char format)
    (string format)
    str = (char format)
    i_num = 0.000000(double format)
    d_num = 4(int format)
    d_num = 4(int format) = 0.000000(double format)

22. Example 3-4.14
    --------------
    Program:
    /* printf12.c -- printf() with long strings */
    #include <stdio.h>
    int main(void)
    {
      /* use more than one printf() statement */
      printf("Here's one way to print a ");
      printf("long string.\n");

      /* use one quoted string followed by another quoted string,
         separated only by whitespace */
      printf("Here's another way to print a "
        "long string.\n");
      return 0;
    }

    Output:
    Here's one way to print a long string.
    Here's another way to print a long string.

23. scanf()
    -------
    (1) Form:
            scanf(control-string, loc1, loc2, ... )
                where control-string = a character string indicating
                                       into which formats the input is
                                       to be converted
                      loc1 = location where first item read is to be
                             stored
                      loc2 = location where second item read is to be
                             stored
                and return value = (a) number of items read
                                   (b) EOF if end-of-file
    (2) The following is a list of the conversion-specifiers used with
        scanf().
          Conversion
          Specifier                         Meaning
        --------------  ------------------------------------------------
           %c           Interpret input as a character.
          %d,%i         Interpret input as a signed decimal integer.
        %e,%E,%f,%g,%G  Interpret input as a floating-point number.
           %o           Interpret input as a signed octal integer.
           %p           Interpret input as a pointer.
           %s           Interpret input as a string; input begins with
                        the first nonwhitespace character and includes
                        everything up to the next whitespace character.
           %u           Interpret input as an unsigned decimal integer.
          %x,%X         Interpret input as a signed hexadecimal integer.
    (3) A conversion specification can be modified by inserting
        modifiers between the % and the defining conversion character.
        If you use more than one modifier, they should be in the same
        order as they appear in the following table.
        Modifier                         Meaning
        ---------  ----------------------------------------------------
        *          Suppress assignment.
        digit(s)   Maximum field width; input stops when the maximum
                   field width is reached or when the first whitespace
                   character is encountered, whichever comes first.
        h,l,L      %hd and %hi indicate that the value will be stored in
                   a short.  %ho, %hx, %hX, and %hu indicate that the
                   value will be stored in an unsigned short.  %ld and
                   %li indicate that the value will be stored in a long.
                   %lo, %lx, %lX, and %lu indicate that the value will
                   be stored in an unsigned long.  %le, %lE, %lf, %lg,
                   and %lG indicate that the value will be stored in a
                   double.  Using %Le, %LE, %Lf, %Lg, and %LG indicate
                   that the value will be stored in a long double.
    (4) (a) Precede scalar variables in a scanf() list with '&'.
        (b) Do not precede arrays in a scanf() list with '&'.
    (5) Be sure the conversion specifications and list items match in
        number and type.
    (6) For each conversion specifier, scanf() skips over leading 
        whitespace characters (spaces, tabs, and newlines) until it
        finds a nonwhitespace character.  It then reads valid characters
        for the given conversion specifier until the field width is
        reached or a whitespace is encountered, whichever occurs first.
        Exception:
        (a) %c reads all input characters; whitespace is not skipped.
        (b) A space followed by %c skips leading whitespace.
    (7) A space in the format string means to skip over any whitespace
        before the next input item.
    (8) Ordinary characters other than the space in the format string
        must be matched exactly by the input string.
    (9) If an invalid character is encountered for a conversion
        specifier, scanf() stops reading input at the invalid character.

24. Example 3-4.15
    --------------
    Program:
    /* scanf1.c -- scanf() with %c, %s, %d, %ld, %f, and %lf */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      char str[21];
      int i_num;
      long l_num;
      float f_num;
      double d_num;

      /* char with %c */
      printf("Please enter a character.\n");
      scanf("%c", &ch);
      printf("ch = '%c'\n", ch);

      /* string with %s */
      printf("Please enter a string.\n");
      scanf("%s", str);
      printf("str = \"%s\"\n", str);

      /* int with %d */
      printf("Please enter an integer.\n");
      scanf("%d", &i_num);
      printf("i_num = %d\n", i_num);

      /* long with %ld */
      printf("Please enter a long.\n");
      scanf("%ld", &l_num);
      printf("l_num = %ld\n", l_num);

      /* float with %f */
      printf("Please enter a float.\n");
      scanf("%f", &f_num);
      printf("f_num = %f\n", f_num);

      /* double with %lf */
      printf("Please enter a double.\n");
      scanf("%lf", &d_num);
      printf("d_num = %lf\n", d_num);
      return 0;
    }

    Screen:
    Output: Please enter a character.
    Input:  a
    Output: ch = 'a'
    Output: Please enter a string.
    Input:  abc
    Output: str = "abc"
    Output: Please enter an integer.
    Input:  2
    Output: i_num = 2
    Output: Please enter a long.
    Input:  4
    Output: l_num = 4
    Output: Please enter a float.
    Input:  6.5
    Output: f_num = 6.500000
    Output: Please enter a double.
    Input:  8.5
    Output: d_num = 8.500000

25. Example 3-4.16
    --------------
    Program:
    /* scanf2.c -- scanf() with %lf, %le, and %lE */
    #include <stdio.h>
    int main(void)
    {
      double num1, num2, num3;

      /* (1) the user may enter a double value in integer, decimal, or
             exponential form
         (2) a double value can be read with %lf, %le, or %lE */
      printf("Please enter three numbers in integer form.\n");
      scanf("%lf %le %lE", &num1, &num2, &num3);
      printf("num1 = %lf, num2 = %lf, num3 = %lf\n", num1, num2, num3);

      printf("Please enter three numbers in decimal form.\n");
      scanf("%lf %le %lE", &num1, &num2, &num3);
      printf("num1 = %lf, num2 = %lf, num3 = %lf\n", num1, num2, num3);

      printf("Please enter three numbers in exponential form.\n");
      scanf("%lf %le %lE", &num1, &num2, &num3);
      printf("num1 = %lf, num2 = %lf, num3 = %lf\n", num1, num2, num3);
      return 0;
    }

    Screen:
    Output: Please enter three numbers in integer form.
    Input:  2 4 6
    Output: num1 = 2.000000, num2 = 4.000000, num3 = 6.000000
    Output: Please enter three numbers in decimal form.
    Input:  2.5 4.5 6.5
    Output: num1 = 2.500000, num2 = 4.500000, num3 = 6.500000
    Output: Please enter three numbers in exponential form.
    Input:  2.75e+1 4.75E+1 6.75e+1
    Output: num1 = 27.500000, num2 = 47.500000, num3 = 67.500000

26. Example 3-4.17
    --------------
    Program:
    /* scanf3.c -- in a scanf() list the '&' character must appear
                   before scalar variables */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /*** error ***/
      /* '&' does not appear before scalar variable in scanf() list */
      printf("Please enter an integer.\n");
      scanf("%d", num);
      printf("num = %d\n", num);
      return 0;
    }

    Screen:
    Output: Please enter an integer.
    Input:  2
    Output: num = 25
    Output:
    Output: run-time error R6001
    Output: - null pointer assignment

    Note:
    Null pointer assignment error messages are issued after a program is
    finished executing -- not at the time the error occurs.

27. Example 3-4.18
    --------------
    Program #1:
    /* scanf4.c -- the number of format specifications and the number of
                   items in the scanf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num1, num2;

      /*** error ***/
      /* too many items in scanf() list */
      printf("Please enter two integers.\n");
      scanf("%d", &num1, &num2);
      printf("num1 = %d, num2 = %d\n", num1, num2);
      return 0;
    }

    Screen #1:
    Output: Please enter two integers.
    Input:  2 4
    Output: num1 = 2, num2 = 0

    Program #2:
    /* scanf5.c -- the number of format specifications and the number of
                   items in the scanf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num1, num2;

      /*** error ***/
      /* too few items in scanf() list */
      printf("Please enter two integers.\n");
      scanf("%d %d", &num1);
      printf("num1 = %d, num2 = %d\n", num1, num2);
      return 0;
    }

    Screen #2:
    Output: Please enter two integers.
    Input:  2 4
    Output: num1 = 2, num2 = 0
    Output:
    Output: run-time error R6001
    Output: - null pointer assignment

28. Example 3-4.19
    --------------
    Program #1:
    /* scanf6.c -- the type of the format specifications and the type of
                   the items in the scanf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /*** error ***/
      /* int with %f and no floating-point in program */
      printf("Please enter an integer.\n");
      scanf("%f", &num);
      printf("num = %d\n", num);
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.
    Input:  2
    Output:
    Output: run-time error R6002
    Output: - floating-point support not loaded

    Program #2:
    /* scanf7.c -- the type of the format specifications and the type of
                   the items in the scanf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num;
      float x = 0.0;

      /*** error ***/
      /* int with %f and floating-point in program */
      printf("Please enter an integer.\n");
      scanf("%f", &num);
      printf("num = %d\n", num);
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.
    Input:  2
    Output: num = 0

    Program #3:
    /* scanf8.c -- the type of the format specifications and the type of
                   the items in the scanf() list must match */
    #include <stdio.h>
    int main(void)
    {
      int num;
      double x = 0.0;

      /*** error ***/
      /* int with %lf and floating-point in program */
      printf("Please enter an integer.\n");
      scanf("%lf", &num);
      printf("num = %d\n", num);
      return 0;
    }

    Screen #3:
    Output: Please enter an integer.
    Input:  2
    Output: num = 0
    Output: Please enter an integer.
    Input:  4
    Output: num = 0
    Output: Please enter an integer.
    Input:  6
    Output: num = 0
          ...

29. Example 3-4.20
    --------------
    Program #1:
    /* scanf9.c -- more than one line of input per scanf() */
    #include <stdio.h>
    int main(void)
    {
      int i_num;
      double d_num;

      /* for each conversion specifier except %c, scanf() skips over
         leading whitespace characters (spaces, tabs, and newlines) */
      printf("Please enter an integer and a double.\n");
      scanf("%d %lf", &i_num, &d_num);
      printf("i_num = %d, d_num = %lf\n", i_num, d_num);
      return 0;
    }

    Screen #1:
    Output: Please enter an integer and a double.
    Input:  12
    Input:  12.3456
    Output: i_num = 12, d_num = 12.345600

    Program #2:
    /* scanf10.c -- more than one scanf() per line of input */
    #include <stdio.h>
    int main(void)
    {
       int i_num;
       double d_num;

       /* if one scanf() finishes in the middle of a line of input,
          the next scanf() will start with the remaining input */
       printf("Please enter an integer and a double.\n");
       scanf("%d", &i_num);
       scanf("%lf", &d_num);
       printf("i_num = %d, d_num = %lf\n", i_num, d_num);
       return 0;
    }

    Screen #2:
    Output: Please enter an integer and a double.
    Input:  12 12.3456
    Output: i_num = 12, d_num = 12.345600

30. Example 3-4.21
    --------------
    Program #1:
    /* scanf11.c -- conversion specifier without field width */
    #include <stdio.h>
    int main(void)
    {
      char name[21];

      /* for a given conversion specifier without a field width, scanf()
         reads valid characters until whitespace is encountered -- this
         means scanf() with %s reads a word */
      printf("Please enter your name.\n");
      scanf("%s", name);
      printf("name = \"%s\"\n", name);
      return 0;
    }

    Screen #1:
    Output: Please enter your name.
    Input:  Maryann Smith
    Output: name = "Maryann"

    Program #2:
    /* scanf12.c -- conversion specifier with field width */
    #include <stdio.h>
    int main(void)
    {
      char first[6], last[6];

      /* (1) for a given conversion specifier with a field width,
             scanf() reads valid characters until the field width is
             reached or a whitespace is encountered, whichever occurs
             first
         (2) with %s using a field width one less than the dimension
             prevents array overflow
         (3) if the user enters more characters than the field width
             the user input and the program variables no longer match
             up */
      printf("Please enter your name.\n");
      scanf("%5s %5s", first, last);
      printf("first = \"%s\", last = \"%s\"\n", first, last);
      return 0;
    }

    Screen #2:
    Output: Please enter your name.
    Input:  Maryann Smith
    Output: first = "Marya", last = "nn"

31. Example 3-4.22
    --------------
    Program:
    /* scanf13.c -- scanf() with space(s) */
    #include <stdio.h>
    int main(void)
    {
      int num1, num2;

      /* space(s) before a conversion specifier except %c do not change
         its meaning:
         (1) a space in the format string means to skip over any
             whitespace before the next input item
         (2) for each conversion specifier except %c, scanf() skips
             over leading whitespace characters (spaces, tabs, and
             newlines) */
      printf("Please enter two integers.\n");
      scanf("%d%d", &num1, &num2);
      printf("num1 = %d, num2 = %d\n", num1, num2);

      printf("Please enter two integers.\n");
      scanf(" %d %d", &num1, &num2);
      printf("num1 = %d, num2 = %d\n", num1, num2);     
      return 0;
    }

    Screen:
    Output: Please enter two integers.
    Input:  12 34
    Output: num1 = 12, num2 = 34
    Output: Please enter two integers.
    Input:  56 78
    Output: num1 = 56, num2 = 78

32. Example 3-4.23
    --------------
    Program #1:
    /* scanf14.c -- scanf() with %c */
    #include <stdio.h>
    int main(void)
    {
      char ch1, ch2, ch3;

      /* %c reads all input characters, whitespace is not skipped */
      printf("Please enter 3 characters.\n");
      scanf("%c%c%c", &ch1, &ch2, &ch3);
      printf("ch1 = '%c', ch2 = '%c', ch3 = '%c'\n", ch1, ch2, ch3);
      return 0;
    }

    Screen #1:
    Output: Please enter 3 characters.
    Input:   a  bc
    Output: ch1 = ' ', ch2 = 'a', ch3 = ' '

    Program #2:
    /* scanf15.c -- scanf() with %c and space(s) */
    #include <stdio.h>
    int main(void)
    {
      char ch1, ch2, ch3;

      /* a space followed by %c skips leading whitespace */
      printf("Please enter 3 characters.\n");
      scanf(" %c %c %c", &ch1, &ch2, &ch3);
      printf("ch1 = '%c', ch2 = '%c', ch3 = '%c'\n", ch1, ch2, ch3);
      return 0;
    }

    Screen #2:
    Output: Please enter 3 characters.
    Input:   a  bc
    Output: ch1 = 'a', ch2 = 'b', ch3 = 'c'

33. Example 3-4.24
    --------------
    Program:
    /* scanf16.c -- scanf() with ordinary characters in control
                    string */
    #include <stdio.h>
    int main(void)
    {
      int num1, num2;

      /* (1) a space in the format string means to skip over any
             whitespace before the next input item
         (2) ordinary characters other than the space in the format
             string must be matched exactly by the input string */
      printf("Please enter two integers.\n");
      scanf("%d, %d", &num1, &num2);
      printf("num1 = %d, num2 = %d\n", num1, num2);
      return 0;
    }

    Valid Inputs:
    (1) 12,34
    (2) 12, 34

    Invalid Inputs:
    (1) 12 34
    (2) 12 ,34
    (3) 12 , 34

34. Example 3-4.25
    --------------
    Program:
    /* scanf17.c -- scanf() with * modifier */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* with scanf() the * modifier suppresses assignment */
      printf("Please enter two integers.\n");
      scanf("%*d %d", &num);
      printf("num = %d\n", num);
      return 0;
    }

    Screen:
    Output: Please enter two integers.
    Input:  12 34
    Output: num = 34

35. Example 3-4.26
    --------------
    Program:
    /* scanf18.c -- scanf()'s return value */
    #include <stdio.h>
    int main(void)
    {
      int num1, num2, n;

      /* (1) scanf() returns the number of items read
         (2) if an invalid character is encountered for a conversion
             specifier, scanf() stops reading input at the invalid
             character */
      printf("Please enter two integers.\n");
      n = scanf("%d %d", &num1, &num2);
      printf("num1 = %d, num2 = %d, n = %d\n", num1, num2, n);
      return 0;
    }

    Screen #1:
    Output: Please enter two integers.
    Input:  12 34
    Output: num1 = 12, num2 = 34, n = 2

    Screen #2:
    Output: Please enter two integers.
    Input:  12,34
    Output: num1 = 12, num2 = 0, n = 1

    Screen #3:
    Output: Please enter two integers.
    Input:  q
    Output: num1 = 25, num2 = 0, n = 0

36. Example 3-4.27
    --------------
    Problem:
    Write a program that asks the user to enter the radius of a circle.
    Calculate the circumference and area of the circle.  Then print the
    results in message form.

    Program:
    /* circle.c -- calculates the circumference and area of a circle */
    #include <stdio.h>
    #define PI 3.14159
    int main(void)
    {
      double radius, circum, area;

      printf("This program calculates the circumference and area\n");
      printf("of a circle.\n");

      /* get radius from user */
      printf("Please enter the radius.\n");
      scanf("%lf", &radius);

      /* calculate circumference and area */
      circum = 2.0 * PI * radius;
      area = PI * radius * radius;

      /* print results in message form */     
      printf("radius = %.2lf\n", radius);
      printf("circumference = %.2lf\n", circum);
      printf("area = %.2lf\n", area);
      return 0;
    }

    Screen:
    Output: This program calculates the circumference and area
    Output: of a circle.
    Output: Please enter the radius.
    Input:  2.5
    Output: radius = 2.50
    Output: circumference = 15.71
    Output: area = 19.63

37. Example 3-4.28
    --------------
    Problem:
    Write a program that asks the user to enter his name, the price
    per night at the hotel, and the number of nights stayed at the
    hotel.  Calculate the hotel bill.  Then print the results in a
    table.

    Program:
    /* hotel.c -- calculates a hotel bill */
    #include <stdio.h>
    int main(void)
    {
      char first[21], last[21];
      int nights;
      double price, bill;

      printf("This program calculates a hotel bill.\n");

      /* get name, price per night, and number of nights from user */
      printf("Please enter name, price per night, and number of "
        "nights.\n");
      scanf("%s %s %lf %d", first, last, &price, &nights);

      /* calculate hotel bill */
      bill = price * nights;

      /* print results in a table */
      printf("%-15s  %-15s  %-6s  %-6s  %-6s\n", "First", "Last",
        "Price", "Nights", "Bill");
      printf("---------------  ---------------  ------  ------  "
        "------\n");
      printf("%-15s  %-15s  %6.2lf  %6d  %6.2lf\n", first, last, price,
        nights, bill);
      return 0;
    }

    Screen:
    Output: This program calculates a hotel bill.
    Output: Please enter name, price per night, and number of nights.
    Input:  Sally Smith 75.50 3
    Output: First            Last             Price   Nights  Bill  
    Output: ---------------  ---------------  ------  ------  ------
    Output: Sally            Smith             75.50       3  226.50
 



1



