← Back to TutorialsC Programming Tutorial: Complete Guide with Code Examples
A beginner-friendly, end-to-end C tutorial. Learn syntax, control flow, functions, arrays, pointers, and structs — and run every example in our online C compiler without installing anything.
C is the language that operating systems, databases, compilers, embedded firmware, and most language runtimes are written in. Learning C teaches you how memory, pointers, and system calls actually work — the mental model that makes every other language click.
<div><span>Foundation for operating systems, kernels, and embedded software</span></div><div><span>Direct control over memory and hardware</span></div><div><span>The lingua franca that other languages (Python, Ruby, Node) call into</span></div><div><span>Strong base for learning C++, Rust, Go, and Java</span></div>
<div><div><div><div id="hello">1. Your First C Program</div></div> <div><p>Every C program starts with main(). stdio.h provides printf for writing to standard output.</p> <div><pre>#include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="variables">2. Variables and Data Types</div></div> <div><p>C is statically typed. The core built-in types are int, double, char, and fixed-size arrays for strings.</p> <div><pre>#include <stdio.h> int main(void) { int age = 25; double price = 19.99; char grade = 'A'; char name[] = "Alice"; printf("%s is %d years old\n", name, age); printf("Grade: %c, Price: $%.2f\n", grade, price); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="conditionals">3. Conditional Statements</div></div> <div><p>Use if / else if / else to branch on a condition — the same shape you'll see in almost every C-family language.</p> <div><pre>#include <stdio.h> int main(void) { int score = 85; if (score >= 90) printf("Grade: A\n"); else if (score >= 80) printf("Grade: B\n"); else if (score >= 70) printf("Grade: C\n"); else printf("Grade: F\n"); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="loops">4. Loops</div></div> <div><p>for loops are ideal when you know the iteration count; while loops run as long as a condition holds.</p> <div><pre>#include <stdio.h> int main(void) { for (int i = 1; i <= 5; i++) { printf("Count: %d\n", i); } int n = 0; while (n < 3) { printf("n = %d\n", n); n++; } return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="functions">5. Functions</div></div> <div><p>Functions package reusable logic. Declare the return type, name, and parameters, and return a value with return.</p> <div><pre>#include <stdio.h> int add(int a, int b) { return a + b;
} double average(double x, double y) { return (x + y) / 2.0;
} int main(void) { printf("Sum: %d\n", add(10, 5)); printf("Average: %.2f\n", average(8.0, 12.0)); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="arrays">6. Arrays</div></div> <div><p>C arrays have a fixed size known at compile time. Indexing starts at 0 and the compiler does not check bounds — that's your job.</p> <div><pre>#include <stdio.h> int main(void) { int nums[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) { printf("nums[%d] = %d\n", i, nums[i]); } return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="strings">7. Strings</div></div> <div><p>C strings are just null-terminated char arrays. The <string.h> header provides strlen, strcpy, strcat, and friends.</p> <div><pre>#include <stdio.h>
#include <string.h> int main(void) { char first[20] = "Hello"; char second[] = "World"; strcat(first, ", "); strcat(first, second); printf("%s (length %lu)\n", first, strlen(first)); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="pointers">8. Pointers</div></div> <div><p>A pointer holds a memory address. Use & to take an address and * to dereference. Pointers are the reason C is fast — and the reason bugs bite.</p> <div><pre>#include <stdio.h> int main(void) { int value = 42; int *ptr = &value; printf("Value: %d\n", value); printf("Address: %p\n", (void *)ptr); printf("Deref: %d\n", *ptr); *ptr = 100; printf("Updated: %d\n", value); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="structs">9. Structs</div></div> <div><p>A struct groups related fields under one type. C has no classes, but structs plus functions get you most of the way there.</p> <div><pre>#include <stdio.h>
#include <string.h> struct Person { char name[32]; int age;
}; int main(void) { struct Person p; strcpy(p.name, "Alice"); p.age = 30; printf("%s is %d\n", p.name, p.age); return 0;
}</pre></div> <span><a>Try this example</a></span></div></div></div>
Next Steps in Your C Journey
<div><span>Dynamic memory: malloc, calloc, realloc, and free</span></div><div><span>File I/O with fopen, fread, fwrite, and fclose</span></div><div><span>Header files, multi-file projects, and the preprocessor</span></div><div><span>Function pointers and callbacks</span></div><div><span>Debugging with gdb and memory-checking with valgrind</span></div><div><span>Building with make and understanding compilation vs linking</span></div>
Practice C Online
Ready to write your own C code? Open the online C compiler, paste any example from this tutorial, and run it instantly — no gcc install, no setup.