|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -## Introduction |
6 | | -**The C Programming Language** is a very popular book and sometimes people refer to it as **K&R**. The authors *Brian W. Kernighan* and *Dennis M. Ritchie* did a very good job of explaining the core concepts of programming. The focus of the book is the C programming language, however, the approach is general, so it can be extrapolated to other programming languages. |
| 5 | +Solutions to exercises from **The C Programming Language** (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie, commonly known as **K&R**. |
7 | 6 |
|
8 | | -Each chapter of the book contains **exercises** that could be very helpful for a better understanding of the C language. The exercises are designed so that anybody can solve them with the knowledge acquired up to that exercise. |
| 7 | +## Getting Started |
9 | 8 |
|
10 | | -This repository contains the **solutions** to the exercises from each chapter of the book. These solutions are meant to be helpful for those who want to *learn* to program with the C language. |
| 9 | +### Prerequisites |
11 | 10 |
|
12 | | -## Environment |
13 | | -The source code is not tied up to an IDE, so any text editor will do the job. However, there are useful tasks and settings available for [Visual Studio Code](https://code.visualstudio.com). For a **better experience** using this editor, the [C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) provides some very helpful features specific to the C programming language. |
14 | | - |
15 | | -### Compilers |
16 | | -To be able to write programs in C, a compiler is required. There are many options available for each operating system. |
17 | | - |
18 | | -#### macOS |
19 | | -The [**Clang**](https://clang.llvm.org/get_started.html) compiler is a very nice choice when using macOS. It is available with **Xcode Command Line Tools**, which can be easily installed using the following command: |
| 11 | +You need a C compiler: |
20 | 12 |
|
| 13 | +**macOS:** |
21 | 14 | ```shell |
22 | 15 | xcode-select --install |
23 | 16 | ``` |
24 | 17 |
|
25 | | -#### Linux |
26 | | -The [**GCC**](https://gcc.gnu.org) compiler is a very popular way to build C programs and it is a good choice when using Linux. Each distro has its own set of **development tools** that comes with the GCC compiler out of the box. The development tools can be installed with the following commands: |
| 18 | +**Ubuntu / Debian:** |
| 19 | +```shell |
| 20 | +sudo apt-get update && sudo apt-get install build-essential |
| 21 | +``` |
| 22 | + |
| 23 | +**Windows:** |
| 24 | +Use [WSL](https://docs.microsoft.com/en-us/windows/wsl/install) (recommended) or [MinGW](http://www.mingw.org). |
27 | 25 |
|
28 | | -##### Ubuntu / Debian / Debian derivatives |
| 26 | +### Building |
| 27 | + |
| 28 | +Build all exercises: |
29 | 29 | ```shell |
30 | | -sudo apt-get update |
31 | | -sudo apt-get install build-essential |
| 30 | +make |
32 | 31 | ``` |
33 | 32 |
|
34 | | -##### Arch Linux |
| 33 | +Build a specific exercise: |
35 | 34 | ```shell |
36 | | -sudo pacman -Sy base-devel |
| 35 | +cd chapter_1/exercise_1_01 |
| 36 | +make hello_world |
| 37 | +./hello_world |
37 | 38 | ``` |
38 | 39 |
|
39 | | -##### Fedora |
| 40 | +Clean up: |
40 | 41 | ```shell |
41 | | -sudo yum update |
42 | | -sudo yum groupinstall "Development Tools" "Legacy Software Development" |
| 42 | +make clean |
| 43 | +``` |
| 44 | + |
| 45 | +## Repository Structure |
| 46 | + |
43 | 47 | ``` |
| 48 | +chapter_N/ |
| 49 | + exercise_N_XX/ |
| 50 | + solution.c # Solution source code |
| 51 | + file_in.txt # Input file (if needed) |
| 52 | + file_out.txt # Output file (if needed) |
| 53 | +``` |
| 54 | + |
| 55 | +## Contributing |
| 56 | + |
| 57 | +Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. |
| 58 | + |
| 59 | +Quick start: |
| 60 | +```shell |
| 61 | +make format # Format your code |
| 62 | +make check # Run all checks before submitting |
| 63 | +``` |
| 64 | + |
| 65 | +## About the Book |
| 66 | + |
| 67 | +**The C Programming Language** is a classic programming book. The exercises are designed so that you can solve them with the knowledge acquired up to that point in the book. |
44 | 68 |
|
45 | | -#### Windows |
46 | | -Because Windows is not a Unix like operating system, [**Windows Subsystem for Linux**](https://docs.microsoft.com/en-us/windows/wsl) (a.k.a. WSL) could be a very good approach when writing C programs. It provides a full Linux system that can make the programming experience much better. The official documentation has a pretty good explanation about how to [install WSL](https://docs.microsoft.com/en-us/windows/wsl/install-win10). |
| 69 | +These solutions are meant to help those learning C. If you're working through the book, try solving the exercises yourself first! |
47 | 70 |
|
48 | | -[**MinGW Compiler Collection**](http://www.mingw.org) is another good alternative to obtain access to the GCC compiler on a Windows system. The official documentation shows how it can be [installed](http://www.mingw.org/wiki/Getting_Started) step by step. |
| 71 | +## Tools |
49 | 72 |
|
50 | | -### Debuggers |
51 | | -A debugger is a tool that can become very handy when trying to find out how a program works or why it doesn't. There are many times when the code will compile successfully because syntactically there are no problems. However, that doesn't mean there aren't logical problems. If that is the case it might be a very good idea to use a debugger. |
| 73 | +- **Editor:** Any text editor works. [VS Code](https://code.visualstudio.com) with the [C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) is recommended. |
| 74 | +- **Debugger:** [LLDB](https://lldb.llvm.org) (macOS) or [GDB](https://www.gnu.org/software/gdb) (Linux) |
| 75 | +- **Formatter:** [clang-format](https://clang.llvm.org/docs/ClangFormat.html) |
52 | 76 |
|
53 | | -A very good option is [**LLDB**](https://lldb.llvm.org). It is the default debugger in Xcode on macOS and supports debugging C, Objective-C and C++. It converts debug information into Clang types so that it can leverage the Clang compiler infrastructure. |
| 77 | +## License |
54 | 78 |
|
55 | | -Another very popular option is [**GDB**](https://www.gnu.org/software/gdb). It supports the following languages (in alphabetical order): Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL, Modula-2, Pascal, Rust. |
| 79 | +This project is open source. Feel free to use these solutions for learning! |
0 commit comments