You are looking at lesson 9. Your place is still lesson 4, task 1: Style.
Why not scanf("%s") and gets, the three fgets outcomes, strtol with the end pointer and errno, fflush and INPUT_ECHO.
Since topic 2 you've read numbers and lines with read_int, read_double and read_line from the course's input.h. Now you see how they work inside, and in step 5 you write the same kind of thing yourself.
char name[8];
scanf("%s", name);
scanf("%s") doesn't know the array's size. Type "Konstantinas" and it writes 13 bytes into an 8-byte array, wrecking the neighbouring variables. The compiler gives no warning, and the program often doesn't even crash – the bug shows up later and somewhere else. GitHub CI with AddressSanitizer stops such a program with stack-buffer-overflow.
On top of that, scanf("%d") given abc reads nothing and leaves abc in the input: the next scanf refuses it again, and the program loops forever.
gets(t) is even worse: no size at all. It was removed from C in the C11 standard. MinGW still lets it through, but on Linux (CI) with -std=c17 a program with gets doesn't build.
char t[8];
if (fgets(t, sizeof t, stdin) == NULL) {
/* the input ended */
}
fgets reads at most sizeof t - 1 bytes, stops after '\n' and always adds '\0'. What happens with char t[8]:
| typed | t after fgets |
what's left in the input |
|---|---|---|
Ona ↵ |
"Ona\n" |
nothing |
Konstantinas ↵ |
"Konstan" |
tinas ↵ |
| (end of file) | unchanged, returns NULL |
— |
Hence three jobs after every fgets:
NULL – the input ended: there's nothing left to ask.'\n' at the end – the line is too long. The rest must be thrown away up to '\n' (getchar in a loop), or the next fgets reads it.'\n' at the end – cut it off: t[strcspn(t, "\r\n")] = '\0';. \r comes from Windows files read on Linux.char *end;
errno = 0;
long x = strtol(t, &end, 10);
strtol skips spaces at the start, reads digits, and writes into end where it stopped. Through a pointer – topic 8: the function returns two results. They decide whether the text fits:
t |
x |
end points to |
fits? |
|---|---|---|---|
"42" |
42 | '\0' |
yes |
"abc" |
0 | the start of t |
no: not a single digit |
"12abc" |
12 | "abc" |
no: garbage at the end |
"99999999999" |
— | '\0' |
no: doesn't fit in int |
The last row goes two ways. On Windows long is 4 bytes and strtol reports errno == ERANGE. On Linux long is 8 bytes: the number fits in a long but not in an int. So both are checked: errno == ERANGE and x < INT_MIN || x > INT_MAX (<limits.h>).
strtod is the same for real numbers. "7,5" stops at the comma, so it doesn't fit; "1e999" is too big – errno == ERANGE.
printf("> "); without '\n' may stay in a buffer: when the program doesn't run in a terminal (the CLion console, the tests), output is collected and written out later. fflush(stdout); after the prompt shows it at once.
When you type, the console shows what you type. The tests give the input from a file, and it doesn't show in the output. So the course's input.h, when the environment variable INPUT_ECHO equals 1, prints each line it reads itself:
const char *e = getenv("INPUT_ECHO");
if (e != NULL && strcmp(e, "1") == 0) {
printf("%s\n", t);
}
That way a test's expected output looks exactly like the console. Your module does the same – without it the tests can't make sense of your program.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.