- 2.1. Introduction
- 2.2. A Simple C Program: Printing a Line of Text
- 2.3. Another Simple C Program: Adding Two Integers
- 2.4. Arithmetic in C
- 2.5. Decision Making: Equality and Relational Operators
- 2.6. Secure C Programming
2.2. A Simple C Program: Printing a Line of Text
We begin by considering a simple C program. Our first example prints a line of text. The program and its screen output are shown in Fig. 2.1.
Fig. 2.1. A first program in C.
1
// Fig. 2.1: fig02_01.c
2
//A first program in C.
3
#include <stdio.h>4
5
// function main begins program execution
6
int main( void )7
{8
printf("Welcome to C!\n"
);9
}// end function main
Comments
This program illustrates several important C features. Lines 1 and 2
// Fig. 2.1: fig02_01.c
// A first program in C
begin with //, indicating that these two lines are comments. Comments do not cause the computer to perform any action when the program is run. Comments are ignored by the C compiler and do not cause any machine-language object code to be generated. The preceding comment simply describes the figure number, file name and purpose of the program.
You can also use /*...*/ multiline comments in which everything from /* on the first line to */ at the end of the last line is a comment. We prefer // comments because they’re shorter and they eliminate common programming errors that occur with /*...*/ comments, especially when the closing */ is omitted.
#include Preprocessor Directive
Line 3
#include <stdio.h>
is a directive to the C preprocessor. Lines beginning with # are processed by the preprocessor before compilation. Line 3 tells the preprocessor to include the contents of the standard input/output header (<stdio.h>) in the program. This header contains information used by the compiler when compiling calls to standard input/output library functions such as printf (line 8). We explain the contents of headers in more detail in Chapter 5.
Blank Lines and White Space
Line 4 is simply a blank line. You use blank lines, space characters and tab characters (i.e., “tabs”) to make programs easier to read. Together, these characters are known as white space. White-space characters are normally ignored by the compiler.
The main Function
Line 6
int main( void )
is a part of every C program. The parentheses after main indicate that main is a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. Functions can return information. The keyword int to the left of main indicates that main “returns” an integer (whole-number) value. We’ll explain what this means when we demonstrate how to create your own functions in Chapter 5. For now, simply include the keyword int to the left of main in each of your programs. Functions also can receive information when they’re called upon to execute. The void in parentheses here means that main does not receive any information. In Chapter 14, we’ll show an example of main receiving information.
A left brace, {, begins the body of every function (line 7). A corresponding right brace ends each function (line 9). This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C.
An Output Statement
Line 8
printf( "Welcome to C!\n" );
instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal. The entire line, including the printf function (the “f” stands for “formatted”), its argument within the parentheses and the semicolon (;), is called a statement. Every statement must end with a semicolon (also known as the statement terminator). When the preceding printf statement is executed, it prints the message Welcome to C! on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement.
Escape Sequences
Notice that the characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. Some common escape sequences are listed in Fig. 2.2.
Fig. 2.2. Some common escape sequences.
Escape sequence |
Description |
\n |
Newline. Position the cursor at the beginning of the next line. |
\t |
Horizontal tab. Move the cursor to the next tab stop. |
\a |
Alert. Produces a sound or visible alert without changing the current cursor position. |
\\ |
Backslash. Insert a backslash character in a string. |
\" |
Double quote. Insert a double-quote character in a string. |
Because the backslash has special meaning in a string, i.e., the compiler recognizes it as an escape character, we use a double backslash (\\) to place a single backslash in a string. Printing a double quote also presents a problem because double quotes mark the boundaries of a string—such quotes are not printed. By using the escape sequence \" in a string to be output by printf, we indicate that printf should display a double quote. The right brace, }, (line 9) indicates that the end of main has been reached.
We said that printf causes the computer to perform an action. As any program executes, it performs a variety of actions and makes decisions. Section 2.5 discusses decision making. Chapter 3 discusses this action/decision model of programming in depth.
The Linker and Executables
Standard library functions like printf and scanf are not part of the C programming language. For example, the compiler cannot find a spelling error in printf or scanf. When the compiler compiles a printf statement, it merely provides space in the object program for a “call” to the library function. But the compiler does not know where the library functions are—the linker does. When the linker runs, it locates the library functions and inserts the proper calls to these library functions in the object program. Now the object program is complete and ready to be executed. For this reason, the linked program is called an executable. If the function name is misspelled, the linker will spot the error, because it will not be able to match the name in the C program with the name of any known function in the libraries.
Using Multiple printfs
The printf function can print Welcome to C! several different ways. For example, the program of Fig. 2.3 produces the same output as the program of Fig. 2.1. This works because each printf resumes printing where the previous printf stopped printing. The first printf (line 8) prints Welcome followed by a space, and the second printf (line 9) begins printing on the same line immediately following the space.
Fig. 2.3. Printing on one line with two printf statements.
1
// Fig. 2.3: fig02_03.c
2
//Printing on one line with two printf statements.
3
#include <stdio.h>4
5
// function main begins program execution
6
int main( void )7
{8
printf(
"Welcome " );
9
printf(
"to C!\n" );
10
}// end function main
One printf can print several lines by using additional newline characters as in Fig. 2.4. Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line.
Fig. 2.4. Printing multiple lines with a single printf.
1
// Fig. 2.4: fig02_04.c
2
//Printing multiple lines with a single printf.
3
#include <stdio.h>4
5
// function main begins program execution
6
int main( void )7
{8
printf("Welcome
to
\n
C!\n" );
\n
9
}// end function main