Get to know the skeleton of a C program line by line; print text and numbers with printf, and learn escape sequences and main's return value.
One of the first things you do when you power up a microcontroller board is send a message like "board started" over the serial port. On a computer, the equivalent is printing text to the screen. Printing is the simplest way both to inform the user and to see what is going on inside your program. Let's take apart the program we ran in the previous lesson.
Anatomy of a C program
#include <stdio.h>
int main(void) {
printf("Byte Akademi\n");
printf("Ohm yasasi: V = I * R\n");
return 0;
}
Byte Akademi
Ohm yasasi: V = I * R
#include <stdio.h>: Tells the preprocessor "insert the standard input-output header here". The declaration ofprintflives in this file. Lines starting with#are preprocessor directives, so they don't end with a semicolon.int main(void): The program's entry point. When the operating system starts the program, this is the first function it calls.intsays the function returns an integer;voidsays it takes no parameters.- Everything between
{and}is the function body. Statements run in order from top to bottom. printf(...): Prints the text inside the quotes. Every statement ends with;.return 0;: Tells the operating system that the program finished and everything went fine.
C is case sensitive: if you write Printf or MAIN, the compiler won't recognise them.
Escape sequences: special characters
Some characters have a special meaning inside quotes. To print them you use escape sequences that start with a backslash:
| You write | Meaning |
|---|---|
\n | Move to a new line |
\t | Tab |
\\ | A backslash itself |
\" | A double quote |
%% | A percent sign (printf only) |
#include <stdio.h>
int main(void) {
printf("Olcum raporu\n");
printf("------------\n");
printf("Dosya yolu: C:\\olcum\\pano1.txt\n");
printf("Etiket: \"Ana Pano\"\n");
printf("Verim: %%95\n");
return 0;
}
Olcum raporu
------------
Dosya yolu: C:\olcum\pano1.txt
Etiket: "Ana Pano"
Verim: %95
You'll see why the percent sign is written twice in the next section: for printf, % means "a value goes here".
Printing numbers with printf
The real power of printf is that it can place values inside the text. The format specifiers in the text are matched, in order, with the values after the comma. For now you only need two: %d for integers and %f for decimal numbers.
#include <stdio.h>
int main(void) {
printf("Gerilim: %d V\n", 230);
printf("Akim: %f A\n", 2.5);
printf("Guc: %.1f W\n", 230 * 2.5);
printf("%d + %d = %d\n", 3, 4, 3 + 4);
return 0;
}
Gerilim: 230 V
Akim: 2.500000 A
Guc: 575.0 W
3 + 4 = 7
By default %f prints six digits after the decimal point. %.1f limits it to one. The calculation happens before printf receives it: 230 * 2.5 first becomes 575.0, then gets printed. We'll cover all the format specifiers in the "Input and output" lesson.
Comments and main's return value
#include <stdio.h>
/* Bu program bir hata durumunu
isletim sistemine bildirir. */
int main(void) {
// Sensor baglanamadi varsayalim
printf("Sensor bulunamadi!\n");
return 1;
}
Sensor bulunamadi!
// turns the rest of the line into a comment, and /* ... */ comments out everything in between; the compiler ignores comments. A good comment explains why the code does something, not what it does.
return 1; means "something went wrong". On Linux and macOS you can see this value by typing echo $? right after the program; in the Windows Command Prompt, type echo %errorlevel%. Batch scripts, automated tests and build tools decide whether a program succeeded by looking at this number. On a microcontroller, main usually never ends; it keeps running in an endless loop. We'll see that in the last lesson of the course.
Reading compiler messages
Making mistakes is part of learning; what matters is understanding what the compiler says. In the program below, the semicolon at the end of line 4 is missing:
#include <stdio.h>
int main(void) {
printf("Merhaba\n")
return 0;
}
merhaba.c: In function 'main':
merhaba.c:4:24: error: expected ';' before 'return'
The message has four parts: the file name, the line number (4), the column number (24) and the description. Depending on the GCC version, the offending line and a small arrow may also appear below the message. If several errors are listed, always start with the topmost one; the ones below are often side effects of the first. Fix one, rebuild, then carry on.
Common mistakes
Forgetting the semicolon: GCC gives a message like "expected ';' before 'return'". Note that the mistake is often on the line above the one shown in the message.
- Writing text in single quotes: In C, single quotes are only for a single character (
'A'). Text is always written in double quotes:"Merhaba". - Leaving out
\n: printf does not move to a new line on its own. Two printf calls in a row print their text side by side. - Mixing up specifier and value:
printf("%d", 2.5)prints a meaningless number. If you compile with-Wall, GCC warns you.
Exercise
Write a program that prints a motor's nameplate data. The output should be these three lines: Motor: "M1" (3 faz), Guc: 5.5 kW, Verim: %91. Don't hard-code the power value in the text; print it with %.1f.
Hint: use \" for double quotes and %% for the percent sign.
Solution
#include <stdio.h>
int main(void) {
printf("Motor: \"M1\" (3 faz)\n");
printf("Guc: %.1f kW\n", 5.5);
printf("Verim: %%91\n");
return 0;
}
Motor: "M1" (3 faz)
Guc: 5.5 kW
Verim: %91
You now know the skeleton of a C program and the basics of printf. In the next lesson we'll store values in variables and meet C's data types.
// Spot the bug +10 XP
Exactly one line below is wrong. Click the faulty line.
Line 5 writes the text in single quotes. In C, single quotes are only for one character (like 'D'); text (a string) always goes in double quotes: printf("Durum: normal\n");
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.


