Introduction
C is the language most CSE departments teach first, because it shows how a computer really works: every variable is a piece of memory, and you control all of it.
Most operating systems, and the engines of many other languages, are written in C. Learning it means learning what the machine is really doing, and that knowledge makes every later language easier.
How it works
The smallest useful program
#include <stdio.h>
int main(void) {
printf("Hello, StepVioz!\n");
printf("I am learning C.\n");
return 0;
}Press Visualize and step through with the → key.
#include <stdio.h>brings in the standard input/output library, whereprintflives. Without it, C does not know whatprintfis.int main(void)is where every C program starts. The computer runs the lines inside its braces, top to bottom.printf("...")prints text.\ninside the quotes means "new line".return 0;endsmainand tells the operating system the program finished without problems.
Every statement ends with a semicolon ;. Forgetting one is the most common first error, and StepVioz points at the line where it is missing.
Syntax
#include <stdio.h>
int main(void) {
printf("Hello\n"); // \n starts a new line
printf("%d\n", 2 + 3); // %d is replaced by a whole number
printf("100%%\n"); // %% prints one percent sign
printf("\"quoted\"\n"); // \" prints a double quote
printf("C:\\temp\n"); // \\ prints one backslash
return 0;
}
Example
printf can fill numbers into the text. Each %d is replaced by the next value after the quotes, in order:
#include <stdio.h>
int main(void) {
printf("%d + %d = %d\n", 2, 3, 2 + 3);
printf("7 times 6 is %d\n", 7 * 6);
return 0;
}The calculation 7 * 6 happens first, then its result is printed. Watch the output panel: each line appears at exactly the step that printed it.
Line by line
#include <stdio.h>makesprintfavailable.int main(void)is where the program begins.- The first
printfhas three%dmarkers and three values.2 + 3is worked out before printing. - The second
printffills in7 * 6. return 0;tells the operating system that everything went well.
Expected result
2 + 3 = 5
7 times 6 is 42
In the real world
C runs inside phones, cars, routers and washing machines, because it can work very close to the hardware. Tools such as Python are themselves written largely in C.
Common mistakes
- Forgetting
#include <stdio.h>: C saysprintfis not declared. - Forgetting the
;at the end of a line. - Writing
Printforprint: C is case-sensitive, and the name isprintf. - Forgetting
\n: the next output then continues on the same line.
Best practices
- Compile and run after every small change.
- Fix the first error the compiler reports, then compile again.
- Indent the code inside braces consistently.
Summary
A C program includes the headers it needs, starts in main, runs its statements in order and returns 0. printf prints text, and format codes such as %d place values inside that text.
Practice
Change the first example to print your name and your university on two separate lines.
Quick check
Key takeaways
- Every C program starts in main.
- Each statement ends with a semicolon.
- printf replaces each %d with the next value.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.