The Components of a C
Program
Every C program consists of several components combined in a certain way.
Most of this book is devoted to explaining these various program components
and how you use them. To help illustrate the overall picture, you should begin
by reviewing a complete (though small) C program with all its components
identified. Today you will learn:
• About a short C program and its components
• The purpose of each program component
• How to compile and run a sample program
The Program’s Components
The following sections describe the various components of the preceding sample program.
Line numbers are included so that you can easily identify the program parts being
discussed.
The main() Function (Lines 8 Through 23)
The only component that is required in every executable C program is the main() function.
In its simplest form, the main() function consists of the name main followed by a
pair of parentheses containing the word void ((void)) and a pair of braces ({}). You can
leave the word void out and the program will still work with most compilers. The ANSI
standard states that you should include the word void so that you know there is nothing
being sent to the main function.
The #include Directive (Line 2)
The #include directive instructs the C compiler to add the contents of an include
file into your program during compilation. An include file is a separate disk file
that contains information that can be used by your program or the compiler. Several of
these files (sometimes called header files) are supplied with your compiler. You rarely
need to modify the information in these files; that’s why they’re kept separate from your
source code. Include files should all have an .h extension (for example, stdio.h).
You use the #include directive to instruct the compiler to add a specific include file to
your program during compilation. In Listing 2.1, the #include directive is interpreted to
LISTING 2.1 continued
INPUT/
OUTPUT
NEW TERM
mean “Add the contents of the file stdio.h.” You will almost always include one or more
include files in your C programs. More information about include files is presented on
Day 21, “Advanced Compiler Use.”
The Variable Definition (Line 4)
A variable is a name assigned to a location in memory used to store information.
Your program uses variables to store various kinds of information during program
execution. In C, a variable must be defined before it can be used. A variable definition
informs the compiler of the variable’s name and the type of information the variable
is to hold. In the sample program, the definition on line 4, int val1, val2, val3;,
defines three variables—named val1, val2, and val3—that will each hold an integer
value. More information about variables and variable definitions is presented on Day 3,
“Storing Information: Variables and Constants.”
The Function Prototype (Line 6)
A function prototype provides the C compiler with the name and arguments of
the functions contained in the program. It appears before the function is used. A
function prototype is distinct from a function definition, which contains the actual statements
that make up the function. (Function definitions are discussed in more detail later
today.)
Program Statements (Lines 11, 12, 15, 16, 19, 20, 22,
and 28)
The real work of a C program is done by its statements. C statements display information
on-screen, read keyboard input, perform mathematical operations, call functions, read
disk files, and all the other operations that a program needs to perform. Most of this book
is devoted to teaching you the various C statements. For now, remember that in your
source code, C statements are generally written one per line and always end with a semicolon.
The statements in multiply.c are explained briefly in the following sections.
The printf() Statement
The printf() statement (lines 11, 15, and 20) is a library function that displays information
on-screen. The printf() statement can display a simple text message (as in lines 11
and 15) or a message and the value of one or more program variables (as in line 20).
The scanf() Statement
The scanf() statement (lines 12 and 16) is another library function. It reads data from
the keyboard and assigns that data to one or more program variables.
32 Day 2
NEW TERM
NEW TERM
The Components of a C Program 33
2
The program statement on line 19 calls the function named product(). In other words, it
executes the program statements contained in the function product(). It also sends the
arguments val1 and val2 to the function. After the statements in product() are completed,
product() returns a value to the program. This value is stored in the variable
named val3.
The return Statement
Lines 22 and 28 contain return statements. The return statement on line 28 is part of
the function product(). It calculates the product of the variables x and y and returns the
result to the program that called product(). The return statement on line 22 returns a
value of 0 to the operating system just before the program ends.
The Function Definition (Lines 26 Through 29)
A function is an independent, self-contained section of code that is written to
perform a certain task. Every function has a name, and the code in each function
is executed by including that function’s name in a program statement. This execution is
known as calling the function.
The function named product(), in lines 26 through 29, is a user-defined function. As the
name implies, user-defined functions are written by the programmer during program
development. This function, which is included in lines 26 to 29, is simple. All it does is
multiply two values and return the answer to the program that called it. On Day 5,
“Functions: The Basics,” you will learn that the proper use of functions is an important
part of good C programming practice.
Note that in a real C program, you probably wouldn’t use a function for a task as simple
as multiplying two numbers. It has been done here for demonstration purposes only.
C also includes library functions that are a part of the C compiler package. Library functions
perform most of the common tasks (such as screen, keyboard, and disk input/output)
your program needs. In the sample program, printf() and scanf() are library
functions.
Program Comments (Lines 1, 10, 14, 18, and 25)
Any part of your program that starts with /* and ends with */ is called a comment.
The compiler ignores all comments, so they have absolutely no effect on
how a program works. You can put anything you want into a comment, and it won’t
modify the way your program operates. A comment can span part of a line, an entire
line, or multiple lines. Here are three examples:
NEW TERM
NEW TERM
/* A single-line comment */
int a,b,c; /* A partial-line comment */
/* a comment
spanning
multiple lines */
You should not use nested comments. A nested comment is a comment that has been put
into another comment. Most compilers will not accept the following:
/*
/* Nested comment */
*/
Some compilers do allow nested comments. Although this feature might be tempting to
use, you should avoid doing so. Because one of the benefits of C is portability, using a
feature such as nested comments might limit the portability of your code. Nested comments
also might lead to hard-to-find problems.
Many beginning programmers view program comments as unnecessary and a waste of
time. This is a mistake! The operation of your program might be quite clear when you’re
writing the code; however, as your programs become larger and more complex, or when
you need to modify a program you wrote six months ago, you’ll find comments invaluable.
Now is the time to develop the habit of using comments liberally to document all
your programming structures and operations.
The newest ANSI standard added the ability to use single line comments. Single line
comments have been available in C++ and Java so it is only natural that C has implemented
them.
Single line comments use double forward slashes to signal a comment. Here are two
examples:
// This entire line is a comment
int x; // Comment starts with slashes.
The two forward slashes signal that the rest of the line is a comment. The ANSI C-99
standard added this feature to C.
Using Braces (Lines 9, 23, 27, and 29)
You use braces ({}) to enclose the program lines that make up every C function—
including the main() function. A group of one or more statements
enclosed within braces is called a block. As you will see in later days, C has many uses
for blocks.
34 Day 2
NEW TERM
The Components of a C Program 35
2
Running the Program
Take the time to enter, compile, and run multiply.c. It provides additional practice in
using your editor and compiler. Recall these steps from Day 1, “Getting Started with C”:
1. Make your programming directory current.
2. Start your editor.
3. Enter the source code for multiply.c exactly as shown in Listing 2.1, but be sure to
omit the line numbers and colons.
4. Save the program file.
5. Compile and link the program by entering the appropriate command(s) for your
compiler. If no error messages are displayed, you can run the program by entering
multiply at the command prompt.
6. If any error messages are displayed, return to step 2 and correct the errors.
DO add abundant comments to your
program’s source code, especially near
statements or functions that could be
unclear to you or to someone who might
have to modify it later.
DO learn to develop a style that will be
helpful. A style that’s too lean or cryptic
doesn’t help. A style that is verbose may
cause you to spend more time commenting
than programming.
DON’T add unnecessary comments to
statements that are already clear. For
example, entering
/* The following prints Hello World!
on the screen */
printf(“Hello World!);
might be going a little too far, at least
once you’re completely comfortable with
the printf() function and how it works.
DO DON’T
If you
Program
Every C program consists of several components combined in a certain way.
Most of this book is devoted to explaining these various program components
and how you use them. To help illustrate the overall picture, you should begin
by reviewing a complete (though small) C program with all its components
identified. Today you will learn:
• About a short C program and its components
• The purpose of each program component
• How to compile and run a sample program
The Program’s Components
The following sections describe the various components of the preceding sample program.
Line numbers are included so that you can easily identify the program parts being
discussed.
The main() Function (Lines 8 Through 23)
The only component that is required in every executable C program is the main() function.
In its simplest form, the main() function consists of the name main followed by a
pair of parentheses containing the word void ((void)) and a pair of braces ({}). You can
leave the word void out and the program will still work with most compilers. The ANSI
standard states that you should include the word void so that you know there is nothing
being sent to the main function.
The #include Directive (Line 2)
The #include directive instructs the C compiler to add the contents of an include
file into your program during compilation. An include file is a separate disk file
that contains information that can be used by your program or the compiler. Several of
these files (sometimes called header files) are supplied with your compiler. You rarely
need to modify the information in these files; that’s why they’re kept separate from your
source code. Include files should all have an .h extension (for example, stdio.h).
You use the #include directive to instruct the compiler to add a specific include file to
your program during compilation. In Listing 2.1, the #include directive is interpreted to
LISTING 2.1 continued
INPUT/
OUTPUT
NEW TERM
mean “Add the contents of the file stdio.h.” You will almost always include one or more
include files in your C programs. More information about include files is presented on
Day 21, “Advanced Compiler Use.”
The Variable Definition (Line 4)
A variable is a name assigned to a location in memory used to store information.
Your program uses variables to store various kinds of information during program
execution. In C, a variable must be defined before it can be used. A variable definition
informs the compiler of the variable’s name and the type of information the variable
is to hold. In the sample program, the definition on line 4, int val1, val2, val3;,
defines three variables—named val1, val2, and val3—that will each hold an integer
value. More information about variables and variable definitions is presented on Day 3,
“Storing Information: Variables and Constants.”
The Function Prototype (Line 6)
A function prototype provides the C compiler with the name and arguments of
the functions contained in the program. It appears before the function is used. A
function prototype is distinct from a function definition, which contains the actual statements
that make up the function. (Function definitions are discussed in more detail later
today.)
Program Statements (Lines 11, 12, 15, 16, 19, 20, 22,
and 28)
The real work of a C program is done by its statements. C statements display information
on-screen, read keyboard input, perform mathematical operations, call functions, read
disk files, and all the other operations that a program needs to perform. Most of this book
is devoted to teaching you the various C statements. For now, remember that in your
source code, C statements are generally written one per line and always end with a semicolon.
The statements in multiply.c are explained briefly in the following sections.
The printf() Statement
The printf() statement (lines 11, 15, and 20) is a library function that displays information
on-screen. The printf() statement can display a simple text message (as in lines 11
and 15) or a message and the value of one or more program variables (as in line 20).
The scanf() Statement
The scanf() statement (lines 12 and 16) is another library function. It reads data from
the keyboard and assigns that data to one or more program variables.
32 Day 2
NEW TERM
NEW TERM
The Components of a C Program 33
2
The program statement on line 19 calls the function named product(). In other words, it
executes the program statements contained in the function product(). It also sends the
arguments val1 and val2 to the function. After the statements in product() are completed,
product() returns a value to the program. This value is stored in the variable
named val3.
The return Statement
Lines 22 and 28 contain return statements. The return statement on line 28 is part of
the function product(). It calculates the product of the variables x and y and returns the
result to the program that called product(). The return statement on line 22 returns a
value of 0 to the operating system just before the program ends.
The Function Definition (Lines 26 Through 29)
A function is an independent, self-contained section of code that is written to
perform a certain task. Every function has a name, and the code in each function
is executed by including that function’s name in a program statement. This execution is
known as calling the function.
The function named product(), in lines 26 through 29, is a user-defined function. As the
name implies, user-defined functions are written by the programmer during program
development. This function, which is included in lines 26 to 29, is simple. All it does is
multiply two values and return the answer to the program that called it. On Day 5,
“Functions: The Basics,” you will learn that the proper use of functions is an important
part of good C programming practice.
Note that in a real C program, you probably wouldn’t use a function for a task as simple
as multiplying two numbers. It has been done here for demonstration purposes only.
C also includes library functions that are a part of the C compiler package. Library functions
perform most of the common tasks (such as screen, keyboard, and disk input/output)
your program needs. In the sample program, printf() and scanf() are library
functions.
Program Comments (Lines 1, 10, 14, 18, and 25)
Any part of your program that starts with /* and ends with */ is called a comment.
The compiler ignores all comments, so they have absolutely no effect on
how a program works. You can put anything you want into a comment, and it won’t
modify the way your program operates. A comment can span part of a line, an entire
line, or multiple lines. Here are three examples:
NEW TERM
NEW TERM
/* A single-line comment */
int a,b,c; /* A partial-line comment */
/* a comment
spanning
multiple lines */
You should not use nested comments. A nested comment is a comment that has been put
into another comment. Most compilers will not accept the following:
/*
/* Nested comment */
*/
Some compilers do allow nested comments. Although this feature might be tempting to
use, you should avoid doing so. Because one of the benefits of C is portability, using a
feature such as nested comments might limit the portability of your code. Nested comments
also might lead to hard-to-find problems.
Many beginning programmers view program comments as unnecessary and a waste of
time. This is a mistake! The operation of your program might be quite clear when you’re
writing the code; however, as your programs become larger and more complex, or when
you need to modify a program you wrote six months ago, you’ll find comments invaluable.
Now is the time to develop the habit of using comments liberally to document all
your programming structures and operations.
The newest ANSI standard added the ability to use single line comments. Single line
comments have been available in C++ and Java so it is only natural that C has implemented
them.
Single line comments use double forward slashes to signal a comment. Here are two
examples:
// This entire line is a comment
int x; // Comment starts with slashes.
The two forward slashes signal that the rest of the line is a comment. The ANSI C-99
standard added this feature to C.
Using Braces (Lines 9, 23, 27, and 29)
You use braces ({}) to enclose the program lines that make up every C function—
including the main() function. A group of one or more statements
enclosed within braces is called a block. As you will see in later days, C has many uses
for blocks.
34 Day 2
NEW TERM
The Components of a C Program 35
2
Running the Program
Take the time to enter, compile, and run multiply.c. It provides additional practice in
using your editor and compiler. Recall these steps from Day 1, “Getting Started with C”:
1. Make your programming directory current.
2. Start your editor.
3. Enter the source code for multiply.c exactly as shown in Listing 2.1, but be sure to
omit the line numbers and colons.
4. Save the program file.
5. Compile and link the program by entering the appropriate command(s) for your
compiler. If no error messages are displayed, you can run the program by entering
multiply at the command prompt.
6. If any error messages are displayed, return to step 2 and correct the errors.
DO add abundant comments to your
program’s source code, especially near
statements or functions that could be
unclear to you or to someone who might
have to modify it later.
DO learn to develop a style that will be
helpful. A style that’s too lean or cryptic
doesn’t help. A style that is verbose may
cause you to spend more time commenting
than programming.
DON’T add unnecessary comments to
statements that are already clear. For
example, entering
/* The following prints Hello World!
on the screen */
printf(“Hello World!);
might be going a little too far, at least
once you’re completely comfortable with
the printf() function and how it works.
DO DON’T
If you
Comments
Post a Comment