Skip to content

Commit a2842be

Browse files
committed
Enhance project configuration and documentation
- Updated .editorconfig to set indent size for C and header files. - Modified Makefile to include linting and formatting tasks, along with a check target for running both. - Improved README.md with clearer instructions on building and contributing, and added sections for tools and prerequisites. - Updated GitHub Actions workflow to include formatting and linting checks before building.
1 parent 2f1fed8 commit a2842be

File tree

6 files changed

+165
-34
lines changed

6 files changed

+165
-34
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ end_of_line = lf
1010
insert_final_newline = true
1111
trim_trailing_whitespace = true
1212

13+
[*.{c,h}]
14+
indent_size = 4
15+
1316
[*.md]
1417
insert_final_newline = false
1518
trim_trailing_whitespace = false

.github/workflows/c-cpp.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ on:
77
branches: [main]
88

99
jobs:
10-
build:
10+
check:
1111
runs-on: ubuntu-latest
12-
1312
steps:
14-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v4
14+
15+
- name: Check formatting
16+
run: make format-check
17+
18+
- name: Lint
19+
run: make lint
1520

16-
- name: Build all .c files
21+
- name: Build
1722
run: make

CONTRIBUTING.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Contributing
2+
3+
Thanks for your interest in contributing! This guide will help you get started.
4+
5+
## Quick Start
6+
7+
1. Fork and clone the repository
8+
2. Create a new branch: `git checkout -b my-fix`
9+
3. Make your changes
10+
4. Format your code: `make format`
11+
5. Test your changes: `make`
12+
6. Commit and push
13+
7. Open a pull request
14+
15+
## Requirements
16+
17+
You'll need:
18+
- A C compiler (GCC or Clang)
19+
- `clang-format` for code formatting
20+
- `make` for building
21+
22+
### Installing clang-format
23+
24+
**macOS:**
25+
```shell
26+
brew install clang-format
27+
```
28+
29+
**Ubuntu / Debian:**
30+
```shell
31+
sudo apt-get install clang-format
32+
```
33+
34+
**Arch Linux:**
35+
```shell
36+
sudo pacman -S clang
37+
```
38+
39+
**Fedora:**
40+
```shell
41+
sudo dnf install clang-tools-extra
42+
```
43+
44+
## Available Commands
45+
46+
| Command | Description |
47+
|---------|-------------|
48+
| `make` | Build all exercises |
49+
| `make format` | Format all code |
50+
| `make format-check` | Check formatting (used by CI) |
51+
| `make lint` | Run linter checks |
52+
| `make check` | Run all checks (format + lint) |
53+
| `make clean` | Remove compiled files |
54+
55+
## Before Submitting
56+
57+
Please run these commands before submitting a pull request:
58+
59+
```shell
60+
make format # Format your code
61+
make check # Run all checks
62+
```
63+
64+
The CI will automatically check formatting and linting on your pull request.
65+
66+
## Code Style
67+
68+
We use `clang-format` with minimal configuration (LLVM style, 4-space indentation). Just run `make format` and your code will be properly formatted.
69+
70+
## What to Contribute
71+
72+
- **Bug fixes** - Found an error in a solution? Please fix it!
73+
- **Improvements** - Better algorithms, clearer code, or better comments
74+
- **Missing solutions** - Some exercises may be missing solutions
75+
- **Documentation** - Improve explanations or add helpful comments
76+
77+
## Questions?
78+
79+
Feel free to open an issue if you have questions or need help!

Makefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@ SOURCES = $(call rwildcard, ., *.c, *.h)
44
DEBUG_FILES = $(call rwildcard, ., *.dSYM)
55
EXECS = $(SOURCES:%.c=%)
66
LDLIBS = -lm
7-
CFLAGS = -Wall
7+
CFLAGS = -Wall -Wextra -Wpedantic
88

99
all: $(EXECS)
10+
11+
lint:
12+
@echo "Linting with compiler warnings..."
13+
@for f in $(filter %.c, $(SOURCES)); do \
14+
$(CC) $(CFLAGS) -fsyntax-only $$f 2>&1 || exit 1; \
15+
done
16+
@echo "Lint passed!"
17+
18+
format:
19+
find . -name '*.c' -o -name '*.h' | xargs clang-format -i
20+
21+
format-check:
22+
find . -name '*.c' -o -name '*.h' | xargs clang-format --dry-run --Werror
23+
24+
check: format-check lint
25+
1026
clean:
1127
rm -rf $(EXECS) $(DEBUG_FILES)
28+
29+
.PHONY: all lint format format-check check clean

README.md

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,78 @@
22

33
![C/C++ CI](https://github.com/gleesik/the-c-programming-language-2nd-edition-solutions/workflows/C/C++%20CI/badge.svg)
44

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**.
76

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
98

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
1110

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:
2012

13+
**macOS:**
2114
```shell
2215
xcode-select --install
2316
```
2417

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).
2725

28-
##### Ubuntu / Debian / Debian derivatives
26+
### Building
27+
28+
Build all exercises:
2929
```shell
30-
sudo apt-get update
31-
sudo apt-get install build-essential
30+
make
3231
```
3332

34-
##### Arch Linux
33+
Build a specific exercise:
3534
```shell
36-
sudo pacman -Sy base-devel
35+
cd chapter_1/exercise_1_01
36+
make hello_world
37+
./hello_world
3738
```
3839

39-
##### Fedora
40+
Clean up:
4041
```shell
41-
sudo yum update
42-
sudo yum groupinstall "Development Tools" "Legacy Software Development"
42+
make clean
43+
```
44+
45+
## Repository Structure
46+
4347
```
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.
4468

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!
4770

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
4972

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)
5276

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
5478

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

Comments
 (0)