Sections 0101, 0102, 0103 and Honors, Fall 1995
Thursday September 7, 1995
Assigned Reading: 2.1
Handouts (available on-line): none
Topics Covered:
- A simple C program
that prints out "Hello World" and
sample run.
Note the #include preprocessor directives.
- Embellishments:
hello2.c and
sample run.
Note the use of \n, \\ and %%.
- A program to add two integers:
add2.c and
sample run.
See how integer variables are declared and used.
Note the use of %d in the printf function.
In the sample run, see that GetInteger() is robust
and will not allow the user to enter a number that
is not an integer.
- A program to add two floating point numbers:
add2f.c and
sample run.
Floating point numbers are declared using double
and printed using %g. Double variables are
read in using GetReal(). Round off errors
may occur (as with hand-held calculators).
- A program that reads in a string and prints it out:
greeting.c and
sample run.
String variables are declared using string
and read in using GetLine().
- Some typical coding errors and how the compiler might
respond to them:
- In this program,
wrong1.c,
the functions GetInteger and printf()
are misspelled. The
sample run
also shows what happens if you didn't copy cc201
correctly.
- In this program,
wrong2.c,
the preprocessor directive #include is misspelled.
There is also a missing end quote in the printf()
statement on line 19. The
sample run
also shows the error messages generated in these
situations.
- In this program,
wrong3.c,
a semicolon is missing at the end of the variable
declarations on line 14. The
sample run
shows that the compiler complains about a syntax
error on line 16. This is because C ignores
whitespace (spaces, newlines and tabs) and does
not notice the missing semicolon until it sees
the printf(). The corrected program
correct3.c,
shows that we can put the declaration of total
in a completely weird place and the complier does not
care, as witnessed by the
sample run.
Last Modified:
Thu Sep 7 16:37:44 EDT 1995
Richard Chang, chang@gl.umbc.edu