UNIX C Programming

Chapter 2: Introducing C



In this chapter, students learn how to write simple C programs. The objectives important to this chapter include:

  • Explain how a C program is put together
  • Show how to create integer-valued variables and assign them values
  • Show how to display those values on screen
  • Discuss the newline character
  • Show how to include comments in a program
  • Demonstrate how to create programs with more than one function
  • Show how to find program errors
  • Explain the use of keywords

C programs consist of one or more functions, the basic modules of a C program. Every C program must contain a function called main(). This function is defined as:

	int main(void)

The void indicates that main() does not take any arguments.

A C program begins with the inclusion of the #include directive and header files. The standard input/output file is included by the statement:

	#include <stdio.h>

The #include statement is an example of a preprocessor directive.

C comments are placed inside of /* */ pairs. Comments help to clarify the meaning of a program.

A pair of braces {} mark the beginning and ending of a function, or a block.

All C variables must be declared before they are used. A variable declaration follows the format:

	data_type variable_name;

as in:

	int num1;

A data type is the kind or type of data the variable is required to store. This chapter is limited to use of the data type int (for integer).

Use meaningful variable names. A name is often restricted to 8 characters; however, the ANSI C standard calls for up to 31 characters.

Four good reasons for declaring variables at the beginning of a C program are:

  1. The program is easier to understand.
  2. Planning is required before starting to program.
  3. Early declarations helps to uncover two spellings of the same name.
  4. Your C program will not compile if you fail to declare a variable.

The assignment statement is written as:

	num1 = 1;

The statement assigns a value to the variable num1.

The printf() statement is actually a function call, where the message placed inside the parentheses is passed to the function. This message is called the argument.

Within printf(), a new line can be represented on the display screen by using the newline (one word) character \n. To display two newlines, write \n\n.

The return statement is placed at the end of main. It is written as:

	return 0;

since the function returns a value of type int.

The structure of a C program contains a header and a body. The header consists of preprocessor directives and all function declarations. The body consists of statements which following the beginning and ending braces.

When there are multiple functions in a program, place all function declarations at the top of the program before main(). These declarations are called function prototypes. A prototype must match the function definition, placed later in the program.

Four ways to make your programs more readable are:

  1. Use comment statements to clarify your work.
  2. Use meaningful variable names.
  3. Use blank lines to make your program easier to follow.
  4. Use one line per statement.

Multiple declarations can be made using a single statement, as in:

	int num1, num2;

Debugging or finding errors is an important programming skill. The easiest type of error to find is a syntax error. This type of error results from the writing of a C statement which does not follow C's rules. A semantic error will not be spotted by the compiler. This type of error often leads to a result, where the result is in error. A program must often by checked using a debugger, a program designed to examine the running of another program to determine the origin of semantic errors. Remind students that errors should be expected.

Keywords are the vocabulary of C. They cannot be used as variable names. For example, int cannot be used as a variable name.