Table of Contents for
Intermediate C Programming

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Intermediate C Programming by Yung-Hsiang Lu Published by CRC Press, 2015
  1. Front Cover
  2. Contents (1/2)
  3. Contents (2/2)
  4. List of Figures (1/2)
  5. List of Figures (2/2)
  6. List of Tables
  7. Foreword
  8. Preface
  9. Author, Reviewers, and Artist
  10. Rules in Software Development
  11. Source Code
  12. I. Computer Storage: Memory and File
  13. 1. Program Execution (1/2)
  14. 1. Program Execution (2/2)
  15. 2. Stack Memory (1/5)
  16. 2. Stack Memory (2/5)
  17. 2. Stack Memory (3/5)
  18. 2. Stack Memory (4/5)
  19. 2. Stack Memory (5/5)
  20. 3. Prevent, Detect, and Remove Bugs (1/2)
  21. 3. Prevent, Detect, and Remove Bugs (2/2)
  22. 4. Pointers (1/6)
  23. 4. Pointers (2/6)
  24. 4. Pointers (3/6)
  25. 4. Pointers (4/6)
  26. 4. Pointers (5/6)
  27. 4. Pointers (6/6)
  28. 5. Writing and Testing Programs (1/4)
  29. 5. Writing and Testing Programs (2/4)
  30. 5. Writing and Testing Programs (3/4)
  31. 5. Writing and Testing Programs (4/4)
  32. 6. Strings (1/3)
  33. 6. Strings (2/3)
  34. 6. Strings (3/3)
  35. 7. Programming Problems and Debugging (1/4)
  36. 7. Programming Problems and Debugging (2/4)
  37. 7. Programming Problems and Debugging (3/4)
  38. 7. Programming Problems and Debugging (4/4)
  39. 8. Heap Memory (1/3)
  40. 8. Heap Memory (2/3)
  41. 8. Heap Memory (3/3)
  42. 9. Programming Problems Using Heap Memory (1/4)
  43. 9. Programming Problems Using Heap Memory (2/4)
  44. 9. Programming Problems Using Heap Memory (3/4)
  45. 9. Programming Problems Using Heap Memory (4/4)
  46. 10. Reading and Writing Files (1/3)
  47. 10. Reading and Writing Files (2/3)
  48. 10. Reading and Writing Files (3/3)
  49. 11. Programming Problems Using File (1/2)
  50. 11. Programming Problems Using File (2/2)
  51. II. Recursion
  52. 12. Recursion (1/4)
  53. 12. Recursion (2/4)
  54. 12. Recursion (3/4)
  55. 12. Recursion (4/4)
  56. 13. Recursive C Functions (1/4)
  57. 13. Recursive C Functions (2/4)
  58. 13. Recursive C Functions (3/4)
  59. 13. Recursive C Functions (4/4)
  60. 14. Integer Partition (1/5)
  61. 14. Integer Partition (2/5)
  62. 14. Integer Partition (3/5)
  63. 14. Integer Partition (4/5)
  64. 14. Integer Partition (5/5)
  65. 15. Programming Problems Using Recursion (1/5)
  66. 15. Programming Problems Using Recursion (2/5)
  67. 15. Programming Problems Using Recursion (3/5)
  68. 15. Programming Problems Using Recursion (4/5)
  69. 15. Programming Problems Using Recursion (5/5)
  70. III. Structure
  71. 16. Programmer-Defined Data Types (1/6)
  72. 16. Programmer-Defined Data Types (2/6)
  73. 16. Programmer-Defined Data Types (3/6)
  74. 16. Programmer-Defined Data Types (4/6)
  75. 16. Programmer-Defined Data Types (5/6)
  76. 16. Programmer-Defined Data Types (6/6)
  77. 17. Programming Problems Using Structure (1/4)
  78. 17. Programming Problems Using Structure (2/4)
  79. 17. Programming Problems Using Structure (3/4)
  80. 17. Programming Problems Using Structure (4/4)
  81. 18. Linked Lists (1/3)
  82. 18. Linked Lists (2/3)
  83. 18. Linked Lists (3/3)
  84. 19. Programming Problems Using Linked List (1/2)
  85. 19. Programming Problems Using Linked List (2/2)
  86. 20. Binary Search Trees (1/4)
  87. 20. Binary Search Trees (2/4)
  88. 20. Binary Search Trees (3/4)
  89. 20. Binary Search Trees (4/4)
  90. 21. Parallel Programming Using Threads (1/5)
  91. 21. Parallel Programming Using Threads (2/5)
  92. 21. Parallel Programming Using Threads (3/5)
  93. 21. Parallel Programming Using Threads (4/5)
  94. 21. Parallel Programming Using Threads (5/5)
  95. IV. Applications
  96. 22. Finding the Exit of a Maze (1/5)
  97. 22. Finding the Exit of a Maze (2/5)
  98. 22. Finding the Exit of a Maze (3/5)
  99. 22. Finding the Exit of a Maze (4/5)
  100. 22. Finding the Exit of a Maze (5/5)
  101. 23. Image Processing (1/3)
  102. 23. Image Processing (2/3)
  103. 23. Image Processing (3/3)
  104. 24. Huffman Compression (1/10)
  105. 24. Huffman Compression (2/10)
  106. 24. Huffman Compression (3/10)
  107. 24. Huffman Compression (4/10)
  108. 24. Huffman Compression (5/10)
  109. 24. Huffman Compression (6/10)
  110. 24. Huffman Compression (7/10)
  111. 24. Huffman Compression (8/10)
  112. 24. Huffman Compression (9/10)
  113. 24. Huffman Compression (10/10)
  114. A. Linux
  115. B. Version Control
  116. C. Integrated Development Environments (IDE) (1/3)
  117. C. Integrated Development Environments (IDE) (2/3)
  118. C. Integrated Development Environments (IDE) (3/3)
Chapter 1
Program Execution
1.1 Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Redirect Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1 Compile
This chapter explains how to write, compile, and execute programs in Linux. We use
a Linux terminal and explain the commands you need to type. Why do you learn how
to use the terminal? First, the terminal is a flexible and convenient interface for working
with a computer. It may take some experience to realize this, but learning how to use the
terminal may improve your productivity. Second, many cloud computing or web services
offer terminal access. This is a natural method of providing computing resources, especially
when working with many computers (like in a data center). A graphical user interface (GUI)
is nice when working with one computer. However, when dealing with many computers, GUI
can become a distraction. Also, using the terminal helps you understand how UNIX systems
work. After becoming familiar with terminal commands, you may understand integrated
development environments (IDEs), and what they can do for you. The Eclipse IDE is
explained later in this book.
Start a terminal in Linux and type
$ cd
$ pwd
$ mkdir cprogram
$ cd cprogram
In this book, $ is used as the terminal prompt.
The first command cd means “change directory”. If no argument is added after cd, as
in the first command, then it will return to your home directory (also called “folder”).
The second command pwd means “print the current working directory”. It will be some-
thing like /home/yourname/.
The third command mkdir means “make a directory”. The command mkdir cprogram
means “make a directory whose name is cprogram”.
You should not create a directory or a file whose name includes spaces. The reason is very
simple: The international standard for directory names and file names (called International
Standard Organization or ISO 9660) disallows spaces. If a directory’s or a file’s name has
spaces, then some programs may not work.
The last command cd cprogram means “change directory to (i.e., enter) cprogram”.
This is the directory that was just created.
In the terminal, type
$ which emacs
If nothing appears in the terminal or it says “Command not found”, then please install
3
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
Program Execution 5
This command means the following:
Execute the gcc command installed in Linux.
Use prog1.c as the input for the gcc command.
Set the output file name to be prog (-o specifies the name of the output file). The
output file is an executable file, meaning that the computer can run it.
Do not do this:
$ gcc prog1.c -o prog1.c
This command erases the file prog1.c.
The gcc compiler has many options. Please read the documentation to learn more. To
find the documentation, type “linux gcc” in an Internet search engine and you will see
something like the following:
GCC(1) GNU GCC(1)
NAME
gcc - GNU project C and C++ compiler
SYNOPSIS
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [@file] infile...
Only the most useful options are listed here; see below for the
remainder. g++ accepts mostly the same options as gcc.
DESCRIPTION
When you invoke GCC, it normally does preprocessing,
compilation, assembly and linking. The "overall options" allow you
to stop this process at an intermediate stage. For example, the -c
option says not to run the linker. Then the output consists of
object files output by the assembler.
Other options are passed on to one stage of processing. Some
options control the preprocessor and others the compiler itself. Yet
other options control the assembler and linker; most of these are not
documented here, since you rarely need to use any of them.
Most of the command line options that you can use with GCC are
useful for C programs; when an option is only useful with another
language
The document is also called the “man page”, where “man” means manual. These manual
pages are typically well written but terse. Early computers were very expensive and the
designers tried to keep everything as short as possible. We have seen a few Linux commands
already:
6 Intermediate C Programming
cd: change directory
gcc: convert a human-readable file to a computer-readable file
man: display a manual page
mkdir: make a new directory
The output of the gcc command is an executable. In Linux, it is possible to find some
information about a file by using the file command. Please type this in the terminal:
$ file prog
The output should be similar to the following, but the specifics will vary depending on
the computer the program is compiled on:
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.24,
BuildID[sha1]=0x65dfd5517523d920d9fbaf4ededa84792a9e9c61, not
stripped
The most important thing to pay attention to is the word “executable”. This word
means that the file “prog” is a program. By convention, executable files in Linux have no
extension, unlike “.exe” used in Windows. How do you execute the program? Type this
command:
$ ./prog
Here, prog is the name of the program; ./ means the current directory. Why is it
necessary to add ./ in front of the program? It is necessary because it is possible to have
files of the same name in different directories. By adding ./, the terminal knows that the
desired program is in this directory. Some people like to call their programs “test”. This is
a bad name for your program because “test” is also a built-in command in Linux. If you
type
$ test
then the Linux command is run. If you type
$ ./test
then your program in the directory is run.
What exactly is argc? It stores the number of arguments given to the program. Let me
demonstrate what I mean by running the program a few times.
$ ./prog
main: a = 5, b = 17, argc = 1
$ ./prog abc
main: a = 5, b = 17, argc = 2
$ ./prog abc 123
main: a = 5, b = 17, argc = 3
$ ./prog abc 123 C Programs
main: a = 5, b = 17, argc = 5
Program Execution 7
Do you notice the changes in argc? When the program is executed without anything
else, argc is 1. If some words are added after the program, then argc becomes larger.
The more words (i.e., arguments) that are added, the larger argc becomes. This illustrates
that arguments can be given to programs when they are run. Consecutive arguments are
separated by one or more spaces. The terminal tells your program (specifically, the main
function) the number of arguments. As can be seen in the examples below, adding extra
spaces between words makes no difference. One space has the same effect as several spaces.
$ ./prog abc 123 C Programs
main: a = 5, b = 17, argc = 5
$ ./prog abc 123 C Programs
main: a = 5, b = 17, argc = 5
$ ./prog abc 123 C Programs
main: a = 5, b = 17, argc = 5
The program itself is always the first argument. Since you must type the program’s name
to run the program, the value of argc is always at least one. What is the value of argc
when using this gcc command?
$ gcc prog1.c -o prog
The answer is 4.
1. gcc
2. prog1.c
3. -o
4. prog
The arguments themselves are strings, and are stored in argv. This will be covered when
explaining strings in Chapter 6.
1.2 Redirect Output
The printf function is probably one of the first things that people learn about writing
C programs. The famous “Hello World!” program is often used as an example for beginners.
In this computer program, the text is printed to the terminal. In some cases, however, it
is useful to redirect the information from the program and save it to a file. Here are some
scenarios when this may be useful:
A program prints too much too fast and the computer screen cannot display everything
printed.
You do not want to wait while the program runs, but instead want to see the infor-
mation later.
Sometimes it is useful to check whether the program produces the same information
when it is run again. If the program produces more than several lines of output,
checking the output line-by-line is too much work.
The program may need to be run on many computers simultaneously. It may be
impossible to watch many screens at once.