mirror of
https://github.com/nicbarker/clay.git
synced 2025-12-23 17:41:06 +00:00
Complete learning guide covering ALL C concepts from beginner to advanced: Main Guide (LEARNING_C_WITH_CLAY.md): - Added Chapters 3-5: Operators, Control Flow, Loops - Enhanced Chapters 6-13: Functions, Pointers, Structs, Arrays, Strings, Type Casting, Storage Classes, Recursion - All chapters include extensive Clay library examples - Progressive difficulty from basics to advanced topics Specialized Tutorial Files (docs/): - 14_bit_manipulation.md: Comprehensive bit operations guide * Bitwise operators, masks, flags * Counting bits, power of 2 operations * Practical applications with Clay examples - 21_standard_library.md: C Standard Library overview * stdio.h, stdlib.h, string.h, math.h * Clay's zero-dependency approach * Custom implementations vs stdlib - 22_file_io.md: Complete File I/O guide * Text and binary file operations * Configuration files for Clay apps * Error handling and best practices - 23_command_line_arguments.md: CLI argument parsing * argc/argv basics * Flag parsing and subcommands * Clay application configuration examples - README_C_TUTORIALS.md: Master index and learning guide * Complete chapter overview * Learning path recommendations * Progress tracking checklist * Prerequisites and setup instructions Features: - 24 comprehensive chapters covering all C concepts - 100+ code examples with detailed explanations - Real-world patterns from Clay library throughout - Practice exercises for each chapter - Modular organization for easy navigation - Zero-dependency programming concepts - Professional C development practices Total content: ~70,000+ words of detailed tutorials
456 lines
12 KiB
Markdown
456 lines
12 KiB
Markdown
# Complete C Programming Guide Using Clay Library
|
|
|
|
## Master C Programming Step-by-Step with Real-World Examples
|
|
|
|
This comprehensive guide teaches you C programming from basics to advanced concepts, using the professional Clay UI layout library as a real-world reference throughout.
|
|
|
|
---
|
|
|
|
## 📚 What You'll Learn
|
|
|
|
- ✅ **All C fundamentals** - Variables, operators, control flow, loops
|
|
- ✅ **Memory management** - Pointers, arrays, dynamic allocation
|
|
- ✅ **Advanced concepts** - Macros, bit manipulation, recursion
|
|
- ✅ **Real-world patterns** - As demonstrated by Clay library
|
|
- ✅ **Professional practices** - Code organization, error handling
|
|
- ✅ **Practical applications** - File I/O, command-line tools
|
|
|
|
---
|
|
|
|
## 📖 Complete Tutorial Index
|
|
|
|
### Part 1: Fundamentals (Main Guide)
|
|
**File: `LEARNING_C_WITH_CLAY.md`**
|
|
|
|
1. **Chapter 1: C Basics** - Your first program, includes, comments
|
|
2. **Chapter 2: Variables and Data Types** - int, float, char, fixed-width types
|
|
3. **Chapter 3: Operators** - Arithmetic, logical, bitwise, ternary
|
|
4. **Chapter 4: Control Flow** - if/else, switch, goto
|
|
5. **Chapter 5: Loops** - while, for, do-while, break, continue
|
|
6. **Chapter 6: Functions** - Declaration, definition, parameters, recursion
|
|
7. **Chapter 7: Pointers** - Memory addresses, dereferencing, pointer arithmetic
|
|
8. **Chapter 8: Structs and Typedef** - Grouping data, nested structs, designated initializers
|
|
9. **Chapter 9: Arrays** - Static arrays, multidimensional, relationship with pointers
|
|
10. **Chapter 10: Strings** - C strings, Clay's string implementation
|
|
11. **Chapter 11: Type Casting** - Implicit/explicit conversion, const, volatile
|
|
12. **Chapter 12: Storage Classes** - auto, static, extern, scope, lifetime
|
|
13. **Chapter 13: Recursion** - Base cases, tail recursion, tree traversal
|
|
|
|
### Part 2: Advanced Topics (Separate Files)
|
|
|
|
#### **Chapter 14: Bit Manipulation** 📄
|
|
**File: `docs/14_bit_manipulation.md`**
|
|
- Bitwise operators (&, |, ^, ~, <<, >>)
|
|
- Bit flags and masks
|
|
- Set/clear/toggle/check bits
|
|
- Counting set bits
|
|
- Power of 2 operations
|
|
- Practical applications in Clay
|
|
- Performance optimizations
|
|
|
|
#### **Chapter 15: Preprocessor and Macros**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 15)
|
|
- #define and #include
|
|
- Conditional compilation (#ifdef, #ifndef)
|
|
- Header guards
|
|
- Function-like macros
|
|
- Stringification and token pasting
|
|
- Predefined macros
|
|
|
|
#### **Chapter 16: Advanced Macros**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 16)
|
|
- Variadic macros
|
|
- X-Macros pattern
|
|
- _Generic for type selection
|
|
- Compound literals
|
|
- For-loop macro trick (Clay's CLAY() macro)
|
|
- Macro debugging
|
|
|
|
#### **Chapter 17: Memory Management**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 17)
|
|
- Stack vs heap
|
|
- malloc, calloc, realloc, free
|
|
- Memory errors and debugging
|
|
- Arena allocators (Clay's approach)
|
|
- Memory pools
|
|
- Alignment
|
|
|
|
#### **Chapter 18: Header Files**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 18)
|
|
- Header vs source files
|
|
- Header guards and #pragma once
|
|
- Single-header library pattern (Clay)
|
|
- Forward declarations
|
|
- Opaque pointers
|
|
- Platform-specific code
|
|
|
|
#### **Chapter 19: Enums and Unions**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 19)
|
|
- Enum basics and typedef
|
|
- Bit flags with enums
|
|
- Unions for memory efficiency
|
|
- Tagged unions
|
|
- Anonymous structs/unions
|
|
|
|
#### **Chapter 20: Function Pointers**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 20)
|
|
- Function pointer basics
|
|
- Typedef for cleaner syntax
|
|
- Callbacks (Clay's measurement callbacks)
|
|
- Function pointer arrays
|
|
- User data pattern
|
|
|
|
#### **Chapter 21: Standard Library Basics** 📄
|
|
**File: `docs/21_standard_library.md`**
|
|
- stdio.h - printf, scanf, file operations
|
|
- stdlib.h - malloc, atoi, rand, exit
|
|
- string.h - strlen, strcpy, strcmp
|
|
- math.h - pow, sqrt, trigonometry
|
|
- ctype.h - character testing
|
|
- time.h - time operations
|
|
- Clay's zero-dependency approach
|
|
|
|
#### **Chapter 22: File I/O** 📄
|
|
**File: `docs/22_file_io.md`**
|
|
- Opening and closing files
|
|
- File modes (read, write, append, binary)
|
|
- fprintf, fscanf, fgets, fputs
|
|
- fread, fwrite for binary data
|
|
- File position (ftell, fseek, rewind)
|
|
- Error handling
|
|
- Configuration files for Clay apps
|
|
- Binary file formats
|
|
|
|
#### **Chapter 23: Command-Line Arguments** 📄
|
|
**File: `docs/23_command_line_arguments.md`**
|
|
- argc and argv basics
|
|
- Parsing arguments
|
|
- Command-line flags
|
|
- getopt for option parsing
|
|
- Subcommands (git-style)
|
|
- Configuration priority
|
|
- Clay application with arguments
|
|
- Interactive vs batch mode
|
|
|
|
#### **Chapter 24: Building Complete Programs**
|
|
**File: `LEARNING_C_WITH_CLAY.md`** (Chapter 24)
|
|
- Complete Clay example
|
|
- Compiling with gcc/clang
|
|
- Makefiles for automation
|
|
- Multi-file projects
|
|
- Common C patterns
|
|
- Best practices
|
|
|
|
---
|
|
|
|
## 🎯 Learning Path
|
|
|
|
### Beginner (Chapters 1-9)
|
|
Start here if you're new to C. Learn the fundamentals:
|
|
- Basic syntax and data types
|
|
- Control structures and loops
|
|
- Functions and pointers
|
|
- Structs and arrays
|
|
|
|
**Estimated time:** 2-3 weeks
|
|
|
|
### Intermediate (Chapters 10-17)
|
|
Build on fundamentals with more advanced topics:
|
|
- String manipulation
|
|
- Type casting and storage classes
|
|
- Recursion and bit manipulation
|
|
- Preprocessor and macros
|
|
- Memory management
|
|
|
|
**Estimated time:** 3-4 weeks
|
|
|
|
### Advanced (Chapters 18-24)
|
|
Master professional C development:
|
|
- Project organization
|
|
- Advanced data structures
|
|
- Function pointers and callbacks
|
|
- File I/O and system interaction
|
|
- Complete application development
|
|
|
|
**Estimated time:** 2-3 weeks
|
|
|
|
---
|
|
|
|
## 🚀 Quick Start
|
|
|
|
1. **Read the main guide** (`LEARNING_C_WITH_CLAY.md`) from Chapter 1
|
|
2. **Follow along** by writing the code examples
|
|
3. **Study Clay's implementation** in `clay.h` for real-world patterns
|
|
4. **Complete practice exercises** at the end of each chapter
|
|
5. **Refer to specialized topics** in the `docs/` folder as needed
|
|
|
|
---
|
|
|
|
## 💡 Why Learn C with Clay?
|
|
|
|
### Clay Library Features
|
|
- **Single-header**: Entire library in one file
|
|
- **Zero dependencies**: No standard library required
|
|
- **High performance**: Microsecond layout calculations
|
|
- **Professional code**: Production-ready patterns
|
|
- **Well-documented**: Clear, readable implementation
|
|
|
|
### What Clay Teaches You
|
|
- ✅ Arena allocators for memory management
|
|
- ✅ Macro DSL design
|
|
- ✅ Single-header library pattern
|
|
- ✅ API design principles
|
|
- ✅ Zero-dependency C programming
|
|
- ✅ Performance optimization techniques
|
|
- ✅ Portable C code practices
|
|
|
|
---
|
|
|
|
## 📁 File Structure
|
|
|
|
```
|
|
clay/
|
|
├── LEARNING_C_WITH_CLAY.md # Main tutorial (Chapters 1-13, 15-20, 24)
|
|
├── clay.h # The Clay library (reference)
|
|
├── docs/
|
|
│ ├── README_C_TUTORIALS.md # This index file
|
|
│ ├── 14_bit_manipulation.md # Chapter 14: Bit operations
|
|
│ ├── 21_standard_library.md # Chapter 21: stdlib overview
|
|
│ ├── 22_file_io.md # Chapter 22: File operations
|
|
│ └── 23_command_line_arguments.md # Chapter 23: argc/argv
|
|
└── examples/ # Clay examples to study
|
|
```
|
|
|
|
---
|
|
|
|
## 🔧 Prerequisites
|
|
|
|
- A C compiler (gcc, clang, or MSVC)
|
|
- Text editor or IDE
|
|
- Basic command-line knowledge
|
|
- Curiosity and patience!
|
|
|
|
### Setting Up
|
|
|
|
**Linux/Mac:**
|
|
```bash
|
|
gcc --version # Check compiler
|
|
```
|
|
|
|
**Windows:**
|
|
- Install MinGW or Visual Studio
|
|
- Or use WSL (Windows Subsystem for Linux)
|
|
|
|
---
|
|
|
|
## 📝 How to Use This Guide
|
|
|
|
### Reading the Main Tutorial
|
|
|
|
```bash
|
|
# View in terminal
|
|
less LEARNING_C_WITH_CLAY.md
|
|
|
|
# Or open in your favorite markdown viewer
|
|
```
|
|
|
|
### Compiling Examples
|
|
|
|
```c
|
|
// example.c
|
|
#include <stdio.h>
|
|
|
|
int main(void) {
|
|
printf("Hello, C!\n");
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
```bash
|
|
# Compile
|
|
gcc -o example example.c
|
|
|
|
# Run
|
|
./example
|
|
```
|
|
|
|
### Studying Clay
|
|
|
|
```c
|
|
// your_program.c
|
|
#define CLAY_IMPLEMENTATION
|
|
#include "clay.h"
|
|
|
|
int main(void) {
|
|
// Your Clay UI code here
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 🎓 Practice Exercises
|
|
|
|
Each chapter includes practice exercises. Try them all!
|
|
|
|
**Example exercises:**
|
|
- Chapter 5: Build a number guessing game
|
|
- Chapter 7: Implement your own string functions
|
|
- Chapter 14: Create a permission system with bit flags
|
|
- Chapter 22: Build a CSV parser
|
|
- Chapter 24: Create a complete Clay application
|
|
|
|
---
|
|
|
|
## 📚 Additional Resources
|
|
|
|
### Clay Resources
|
|
- **Clay Website**: https://nicbarker.com/clay
|
|
- **Clay GitHub**: https://github.com/nicbarker/clay
|
|
- **Examples**: See `clay/examples/` directory
|
|
|
|
### C Programming Resources
|
|
- **C Reference**: https://en.cppreference.com/w/c
|
|
- **Book**: "The C Programming Language" by K&R
|
|
- **Book**: "Modern C" by Jens Gustedt
|
|
- **Online**: C tutorials on YouTube, freeCodeCamp
|
|
|
|
### Tools
|
|
- **Compiler Explorer**: https://godbolt.org
|
|
- **Valgrind**: Memory leak detection
|
|
- **GDB**: Debugging
|
|
- **Clang-format**: Code formatting
|
|
|
|
---
|
|
|
|
## 🤝 Contributing
|
|
|
|
Found an error? Have a suggestion? Contributions welcome!
|
|
|
|
---
|
|
|
|
## ⭐ Features of This Guide
|
|
|
|
- ✅ **Progressive learning** - Start simple, build complexity
|
|
- ✅ **Real-world examples** - Every concept shown in Clay
|
|
- ✅ **Complete coverage** - All C topics from basics to advanced
|
|
- ✅ **Practical focus** - Build real programs, not just theory
|
|
- ✅ **Professional patterns** - Learn industry best practices
|
|
- ✅ **Zero to hero** - From "Hello World" to complex applications
|
|
|
|
---
|
|
|
|
## 🎯 Learning Goals
|
|
|
|
After completing this guide, you will be able to:
|
|
|
|
1. ✅ Write efficient, well-structured C programs
|
|
2. ✅ Understand memory management and pointers
|
|
3. ✅ Use advanced C features (macros, unions, bit manipulation)
|
|
4. ✅ Organize multi-file C projects
|
|
5. ✅ Read and understand professional C code (like Clay)
|
|
6. ✅ Debug and optimize C programs
|
|
7. ✅ Build complete applications with file I/O and CLI
|
|
8. ✅ Apply C knowledge to systems programming
|
|
|
|
---
|
|
|
|
## 📖 Chapter Dependencies
|
|
|
|
```
|
|
Chapter 1-2 (Basics)
|
|
↓
|
|
Chapter 3-5 (Control Flow)
|
|
↓
|
|
Chapter 6 (Functions)
|
|
↓
|
|
Chapter 7 (Pointers) ←─────────┐
|
|
↓ │
|
|
Chapter 8 (Structs) │
|
|
↓ │
|
|
Chapter 9 (Arrays) ────────────┘
|
|
↓
|
|
Chapter 10 (Strings)
|
|
↓
|
|
Chapter 11-12 (Advanced Types)
|
|
↓
|
|
Chapter 13 (Recursion)
|
|
|
|
├─→ Chapter 14 (Bit Manipulation)
|
|
├─→ Chapter 15-16 (Macros)
|
|
├─→ Chapter 17 (Memory)
|
|
├─→ Chapter 18 (Organization)
|
|
├─→ Chapter 19 (Enums/Unions)
|
|
├─→ Chapter 20 (Function Pointers)
|
|
├─→ Chapter 21 (Standard Library)
|
|
├─→ Chapter 22 (File I/O)
|
|
└─→ Chapter 23 (Command Line)
|
|
↓
|
|
Chapter 24 (Complete Programs)
|
|
```
|
|
|
|
---
|
|
|
|
## 🏆 Completion Checklist
|
|
|
|
Track your progress:
|
|
|
|
- [ ] Chapter 1-5: Fundamentals
|
|
- [ ] Chapter 6-9: Functions, Pointers, Structs, Arrays
|
|
- [ ] Chapter 10-13: Strings, Casting, Storage, Recursion
|
|
- [ ] Chapter 14: Bit Manipulation
|
|
- [ ] Chapter 15-16: Preprocessor and Macros
|
|
- [ ] Chapter 17: Memory Management
|
|
- [ ] Chapter 18: Project Organization
|
|
- [ ] Chapter 19: Enums and Unions
|
|
- [ ] Chapter 20: Function Pointers
|
|
- [ ] Chapter 21: Standard Library
|
|
- [ ] Chapter 22: File I/O
|
|
- [ ] Chapter 23: Command-Line Arguments
|
|
- [ ] Chapter 24: Building Complete Programs
|
|
- [ ] Built a complete Clay application
|
|
- [ ] Read and understood clay.h
|
|
|
|
---
|
|
|
|
## 💬 Tips for Success
|
|
|
|
1. **Type every example** - Don't just read, code!
|
|
2. **Experiment** - Modify examples to see what happens
|
|
3. **Read clay.h** - Study professional code
|
|
4. **Complete exercises** - Practice makes perfect
|
|
5. **Debug your errors** - Learn from mistakes
|
|
6. **Build projects** - Apply what you learn
|
|
7. **Ask questions** - Join C programming communities
|
|
8. **Be patient** - C takes time to master
|
|
|
|
---
|
|
|
|
## 🚀 Next Steps After This Guide
|
|
|
|
1. **Build projects** - Create your own applications
|
|
2. **Study data structures** - Implement lists, trees, hash tables
|
|
3. **Learn algorithms** - Sorting, searching, graph algorithms
|
|
4. **Explore systems programming** - OS concepts, networking
|
|
5. **Contribute to open source** - Read and improve real projects
|
|
6. **Specialize** - Embedded systems, game dev, or systems programming
|
|
|
|
---
|
|
|
|
## 📜 License
|
|
|
|
This educational content is provided for learning purposes.
|
|
Clay library: MIT License - see Clay repository for details.
|
|
|
|
---
|
|
|
|
## 🎉 Happy Coding!
|
|
|
|
Welcome to the world of C programming! With dedication and practice, you'll master one of the most powerful and influential programming languages ever created.
|
|
|
|
**Remember:** Every expert was once a beginner. Start with Chapter 1 and work your way through. You've got this! 🚀
|
|
|
|
---
|
|
|
|
*"C is quirky, flawed, and an enormous success." - Dennis Ritchie*
|