Skip to main content

C Programming

Description

The repo contains my solution to the exercises and projects given in the book. The answers--although provides the expected answers--is still prone to unexpected input.

Understanding the Code

The code provided are documented. Since there are plenty of exercises which refers to the previous ones, the logic of the code is documented in the preceeding exercises. For instance, there may be some exercise/project in, say, Chapter 10, which refers to modifying the program from, say, Chapter 5. For such programs, the documentation is provided in Chapter 5 and is not found in the program of Chapter 10. This helps documenting the code easier and no redundant comments are there.

Running the Program

Each sub-directories has Makefile which can prepare the executable for the contents in the respective sub-directories. The provided Makefile is basic to say the least, but it performed as wanted on my machine. To test out a program, simply change the directory to the given sub-directory and enter the make command on the shell. Example:

# Change to the required sub-directory.
cd Chapter\ 19\ -\ Program\ Design/Exercises/exercise_3

# makes the program, target is usually optional.
make <target>

# After done with the program, clean up executable and object files by entering:
make clean

A Word of Caution

The code provided is not tested for various inputs that can be provided. Most programs in the later chapters contains some mechanism to only take the desired input and discard the unwanted ones.

Support the Author

K.N. King has provided a concise yet descriptive book that provides insight on the C programming language. The book provides exercises and projects to the readers as challenges while reading the book. The exercises and projects are not too difficult yet provides usuage of projects on real world applications. The book can be purchased from here.

Some Interesting Projects

Here are some of the exercises/projects that may be interesting to understand:

  1. Queue using Abstract Data Type (ADT). It points to the exercise_3 directory which contains two different implementations of the queue ADT (using an array and a linked list).

  2. Stack ADT which has the "generic" type. There are various implementation of the stack ADT (using defined type specifier, type definiton, and such).

  3. Understanding "complex" variable declaration. The program illustrates one "complex" variable declaration, but understanding one can easily make the reader realize how variable declarations work. This and this can be useful for the reader too.

  4. Function returns and array elements. This illustrates how we can use the functions to returns type not supported by the language. For instance, a function can't return an array, nor can it return another function. Likewise, an array cannot store function as its elements.

  5. Basic inventory program that doubles the size of the array at runtime. This is one of many inventory programs which can be studied to understand various language features. Programming Project 2 and 3 are also part of the inventory program with features as described in the question found at the top of the source file.

  6. Sorting various words obtained from standard input. There is also an modification to this program that uses built-in qsort library function to sort words which can be found here.

  7. Reminder program that utilizes flexible array members in a structure. Similar to the inventory program, the reminders program is used in various chapters and this is one of the latest which includes new features like the usage of flexible array members in a structure.

  8. Linked list Implementation. This has one functionality added to the linked list described in the Chapter. Exercise 12 and Exercise 13 has some functionality for the linekd list as well.

  9. Very basic "class" in C (sort of). The word "class" in used in a very vague way here. It just means that we created a structure with members such as variables and pointers.

  10. Malloc wrapper. This basic program illustrate the creation of a "wrapper" function for the library function malloc.

  11. Mapping country codes. This program checks whether the country code entered by the user using the standard input exists in the array of structures. If the country exists, the corresponding country code is shown to the user.

  12. Inventory with price. As stated eariler, this is another one of inventory program which requires some modifications as indicated by the question.

  13. Working with dates. This program is a modification of previous program (mentioned in the source file), which uses the concept of structure to store dates.

  14. Departure time. Another one of the recurring question which uses various concepts as we move on to later chapters.

  15. Chess. This program shows the "chess board" configuration. Also, this one is also regarding chess...

  16. Working with fractions. This program utilizes the idea of structure to make rational numbers which is then used for basic mathematical operations like addition, subtraction, multiplication, and division.

  17. Justify THIS!. The justify set of programs are interesting to understand. As the name implies, the program is used to justify the unformatted text which is provided to the program. NOTE: To use this program, first prepare the executable by using the make command. Then, use the input redirection (<) to feed the program the input, i.e. use ./project_1 < quote. If you want to the formatted text to be output somewhere apart from the standard output, use the output redirection (>), i.e. ./project_1 < quote > test_test.

  18. Split the sort. This program illustrates breaking the source file into multiple files. Make sure to read the Makefile to get some more understanding. Small thing regarding the "magic symbols" in Makefile. You may encounter Makefiles with symbols like $@, $<, and $^. Before jumping into these symbols, Makfile has the following structure:

    target: prerequisite
    commands # make requires tabs, not spaces.

    Here, $@ will be replaced by the "target", $< will be replaced by the first prerequisite, and $^ will be replaced by all the prerequisite specified.

  19. Macro expansion. This program is useful to understand basic macro expansion along with concept of conditional compilation. NOTE: Use the -E flag in compilation to generate the source file after preprocessor processes the macros and directives. This program also showcase the program before and after preprocessing. To learn about macro and function, Exercise 10 provides some basic understanding.

  20. "Generic" macros. This program provides a basic macro expansion for a macro that's "generic". It just expands into function definition which can be used later in the program.