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)
Strings 95
The output of the program is:
argv[1] = This is his history book., strlen = 25
argv[2] = is, strlen = 2
is is his history book.
is his history book.
is history book.
istory book.
argv[2] occurs 4 times in argv[1].
Earlier we noted that spaces separate the command line arguments. If we enclose a
sentence in double quotation marks, the whole sentence is treated as a single argument,
as shown in this example. Lines 21 prints argv[1] and it is the whole sentence. Without
the quotation marks, “This” is argv[1] and “is” becomes argv[2], etc. We must use two
quotation marks; otherwise, the Terminal says:
Unmatched "
Below is the call stack for this example. It has been formatted to fit onto one page.
We have skipped the column for the frame because only one function is displayed. Line 25
assigns the value of argv[1] to ptr. This value is the address of argv[1][0], namely 116.
Symbol Address Value
argv[1][6] 122 s
argv[1][5] 121 i
argv[1][4] 120
argv[1][3] 119 s
argv[1][2] 118 i
argv[1][1] 117 h
argv[1][0] 116 T
argv[0][10] 115 \0’
argv[0][9] 114 r
argv[0][8] 113 t
argv[0][7] 112 s
argv[0][6] 111 t
argv[0][5] 110 n
argv[0][4] 109 u
argv[0][3] 108 o
argv[0][2] 107 c
argv[0][1] 106 /
argv[0][0] 105 .
argv[2] 104 142
argv[1] 103 116
argv[0] 102 105
argv 101 102
argc 100 3
Symbol Address Value
count 146 0
ptr 145 116
argv[2][2] 144 \0’
argv[2][1] 143 s
argv[2][0] 142 i
argv[1][25] 141 \0’
argv[1][24] 140 .
argv[1][23] 139 k
argv[1][22] 138 o
argv[1][21] 137 o
argv[1][20] 136 b
argv[1][19] 135
argv[1][18] 134 y
argv[1][17] 133 r
argv[1][16] 132 o
argv[1][15] 131 t
argv[1][14] 130 s
argv[1][13] 129 i
argv[1][12] 128 h
argv[1][11] 127
argv[1][10] 126 s
argv[1][9] 125 i
argv[1][8] 124 h
argv[1][7] 123
Line 28 finds “is” in “This is his history book.” The first occurrence of “is” is at address
118. This line changes ptr’s value to 118. Since it is not NULL, counter increments. Line
31 prints the string starting at the address where “is” is found. Recall that strings are
terminated by a null byte, and thus the entire string is printed starting from address 118.
Line 33 increments ptr to 119. This allows us to continue to search for “is” in the rest of
96 Intermediate C Programming
the argv[1]. Without this increment, strstr will search “is” from the address of 118, i.e.,
“is is his history book.”, and the first occurrence will be 118 again. The program would
enter an infinite loop if line 33 were removed. Instead, line 33 makes the program continue
searching from the address 119, i.e., “s is his history book.” The next occurrence of “is”
is at address 121. As ptr increments, it gradually moves toward the end of argv[1]. This
is evident from the output of line 31. After finding four occurrences, strstr cannot find
“is” any more and returns NULL. Note that count was incremented once every time strstr
returned a result other than NULL. Thus count now contains the number of times that “is”
was found in argv[1].
C provides many more functions for processing strings. You can find a list of the C’s
string processing functions by typing into the Terminal:
$ man string
man stands for “manual”. The manual displays a list of functions related to processing
strings, for example strcat.