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

1. Programming Mechanics
    ---------------------
    Preparing a C Program
    ---------------------
    Diagram:
        |-->enter/modify source code
        |              |
        |              | text editor
        |              V
        |         source code     (filename.c)
        |              |
        |              | compiler
        |              V
        |             / \
        |       yes  /   \
        |___________/error\
        |           \     /
        |            \   /
        |             \ /
        |              | no
        |              V
        |      intermediate code  (filename.obj)
        |              |
        |              | linker, start-up code, library code
        |              V
        |             / \
        |       yes  /   \
        |___________/error\
        |           \     /
        |            \   /
        |             \ /
        |              | no
        |              V
        |       executable code   (filename.exe)
        |              |
        |              | run
        |              V
        |             / \
        |       yes  /   \
        |___________/error\
        |           \     /
        |            \   /
        |             \ /
        |              | no
        |              V
        |           results
        |              |
        |              V
        |             / \
        |      yes   /   \
        |___________/error\
                    \     /
                     \   /
                      \ /
                       | no
                       V
                      done

 2. Hung Program
    ------------
    Use the following steps to exit a hung program.
    (1) <Ctrl-C>
        If this fails, try step (2).
    (2) <Ctrl-Break>
        If this fails, try step (3).
    (3) <Ctrl-Alt-Del>
        If this fails, try step (4).
    (4) Reset Button
        If this fails, do step (5).
    (5) Power Off

 3. General Form for a Simple C Program
    -----------------------------------
    Program:
    #include <stdio.h>
    int main(void)
    {
      /* declarations */
      /* executable statements */
      return 0;
    }

 4. Correct Program
    ---------------
    Program:
    /* fathm_ft.c -- converts fathoms to feet */
    #include <stdio.h>
    int main(void)
    {
      int fathoms, feet;

      printf("This program converts fathoms to feet.\n");
      fathoms = 2;
      feet = fathoms * 6;
      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.

 5. Program with Errors
    -------------------
    Program #1:
    /* nogood.c -- a program with errors */
    #include <stdio.h>
    int main(void)
    (
      int n, int n2, int n3;
          /* this program has several errors

      printf("This program computes n squared and n cubed.\n");
      n = 5;
      n2 = n * n;
      n3 = n2 * n2;
      printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)
      return 0;
    )

    Syntax Errors #1:
    (1) Uses parentheses instead of braces to mark the body of the
        function.
    (2) Uses the word "int" too often.
    (3) Missing closing "*/" on comment.
    (4) Missing terminating ";" on printf() statement.

    Program #2:
    /* stillbad.c -- a program with its syntax errors fixed */
    #include <stdio.h>
    int main(void)
    {
      int n, n2, n3;
          /* this program has a semantic error */

      printf("This program computes n squared and n cubed.\n");
      n = 5;
      n2 = n * n;
      n3 = n2 * n2;
      printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);
      return 0;
    }

    Output #2:
    This program computes n squared and n cubed.
    n = 5, n squared = 25, n cubed = 625

    Semantic Error #2:
    The statement
        n3 = n2 * n2;
    is supposed to compute the cube of n, but this statement actually
    computes the fourth power of n.

    Program #3:
    /* good.c -- a program with its errors fixed */
    #include <stdio.h>
    int main(void)
    {
      int n, n2, n3;

      printf("This program computes n squared and n cubed.\n");
      n = 5;
      n2 = n * n;
      n3 = n2 * n;
      printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);
      return 0;
    }

    Output #3:
    This program computes n squared and n cubed.
    n = 5, n squared = 25, n cubed = 125
 



2



