- Introduction
- Learning Objectives
- Getting Started
- Topics Covered
- Project Structure
- Build Instructions
- Running Examples
- Exercises
- Troubleshooting
- Learning Path
This tutorial provides a hands-on approach to learning C programming variables. Through practical examples and exercises, you'll master the fundamentals of variable handling in C, from basic declarations to advanced memory management concepts.
- Declaration, definition, and naming rules
- Valid and invalid variable names
- Basic operations and assignments
- Integer types (
int,short,long) - Floating-point types (
float,double) - Character types (
char) - Size and range of each type
- Different initialization methods
- Default values
- Multiple variable initialization
- Stack vs heap memory
- Dynamic memory allocation
- Memory layout and organization
- Local variables
- Global variables
- Static variables
- Register variables
const(constant variables)volatile(volatile variables)register(register storage)
- Implicit type conversion
- Explicit type casting
- Type promotion rules
# 1. Clone or download the project
# 2. Navigate to the project directory
cd c-variables-tutorial
# 3. Make scripts executable
chmod +x compile_and_run.sh
chmod +x test_all.sh
# 4. Build the project
make
# 5. Run all examples
./compile_and_run.sh
# 6. Run exercises
make exercises
# 7. Test everything
./test_all.sh
# 8. Alternative: Use make targets
make runc-variables-tutorial/
├── src/
│ ├── basics/ # Basic variable concepts
│ │ ├── declaration.c
│ │ ├── initialization.c
│ │ └── naming_rules.c
│ ├── datatypes/ # Data type examples
│ │ ├── integers.c
│ │ ├── floats.c
│ │ └── characters.c
│ ├── memory/ # Memory management
│ │ ├── stack_heap.c
│ │ └── allocation.c
│ ├── scope/ # Variable scope
│ │ ├── local_global.c
│ │ └── static_vars.c
│ └── type_conversion/ # Type conversion
│ ├── implicit.c
│ └── explicit.c
├── exercises/ # Practice exercises
│ ├── basic_exercises.c
│ └── advanced_exercises.c
├── include/ # Header files
├── build/ # Compiled output
├── Makefile # Build configuration
├── compile_and_run.sh # Build and run script
├── test_all.sh # Test script
└── README.md # This file
# Build all examples
make
# Build and run all examples
make run
# Build only exercises
make exercises
# Clean build files
make clean
# Remove all build artifacts
make distclean# Compile a single example
gcc -o build/declaration src/basics/declaration.c
# Compile with debugging symbols
gcc -g -o build/debug_example src/basics/declaration.c
# Compile with all warnings
gcc -Wall -Wextra -o build/warnings_example src/basics/declaration.c# Run basic declaration example
./build/declaration
# Run integer data types example
./build/integers
# Run scope example
./build/scope# Method 1: Using the script
./compile_and_run.sh
# Method 2: Using make
make run
# Method 3: Using test script
./test_all.sh# Compile and run basic exercises
gcc -o build/basic_exercises exercises/basic_exercises.c
./build/basic_exercises
# Or use the make target
make exercises- Variable Declaration: Practice declaring variables of different types
- Type Conversion: Convert between different data types
- Scope Practice: Understand local vs global scope
- Memory Allocation: Practice dynamic memory management
- Constants: Work with const and volatile qualifiers
// Exercise 1: Variable Declaration
// Declare variables for: age (integer), price (float), grade (char)
// Initialize them with appropriate values
// Exercise 2: Type Conversion
// Convert a float to int and observe data loss
// Exercise 3: Scope Challenge
// Create local and global variables with same name1. Permission Denied
# Solution: Make scripts executable
chmod +x compile_and_run.sh
chmod +x test_all.sh# Install GCC on Ubuntu/Debian
sudo apt update
sudo apt install gcc
# Install GCC on macOS
xcode-select --install
# or
brew install gcc
# Install GCC on Windows (MinGW)
# Download from: https://www.mingw-w64.org/3. Make Command Not Found
# Install make on Ubuntu/Debian
sudo apt install make
# Install make on macOS (already installed with Xcode)
xcode-select --install
# Install make on Windows (via MinGW or Cygwin)4. Build Directory Issues
# Create build directory if missing
mkdir -p build
# Clean and rebuild
make clean
make5. Program Compiles but Doesn't Run
# Check execution permissions
chmod +x ./build/*
# Run with full path
./build/declaration
# Or navigate to build directory
cd build
./declaration- Start with
basics/declaration.c - Move to
datatypes/integers.c - Practice
exercises/basic_exercises.c
- Study
memory/stack_heap.c - Understand
scope/local_global.c - Practice type conversion examples
- Master
type_conversion/explicit.c - Study all examples in
memory/directory - Complete
exercises/advanced_exercises.c
- Modify and extend examples
- Create your own variable challenges
- Optimize memory usage in examples
# Compile with debug symbols
gcc -g -o build/debug_example src/basics/declaration.c
# Start GDB
gdb build/debug_example
# Common GDB commands
(gdb) break main # Set breakpoint at main
(gdb) run # Run the program
(gdb) print variable # Print variable value
(gdb) next # Execute next line
(gdb) continue # Continue execution
(gdb) quit # Exit GDB// Add debug prints to understand variable values
printf("DEBUG: Variable x = %d\n", x);
printf("DEBUG: Address of x = %p\n", (void*)&x);- "The C Programming Language" by Kernighan and Ritchie
- "C Programming: A Modern Approach" by K. N. King
- "Head First C" by David Griffiths and Dawn Griffiths
Found an issue or want to improve the tutorial?
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This tutorial is available for educational purposes. Feel free to use and modify for learning.
After completing all examples and exercises, you'll have mastered:
- ✓ Variable declaration and initialization
- ✓ Understanding of all C data types
- ✓ Memory management concepts
- ✓ Scope and lifetime rules
- ✓ Type conversion and casting
- ✓ Practical debugging skills
Remember: The best way to learn programming is by writing code. Don't just read the examples—modify them, break them, and fix them!