
4 Intermediate C Programming
Emacs first. If you do not know how to install software in Linux, please read Section A.5.
In the terminal, type
$ emacs prog1.c &
This command starts Emacs to edit a file called prog1.c. Adding & allows you to use the
terminal as well as the Emacs editor at the same time. Without the trailing &, the terminal
will force you to wait until Emacs quits. Inside Emacs, type the following code:
// prog1 .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
int main ( i n t argc , char * * argv )4
{5
int a = 5;6
int b = 17;7
printf (" main : a = %d , b = %d , argc = %d \n" , a , b, argc );8
return EXIT _SUCCES S ;9
}10
Save the file. You can probably guess that this program prints something like
main: a = 5, b = 17, argc =
This is the first complete program shown in this book and requires some explanation. This
program prints something by calling printf. This is a function provided by the C language
but you need to include stdio.h before you can use this function. This is a header file for
standard input and output functions. In a C program, the starting point is the main function.
The program returns EXIT SUCCESS after it successfully prints the addresses. As you can
guess, if a program can return EXIT SUCCESS, another program can return EXIT FAILURE.
Why should a program return either EXIT SUCCESS or EXIT FAILURE? In today’s complex
computer systems, many programs are called by other computer programs. Thus, it is
important that your programs inform the calling programs whether your programs have
successfully accomplished what they are designed to do. This information allows the calling
programs to decide what actions to take next. EXIT SUCCESS and EXIT FAILURE are symbols
defined in stdlib.h so it is included at the second line.
In this book, source code is listed with line numbers, starting from 1. Sometimes, the
code refers to a previously mentioned example and the line number corrsponds to the value
in the earlier example.
The main function is the starting point of a C program but this is not always true for
a C++ program. If a C++ program has a static object, the object’s constructor will be
called before the main function is called. Since this book is about C programming, it is safe
to assume that the main function is the starting point of all the programs.
What is argc? It is easier to answer this question by running the program. First, we
need to explain how to convert this program from a human-readable format to a computer-
readable format.
What is typed into Emacs is a “C source file”. It is vaguely similar to English and
consists of Latin alphabet letters. However, since the computer does not understand this
format, the “source file” needs to be converted into a computer-readable format called an
executable. A compiler is the tool needed for this conversion and gcc is a popular compiler
on Linux. In the terminal, type
$ gcc prog1.c -o prog