C Programming for Beginners: Variables, Loops and Functions
C is a compiled language where you tell the computer, step by step, what to store and what to repeat. Three ideas cover most beginner programs: variables hold data, loops repeat work, and functions package that work under a name. Learn those and you can read and write most short C programs, including the kind used in programming contests.
Every example below can be compiled with a standard compiler such as GCC. Type them rather than pasting them; the typos you make teach you to read compiler messages.
The shape of a C program
#include <stdio.h>
int main(void) {
printf("Hello, C\n");
return 0;
}
#include <stdio.h> brings in input and output functions. main is where execution starts. The return 0 tells the operating system that the program finished normally. Save the file as hello.c, compile with gcc hello.c -o hello, and run ./hello.
Each statement ends with a semicolon, and blocks sit inside curly braces. Missing a semicolon is the most common first error, and compilers usually report the line after the real mistake.
Variables and data types
A variable is a named place in memory with a type. The type decides how much memory it uses and what it can hold.
| Type | Holds | Example |
|---|---|---|
int |
whole numbers | int count = 10; |
float, double |
decimals | double price = 2.5; |
char |
one character | char grade = 'A'; |
C does not have a built-in string type. Text is an array of char ending with a zero byte, which is why strings need extra care later on.
Declare a variable before using it, and give it a value. An uninitialised local variable contains whatever was in memory, and printing it gives unpredictable output.
Integer division trips up many beginners: 7 / 2 is 3, because both operands are integers. Write 7 / 2.0 to get 3.5.
Reading input and making decisions
int age;
printf("Enter age: ");
scanf("%d", &age);
if (age >= 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
scanf needs the address of the variable, hence the &. The format code must match the type: %d for int, %f for float, %lf for double when reading. Conditions use == for equality; a single = assigns, and writing it inside an if by mistake is a classic bug.
Loops
Use a for loop when you know how many times to repeat, and while when you repeat until a condition changes.
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
int n = 27;
while (n != 1) {
n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
}
The first loop prints 1 to 5. The second keeps applying a rule until it reaches 1. If the condition can never become false, you have an infinite loop; press Ctrl+C to stop it. Off-by-one errors, such as < instead of <=, are the other everyday loop bug, so test the first and last values by hand.
break leaves a loop early and continue skips to the next round. Use them sparingly, since too many make the flow hard to follow.
Functions
A function takes values in, does a job and optionally sends a value back. Break a program into functions as soon as you notice yourself copying code.
int square(int x) {
return x * x;
}
int main(void) {
printf("%d\n", square(7)); // 49
return 0;
}
The first int is the return type, x is a parameter. C passes arguments by value, so changing x inside the function does not change the caller’s variable. To modify the original you pass its address using a pointer, a topic to take on once the basics feel natural.
If you call a function before defining it, add a prototype near the top, for example int square(int x);.
A small exercise to combine everything
Write a program that reads ten integers, finds the largest and prints it. You need a variable for the maximum, a loop to read the numbers, an if to compare and a function if you want to tidy up the comparison. Then extend it to the average, remembering the integer-division trap.
Arrays: a first step beyond single values
When you need many values of one type, use an array. int scores[5] = {70, 82, 91, 64, 88}; stores five integers, numbered from index 0 to index 4. A loop is the natural way to visit them:
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += scores[i];
}
printf("Average: %.1f\n", sum / 5.0);
C does not check array bounds. Reading scores[5] compiles, but it goes past the end of the array and gives unpredictable results. Always keep the index below the length.
Reading compiler messages
Compilers print the file, the line and a short description. Read the first error only; later ones are often side effects of it. Turn on warnings with gcc -Wall -Wextra, which flags unused variables, missing return values and suspicious comparisons that still compile. Treat a warning as a question to answer, not noise.
When a program runs but prints the wrong thing, add temporary printf lines to show the value of each variable at key points. That habit of tracing values by hand finds most beginner bugs faster than rereading the code.
Frequently asked questions
Is C still worth learning?
Yes. It is used in operating systems, embedded devices and many performance-sensitive programs, and it teaches how memory and compilation work, which carries over to other languages.
Do I need an IDE?
No. A text editor and a compiler are enough. An IDE helps by highlighting errors, but learning the command-line compile step first is useful.
What should I learn after loops and functions?
Arrays, strings, pointers and then structs. Practising with small problems, as in student coding contests, builds speed.
Once these basics feel comfortable, apply them to timed problems like those described in the C programming contest write-up and the wider programming contests.