This commit is contained in:
deck committed 2026-10-03 20:32:17 +02:00
commit 0c40ea3e9e
83 files changed
+418

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
.prog
*.class
__pycache__/
node_modules/
+62
View File
@@ -0,0 +1,62 @@
{
"course": "c-fundamentals",
"language": "c",
"language_id": 50,
"entrypoint": "main.c",
"version": 1,
"content_hash": "c3a2ddbd275f2184",
"lessons": [
{
"slug": "hello-world",
"tests_hash": "f24145703f18"
},
{
"slug": "variables-and-types",
"tests_hash": "06e6334af07b"
},
{
"slug": "numbers-and-printf",
"tests_hash": "790ecc50aa50"
},
{
"slug": "arrays",
"tests_hash": "ef2fe36012bc"
},
{
"slug": "strings",
"tests_hash": "d38d1bcf0ee0"
},
{
"slug": "conditionals",
"tests_hash": "2f96f655f1c0"
},
{
"slug": "loops",
"tests_hash": "e016ae68115f"
},
{
"slug": "functions",
"tests_hash": "d853d5b83122"
},
{
"slug": "pointers-basics",
"tests_hash": "6091501c7abf"
},
{
"slug": "structs",
"tests_hash": "21f1c690513f"
},
{
"slug": "malloc-and-free",
"tests_hash": "3f9bf1bd5581"
},
{
"slug": "file-io",
"tests_hash": "63e1c6d72ca6"
},
{
"slug": "preprocessor",
"tests_hash": "71bf76be2d68"
}
]
}
+61
View File
@@ -0,0 +1,61 @@
# C Fundamentals
[![shipthatcode — C Fundamentals](https://api.shipthatcode.com/cert/c703339228681dd0cb682d35a4223608.svg)](https://shipthatcode.com/courses/c-fundamentals)
My working repo for [C Fundamentals](https://shipthatcode.com/courses/c-fundamentals) on [shipthatcode.com](https://shipthatcode.com) — built lesson by lesson in my own editor.
## What you need
- **git** and a terminal (macOS/Linux: the built-in one; Windows: see below)
- a C compiler — `gcc` or `clang` (macOS: `xcode-select --install`, Ubuntu: `sudo apt install build-essential`, Windows: MinGW via Git Bash or WSL). Single-file, standard library only — no OpenSSL or other external libraries, same as the grader
- any editor you like — VS Code, Vim, JetBrains, anything
### On Windows
Use **WSL** if you can: run `wsl --install` in an admin PowerShell once, then do everything (git, editing, `./run_tests.sh`) inside the Ubuntu terminal — it behaves exactly like the grader.
[Git Bash](https://git-scm.com/downloads) (installs with git) also runs `./run_tests.sh`. One caveat: native Windows compilers and runtimes write Windows line endings (`\r\n`), so on byte-exact tests you can see local FAILs where the diff looks identical — your logic is fine, the invisible line endings differ. If that happens, trust **Check my solution** on the lesson page (graded on Linux), or switch to WSL.
## Getting started (one-time setup)
1. Unzip this download and open the folder in your editor.
2. Create a new **empty, public** repo at [github.com/new](https://github.com/new) — leave "Add a README" and ".gitignore" **unchecked** (this folder already has both).
3. In your terminal, inside the unzipped folder, push it to GitHub:
```sh
git init
git add .
git commit -m "start C Fundamentals"
git branch -M main
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO.git
git push -u origin main
```
4. Paste your repo link on the [course page](https://shipthatcode.com/courses/c-fundamentals) ("Work in your own editor" → **Link repo**). Done — you never do this again.
## The lesson loop
1. Read the lesson on shipthatcode, write your code in `main.c` here.
2. Test locally against **the lesson you're on**: `./run_tests.sh 01`, `./run_tests.sh 02`, and so on. On Windows, run this inside Git Bash. (A bare `./run_tests.sh` runs every lesson's tests, which is only useful on courses where one program answers all of them — see below.)
3. When it passes: `git add -A && git commit -m "lesson 01" && git push`
4. Hit **Check my solution** on the lesson page — shipthatcode pulls this repo and grades it against the full suite, including hidden tests.
## How this repo is laid out
- `main.c` — the program you're grading right now.
- `tests/` — the public test cases per lesson (`tests/01-…/1.in` → expected `1.out`).
- `run_tests.sh` — the local runner. `.shipthatcode.json` tells the grader what this repo is; don't delete either.
### One lesson at a time
Each lesson states its own input and output format, and **most lessons are a separate exercise rather than a bigger version of the last one**. Two lessons can be handed the same input line and correctly want different output — a tokenizer prints `[ls] [-la]`, while the next lesson, which receives already-tokenized input, prints `ls -la`. No single program can satisfy both, and it isn't supposed to.
So treat `main.c` as the file for the lesson you're grading: when you move on, change what it does. Nothing is lost — every earlier lesson is in your git history (`git log`, `git show`), and shipthatcode remembers each lesson you passed, so a lesson stays completed even after you replace the code that passed it. If you'd rather keep the code visible, copy it aside first (`cp main.c solutions/01-<lesson>.c`) — extra files are ignored by the grader.
When a lesson genuinely does build on the previous one, its own text says so and its tests will pass with the earlier behaviour still in place.
## If the course gets updated
Courses improve over time — exercises get added, tests get fixed. If the [course page](https://shipthatcode.com/courses/c-fundamentals) says your starter is out of date: download a fresh zip, **delete this repo's `tests/` folder entirely**, copy in the fresh `tests/` and `.shipthatcode.json`, and keep your own code exactly as it is. (Don't merge test folders — lesson numbering can shift between versions.) Lessons you've already completed stay completed either way.
<sub>Repo topic suggestion: `shipthatcode` · Starter generated by [shipthatcode.com](https://shipthatcode.com)</sub>
+17
View File
@@ -0,0 +1,17 @@
#include <stdio.h>
#include <string.h>
int main(void) {
/* Already done for you: read one line of text into name and drop the
Enter key from its end. The Strings lesson explains how this works. */
char name[256] = "";
if (fgets(name, sizeof name, stdin) == NULL) {
name[0] = '\0';
}
name[strcspn(name, "\r\n")] = '\0';
/* TODO: print the two lines described in the exercise.
Use a %s placeholder for name; do not type any name yourself. */
return 0;
}
Executable
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env bash
# Local test runner for "C Fundamentals" (shipthatcode.com).
# Usage:
# ./run_tests.sh run every lesson's tests
# ./run_tests.sh 03 run only tests/03-*'s tests
set -u
LANG_SLUG="c"
ENTRY="main.c"
FILTER="${1:-}"
# Test dirs are zero-padded (01-, 02-, …) — accept "./run_tests.sh 3" too.
case "$FILTER" in [1-9]) FILTER="0$FILTER" ;; esac
# Languages the grader supports but this local runner can't fake reliably
# yet. The website's Check is the official grade either way.
case "$LANG_SLUG" in
sql|prolog)
echo "The local runner doesn't support $LANG_SLUG yet."
echo "Write your code here, push, and grade with 'Check my solution' on the lesson page."
exit 2 ;;
esac
# Prefer gcc/g++ (Linux, MinGW on Windows) but fall back to cc/c++ (macOS).
CC_BIN=cc; command -v gcc >/dev/null 2>&1 && CC_BIN=gcc
CXX_BIN=c++; command -v g++ >/dev/null 2>&1 && CXX_BIN=g++
# Windows installs often have "python", not "python3".
PY_BIN=python3; command -v python3 >/dev/null 2>&1 || PY_BIN=python
compile() {
case "$LANG_SLUG" in
c) "$CC_BIN" -O2 -o .prog "$ENTRY" ;;
cpp) "$CXX_BIN" -std=c++17 -O2 -o .prog "$ENTRY" ;;
rust) rustc -O -o .prog "$ENTRY" ;;
java) javac "$ENTRY" ;;
assembly) nasm -felf64 "$ENTRY" -o .prog.o && ld .prog.o -o .prog ;;
basic) fbc -x .prog "$ENTRY" ;;
cobol) cobc -xF -o .prog "$ENTRY" ;;
csharp) mcs -out:.prog.exe "$ENTRY" ;;
d) dmd -of=.prog "$ENTRY" ;;
fortran) gfortran -O2 -o .prog "$ENTRY" ;;
kotlin) kotlinc "$ENTRY" -include-runtime -d .prog.jar ;;
objc)
if [ "$(uname)" = "Darwin" ]; then
"$CC_BIN" -framework Foundation -o .prog "$ENTRY"
else
"$CC_BIN" -o .prog "$ENTRY"
fi ;;
pascal) fpc -o.prog "$ENTRY" > /dev/null ;;
vbnet) vbnc -out:.prog.exe "$ENTRY" ;;
*) return 0 ;;
esac
}
run_one() { # $1 = input file
case "$LANG_SLUG" in
c|cpp|rust|assembly|basic|cobol|d|fortran|pascal) ./.prog < "$1" ;;
csharp|vbnet) mono .prog.exe < "$1" ;;
kotlin) java -jar .prog.jar < "$1" ;;
python) "$PY_BIN" "$ENTRY" < "$1" ;;
javascript) node "$ENTRY" < "$1" ;;
typescript) npx -y tsx "$ENTRY" < "$1" ;;
go) go run "$ENTRY" < "$1" ;;
java) java "${ENTRY%.java}" < "$1" ;;
bash) bash "$ENTRY" < "$1" ;;
clojure) clojure -M "$ENTRY" < "$1" ;;
elixir) elixir "$ENTRY" < "$1" ;;
erlang) escript "$ENTRY" < "$1" ;;
fsharp) dotnet fsi "$ENTRY" < "$1" ;;
groovy) groovy "$ENTRY" < "$1" ;;
haskell) runghc "$ENTRY" < "$1" ;;
lisp) sbcl --script "$ENTRY" < "$1" ;;
lua) lua "$ENTRY" < "$1" ;;
objc) ./.prog < "$1" ;;
ocaml) ocaml "$ENTRY" < "$1" ;;
octave) octave -qf "$ENTRY" < "$1" ;;
perl) perl "$ENTRY" < "$1" ;;
php) php "$ENTRY" < "$1" ;;
r) Rscript "$ENTRY" < "$1" ;;
ruby) ruby "$ENTRY" < "$1" ;;
scala) scala "$ENTRY" < "$1" ;;
swift) swift "$ENTRY" < "$1" ;;
*) echo "unsupported language: $LANG_SLUG" >&2; exit 1 ;;
esac
}
if ! compile; then
echo "COMPILE FAILED"
exit 1
fi
pass=0; fail=0
for dir in tests/${FILTER}*/; do
[ -d "$dir" ] || continue
for input in "$dir"*.in; do
[ -f "$input" ] || continue
expected="${input%.in}.out"
actual="$(run_one "$input")"
if [ "$actual" = "$(cat "$expected")" ]; then
pass=$((pass+1)); echo "PASS $input"
else
fail=$((fail+1)); echo "FAIL $input"
echo " expected: $(head -c 200 "$expected")"
echo " got: $(printf '%s' "$actual" | head -c 200)"
fi
done
done
echo
echo "$pass passed, $fail failed"
[ "$fail" -eq 0 ]
+1
View File
@@ -0,0 +1 @@
Ada
+2
View File
@@ -0,0 +1,2 @@
Hello, Ada!
Welcome to C.
+1
View File
@@ -0,0 +1 @@
Linus
+2
View File
@@ -0,0 +1,2 @@
Hello, Linus!
Welcome to C.
+1
View File
@@ -0,0 +1 @@
Grace Hopper
+2
View File
@@ -0,0 +1,2 @@
Hello, Grace Hopper!
Welcome to C.
+2
View File
@@ -0,0 +1,2 @@
7.5
2.25
+2
View File
@@ -0,0 +1,2 @@
9.750000
9
+2
View File
@@ -0,0 +1,2 @@
10
5
+2
View File
@@ -0,0 +1,2 @@
15.000000
15
+1
View File
@@ -0,0 +1 @@
4.5 4.5
+2
View File
@@ -0,0 +1,2 @@
9.000000
9
+2
View File
@@ -0,0 +1,2 @@
5
3
+2
View File
@@ -0,0 +1,2 @@
15
1.67
+2
View File
@@ -0,0 +1,2 @@
10
10
+2
View File
@@ -0,0 +1,2 @@
100
1.00
+2
View File
@@ -0,0 +1,2 @@
7
2
+2
View File
@@ -0,0 +1,2 @@
14
3.50
+5
View File
@@ -0,0 +1,5 @@
3
8
2
9
1
+1
View File
@@ -0,0 +1 @@
9
+5
View File
@@ -0,0 +1,5 @@
5
5
5
5
5
+1
View File
@@ -0,0 +1 @@
5
+5
View File
@@ -0,0 +1,5 @@
4
1
7
3
5
+1
View File
@@ -0,0 +1 @@
7
+1
View File
@@ -0,0 +1 @@
hello
+1
View File
@@ -0,0 +1 @@
5
+1
View File
@@ -0,0 +1 @@
hello world
+1
View File
@@ -0,0 +1 @@
11
+1
View File
@@ -0,0 +1 @@
a
+1
View File
@@ -0,0 +1 @@
1
+1
View File
@@ -0,0 +1 @@
9
+1
View File
@@ -0,0 +1 @@
Fizz
+1
View File
@@ -0,0 +1 @@
10
+1
View File
@@ -0,0 +1 @@
Buzz
+1
View File
@@ -0,0 +1 @@
15
+1
View File
@@ -0,0 +1 @@
FizzBuzz
+1
View File
@@ -0,0 +1 @@
7
+1
View File
@@ -0,0 +1 @@
7
+1
View File
@@ -0,0 +1 @@
7
+1
View File
@@ -0,0 +1 @@
16
+1
View File
@@ -0,0 +1 @@
5
+1
View File
@@ -0,0 +1 @@
9
+1
View File
@@ -0,0 +1 @@
10
+1
View File
@@ -0,0 +1 @@
25
+3
View File
@@ -0,0 +1,3 @@
0 10
5
-3 0 7 10 15
+5
View File
@@ -0,0 +1,5 @@
0
0
7
10
10
+3
View File
@@ -0,0 +1,3 @@
1 6
3
4 9 -2
+3
View File
@@ -0,0 +1,3 @@
4
6
1
+3
View File
@@ -0,0 +1,3 @@
-10 -3
4
-20 -5 0 -3
+4
View File
@@ -0,0 +1,4 @@
-10
-5
-3
-3
+1
View File
@@ -0,0 +1 @@
3 7
+1
View File
@@ -0,0 +1 @@
7 3
+1
View File
@@ -0,0 +1 @@
10 -4
+1
View File
@@ -0,0 +1 @@
-4 10
+1
View File
@@ -0,0 +1 @@
5 5
+1
View File
@@ -0,0 +1 @@
5 5
+4
View File
@@ -0,0 +1,4 @@
0
0
3
4
+1
View File
@@ -0,0 +1 @@
25
+4
View File
@@ -0,0 +1,4 @@
1
1
1
1
+1
View File
@@ -0,0 +1 @@
0
+4
View File
@@ -0,0 +1,4 @@
2
5
7
1
+1
View File
@@ -0,0 +1 @@
41
+4
View File
@@ -0,0 +1,4 @@
3
1
2
3
+2
View File
@@ -0,0 +1,2 @@
3 2 1
6
+2
View File
@@ -0,0 +1,2 @@
1
42
+2
View File
@@ -0,0 +1,2 @@
42
42
+6
View File
@@ -0,0 +1,6 @@
5
7
-2
15
0
4
+2
View File
@@ -0,0 +1,2 @@
4 0 15 -2 7
24
+5
View File
@@ -0,0 +1,5 @@
2
woke up
coffee
1
read a book
+4
View File
@@ -0,0 +1,4 @@
1:woke up
2:coffee
3:read a book
lines: 3
+5
View File
@@ -0,0 +1,5 @@
1
first entry
2
second entry
third entry
+4
View File
@@ -0,0 +1,4 @@
1:first entry
2:second entry
3:third entry
lines: 3
+4
View File
@@ -0,0 +1,4 @@
0
2
late start
still tired
+3
View File
@@ -0,0 +1,3 @@
1:late start
2:still tired
lines: 2
+1
View File
@@ -0,0 +1 @@
3 1
+4
View File
@@ -0,0 +1,4 @@
3 cubed is 27
4 cubed is 64
1 cubed mod 7 is 1
larger plus one is 4
+1
View File
@@ -0,0 +1 @@
2 5
+4
View File
@@ -0,0 +1,4 @@
2 cubed is 8
7 cubed is 343
5 cubed mod 7 is 6
larger plus one is 6