Code Examples
Short, runnable examples for the basics. Choose a language and open any example in the compiler.
Hello, World!
Every language starts here: print a message to the screen to check that everything works.
Open in compiler#include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }Variables
Variables store values such as text, whole numbers and decimals so you can reuse them.
Open in compiler#include <stdio.h> int main(void) { char name[] = "Ada"; int age = 21; double height = 1.75; printf("%s %d %.2f\n", name, age, height); return 0; }Conditions
if / else lets your program choose a path depending on a value.
Open in compiler#include <stdio.h> int main(void) { int age = 18; if (age >= 18) { printf("Adult\n"); } else if (age >= 13) { printf("Teenager\n"); } else { printf("Child\n"); } return 0; }Loops
Loops repeat a block of code: use for when you know the count and while when you wait for a condition.
Open in compiler#include <stdio.h> int main(void) { for (int i = 1; i <= 5; i++) { printf("%d\n", i); } int n = 3; while (n > 0) { printf("Countdown %d\n", n--); } return 0; }Functions
A function packs code into a reusable block that takes inputs and returns a result.
Open in compiler#include <stdio.h> int add(int a, int b) { return a + b; } int main(void) { printf("%d\n", add(2, 3)); return 0; }Fibonacci sequence
Each number is the sum of the two before it. A classic exercise that combines variables, loops and arithmetic.
Open in compiler#include <stdio.h> int main(void) { int a = 0, b = 1; for (int i = 0; i < 10; i++) { printf("%d ", a); int next = a + b; a = b; b = next; } return 0; }