All exercises from the famous K&R book implemented in both C and Rust!
2020 is the year to learn even more about programming. Why not start learning the C language? Why not learn Rust as well?
Language war is not new. You can find a lot of people saying that Rust will replace C in the future because of all of its security features. Some others are saying C will never go away, as it is the foundation of most things used today. This repo is a shy initiative to those seeking to learn both languages and maybe get the best of both worlds.
Just don't fight the Borrow Checker and don't let those pointer dereferences scare you!
!!! Disclaimer !!!
This repo is intended as a way to learn about syntax and structure from both languages. It is not supposed to be a deep comparison of their features. These are for demonstration/educational purposes only.
A tree defining the structure of this repo.
├── C
│ ├── chapter // Book chapters (1-8)
│ │ └── number.c // Exercise code (numbered within the book)
│ └── bin // Contains executables and object files
└── rustlang
│ ├── chapter // Book chapters (1-8)
│ │ └── number.rs // Exercise code (numbered within the book)
│ └── bin // Contains executables files
├── LICENSE // License file
├── Makefile // File used to generate executables and object files
├── README.md // The file you are reading
The exercises are linked in the table below using the same pattern from the book (e.g. [chapter]-[number]
).
Description | C |
Rust |
---|---|---|
Chapter 1 | --- | --- |
Hello, World! | 1-1 | 1-1 |
Printing with backlashed characters | 1-2 | 1-2 |
Fahrenheit to Celsius table (while loops) | 1-3 | 1-3 |
Celsius to Fahrenheit table (while loops) | 1-4 | 1-4 |
Fahrenheit to Celsius in reversed order (for loops) | 1-5 | 1-5 |
I/O with getchar and putchar | 1-6 | 1-6 |
Checking EOF value | 1-7 | 1-7 |
Counting blanks, tabs and newlines from stdin | 1-8 | 1-8 |
Replacing blanks from stdin | 1-9 | 1-9 |
Replacing tabs, backspaces and backlashes from stdin | 1-10 | 1-10 |
Implementing wc unix program and questioning its behavior | 1-11 | 1-11 |
Reading stdin and printing one word per line | 1-12 | 1-12 |
Drawing an histogram of the length of words from input | 1-13 | 1-13 |
Drawing an histogram of the frequencies of chars from input | 1-14 | 1-14 |
A function to convert Fahrenheit to Celsius | 1-15 | 1-15 |
Computing the longest line from input (using arrays and functions) | 1-16 | 1-16 |
Printing lines from input greater than threshold (using arrays and functions) | 1-17 | 1-17 |
Removing trailing whitespaces/blanks/tabs from input (using arrays and functions) | 1-18 | 1-18 |
Reversing strings (using arrays and functions) | 1-19 | 1-19 |
A program to detab (replace tabs with a constant number of whitespaces) | 1-20 | 1-20 |
A program to entab (replace blanks with the minimum amount of tabs and blanks) | 1-21 | 1-21 |
A program to fold lines at a given limit | 1-22 | 1-22 |
A program to remove all comments from a C source file | 1-23 | 1-23 |
A program to check a C source file for rudimentary syntax errors. | 1-24 | 1-24 |
Chapter 2 | --- | --- |
Checking ranges of basic data types. | 2-1 | 2-1 |
Learning about logical operators. | 2-2 | 2-2 |
Converting hex strings to integer values. | 2-3 | 2-3 |
Squeezing characters from string. | 2-4 | 2-4 |
Finding characters inside string. | 2-5 | 2-5 |
Setting positioned bits in a number (bitwise operators). | 2-6 | 2-6 |
Inverting bits in a number (bitwise operators). | 2-7 | 2-7 |
Rotating bits in a number (bitwise operators). | 2-8 | 2-8 |
Counting 1-bits in a number (bitwise operators). | 2-9 | 2-9 |
Converting ascii letters to lower case (ternary operator). | 2-10 | 2-10 |
Chapter 3 | --- | --- |
Binary search (if-else statements). | 3-1 | 3-1 |
Converting scape sequences (switch statements). | 3-2 | 3-2 |
Expanding string sequences (loops). | 3-3 | 3-3 |
Converting numbers to strings (loops). | 3-4 | 3-4 |
Converting numbers to strings in a different base (loops). | 3-5 | 3-5 |
Converting numbers to strings with padding characters (loops). | 3-6 | 3-6 |
Chapter 4 | --- | --- |
Search for a pattern inside a string (functions). | 4-1 | 4-1 |
Converting strings with scientific notation into doubles (functions). | 4-2 | 4-2 |
Adding modulus operation to a reverse Polish notation calculator (functions). | 4-3 | 4-3 |
Adding new stack operations to a reverse Polish notation calculator (functions). | 4-4 | 4-4 |
Adding new math operations to a reverse Polish notation calculator (functions). | 4-5 | 4-5 |
Handling variables in a reverse Polish notation calculator (functions). | 4-6 | 4-6 |
Input buffering in a reverse Polish notation calculator (functions). | 4-7 | 4-7 |
Changing input buffering in a reverse Polish notation calculator (functions). | 4-8 | 4-8 |
Dealing with EOF in the buffer of a reverse Polish notation calculator (functions). | 4-9 | 4-9 |
Remove buffering in a reverse Polish notation calculator (functions). | 4-10 | 4-10 |
Modifying variable visibility in a reverse Polish notation calculator (header files, external and static variables). | 4-11 | 4-11 |
Converting int to string recursively (recursive functions). | 4-12 | 4-12 |
Reversing a string recursively (recursive functions). | 4-13 | 4-13 |
Swapping values using macros (macro substitutions). | 4-14 | 4-14 |
Chapter 5 | --- | --- |
Reading ints from char stream (pointers). | 5-1 | 5-1 |
Reading floats from char stream (pointers). | 5-2 | 5-2 |
Implementing strcat (pointers). | 5-3 | 5-3 |
Implementing strend (pointers). | 5-4 | 5-4 |
Implementing strncpy, strncat and strncmp (pointers). | 5-5 | 5-5 |
Reimplementing some functions using pointers (pointers). | 5-6 | 5-6 |
Implementing 'sort' unix command (pointers). | 5-7 | 5-7 |
Manipulating dates (pointers). | 5-8 | 5-8 |
Manipulating dates without array indexing (pointers). | 5-9 | 5-9 |
Reverse Polish notation calculator (command line arguments). | 5-10 | 5-10 |
Chapter 6 | --- | --- |
TBI | TBI | |
Chapter 7 | --- | --- |
TBI | TBI | |
Chapter 8 | --- | --- |
TBI | TBI |
-
B. Kernighan and D. Ritchie, "The C Programming Language", 2nd ed., 1988.
-
S. Klabnik and C. Nichols, "The Rust Programming Language", 2018.
Created by ad80.