CISC105: Reading Notes
Tan and D'Orazio, Chapter 2

by Phill Conrad, Asst. Professor,
CIS Dept, University of Delaware

See reading notes for Chapter 1 for discussion of the purpose of these reading notes.

What to emphasize in Chapter 2

Chapter 2 contains very basic and useful information about getting started with C programming. It is pretty well written and easy to follow, and you should read it through in its entirety.

You may want to have a computer terminal handy as you read, so that you can try out the programs that are given in the text as you read about them. You are more likely to succeed if you "play around" with the programs, experimenting with them as you learn about the C language.

The Chapter is divided into the following sections

The five "lessons" will teach you about five basic concepts in the C programming language. The two "application programs" are examples that try to pull everything you've learned so far together. The "application exercises" are chances for you to try out what you've learned.

Chapter 2, Tan and D'Orazio (1999)

Lesson 2.1: Basic Structure

A few observations about this lesson:

  1. The first program you write doesn't have any comments in it. You'll learn about comments in Lesson 2.2. This is probably that last time you should ever write a program with no comments.
  2. One of the things you need to get out of this section is that
    #include <stdio.h>
    is a pre-processor directive. Be sure you read about pre-processor directives and understand what the #include directive does. Figure 2.1 should help.
  3. The program in your text reads as follows:
      #include 
      void main(void)
      {
          printf("This is C!");
      }
    
    However, as it turns out, this might result in some "warnings" on some compilers. If so, you might want to try this instead:
    #include 
    int main(void)
    {
        printf("This is C!");
    	return 0;
    }	
    

    The main difference here is that this program has int in front of main instead of void. This causes us to have include one extra line of code: the return 0; that appears near the end of the program. The purpose of this line is to return a value of 0 to the operating system when the program is complete to let the operating system know that the program was successful. It is not always strictly necessary to do this, however it is a good habit to develop. I may say more about this in lecture.
  4. Another thing you should pick up from this section is that main is the name of a function. Every program must have exactly one function called main. Execution always starts in the main function.
  5. You should also pick up from your reading in this section what a string literal or string constant is, as well as the fact that printf is the name of a function. You should also pick up that { and } are called braces, and that they are used to enclose a block of code.
  6. You should know what tokens are, and what white-space characters are. You should also be able to explain why it is important for a program to look a certain way, even though the compiler may not care how things are spaced out.
  7. Reviewing figure 2.3 is a very good idea!

Exercises (p. 44)

Exercises 1b and 1e are a bit tricky.

1b asks whether it is true that "by default, any C statement is location sensitive". However, "location sensitive" is not clearly defined (or, if it is, I missed it.). I think the idea that the exercise is getting at is that you can insert an arbitrary number of spaces, tabs and blank lines in between tokens, thus "changing the location" of the various parts of a C statement. In that sense, C is not "location sensitive".

1e asks whether main (){} is a complete and correct C program. This is a bit of a trick question in my opinion. It turns out that some compilers will accept this program, so one could argue that the statement is true. However, this program does no useful work. So in that sense, it is, to say the least, a very silly program.

Be careful as you answer 1d. It is a fair question, but you need to read the carefully before answering; there is a trick there.

Apart from that, the remainder of the exercises on pages 44 through 46 are reasonable and you should try them before continuing.

Lesson 2.2: Writing Comments

This lesson is a good overview of the rules regarding comments. Here are a few remarks about this lesson.

First, some of you may have been exposed to another style of comment where you use two slashes // to indicate the start of a comment. This is valid in C++ and Java, but you should not use that style of comment in this course, since it is not part of standard C. (In CISC181, you'll be able to use that kind of comment to your heart's content.)

Seciond, on page 48-49, there is a discussion of nested comments. Nested comments can arise when we use the comment mechanism to "comment out" a line of code to temporarily remove it from a program. However, is a better way of temporarily removing a line of code from a program, as shown below.

Your book suggests that you can remove a line of code by putting comment brackets around it as follows:

  /* printf("How do we write comments in C?");*/
  
Instead, I suggest you write the following:
#if 0
   printf("How do we write comments in C?");
#endif   

The region of code between the #if 0 and #endif pre-processor directives will be ignored by the compiler. The advantage of using this to "comment-out" code instead of comment brackets like /* */ is that the pre-processor directives will not create a nested-comment problem. In fact, the pre-processor directives can be nested without creating any problem.

Be sure to read over the section on p.49 about how to write valuable comments; it is important!


Exercises (p. 50)

These exercises seem like reasonably good practice.

Lesson 2.3: Formatting Output

This section focuses on the \n escape sequence. A key point is that the program at the bottom of p. 51 does not print the output just above it, but instead prints:

Welcome toLondon!

Note that there is no space between to and London! because there is no space inside the quotation marks. C always prints exactly what you tell it to print, and nothing more.

Exercises (p. 52)

These exercises are all good practice.

Lesson 2.4: More Escape Sequences

This lesson adds additional escape sequences in addition to \n. Here are some remarks on this section.

First, you do not need to memorize all of the escape sequences in Table 2.1. However, you do need to memorize the following:

\a alert make a beep sound
\n new line move to a new line
\r carriage return move to the start of the current line
\t horizontal tab move to the next tab position
\\ backslash insert a backslash
\' single quote

insert a single quote

\" double quote

insert a double quote

 

\% percent

insert a percent sign

\? question mark insert a question mark

Later in the semester, you'll also need to know the \0 escape sequence which inserts a null character, but we'll cover that when we begin to cover character strings.

Exercises (p. 57)

I recommend these exercises.

Lesson 2.5: Review of Chapter and Comments about Errors and Debugging

This lesson is a good review. In addition, it covers the differences between and among syntax errors, run-time errors and logic errors. Be sure you understand these concepts.

One note: the book refers to the terms semantic errors and smart errors as synonyms for run-time errors. I have usually seen semantic errors associated with a different type of error, and I have not encountered the term smart errors. What the book describes as run-time errors are usually referred to either as run-time errors or as fatal errors (fatal, because they cause the program to "die").

The remarks in this section on p. 60-61 titled "How do you debug a program" are very good; I recommend them to you.

Exercises (p. 61)

These exercises are good. Note that the answer to Exercise 2 is on p. 62, so try to answer the question yourself before looking at the answer.

Application Program 2.1: Printing Logo

This is a nice short example that pulls together the concepts in this Chapter. I recommend that you go over it.

Application Program 2.2: Debugging

This example is a bit long, however it is very instructive and gives you an idea of what you will encounter when you debug your own programs. I will be interested to find out whether you find it useful and understandable or not. It may be that this section covers something that is better encountered by just getting your feet wet in the lab. Please read this section, and let me know by email what you think of it.

Application Exercises

All of the application exercises given here are good practice. Some of them may be assigned as homework, but any that aren't are still a good idea for you to try!

(end of reading notes for Chapter 2).

These reading notes are based on C Programming for Engineering and Computer Science, by H. H. Tan and T. B. D'Orazio, Copyright 1999 by WCB McGraw-Hill. The notes themselves are Copyright 2004, by Phillip T. Conrad, All Rights Reserved.

Prof. Conrad takes sole responsibility for any views or opinions expressed in these notes. Such view and opinions do necessarily reflect those of the University of Delaware, its Department of Computer and Information Sciences, or any other organization.