Skip to the task
Layout
Back to classic view
READTask 1 of 17 · step 1: How the test worksHow the test works
How the test works in practice/09/

Two parts: questions without a compiler and two programs in the test/2 folder with autotests. They give no points.

Details & hint

The test after topic 12 checks topics 7–12: modules, pointers and bits, strings, structs, files and dynamic memory. This lesson is practice in the same shape.

Two parts

  • Questions. Short code: what it prints, why it doesn't compile, where the bug is. You answer on your own, without a compiler. There are twelve here, each with an answer and an explanation.
  • Programs. Two small tasks in the test/2/ folder: words.c and shopping.c. Autotests check them, like the homework.

How long the real test takes and what you may use will be announced by your lecturer. Practice has no time limit, but try to write both tasks within an hour and a half.

How to check the programs

The task descriptions and the exact output text are in steps 4 and 5 and in the test/2/tests/ files. Run the tests like this:

ctest --preset debug -L test2

In CLion: the All CTest configuration and ▶. Both files already have the #includes and main; while they are empty, the tests fail.

These tasks give no points: GitHub Actions builds them and runs them with ASan and UBSan, but "Points" doesn't count them. They are for checking yourself.

What is checked, same as in the homework

  • the program builds without warnings (-Wall -Wextra -Werror -pedantic -Wvla);
  • the output matches the expected text letter by letter;
  • each part is a function with a contract and an assert, and main only calls them;
  • shopping.c: realloc, free and fclose are there, fscanf isn't, and GitHub CI finds no leak;
  • no TODO is left.

If you get stuck, go back to the topic the question comes from: every answer says which topic it belongs to.

practice 09 text.c Code Diff
UNCHANGED This step does not change this file.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* A string's length without strlen: counts the bytes up to '\0'.
Before: s != NULL. */
int length(const char *s) {
int n = 0;
while (s[n] != '\0') {
n++;
}
return n;
}
/* Turns Latin letters into capitals in the same string.
Before: s != NULL. */
void to_upper(char s[]) {
for (int i = 0; s[i] != '\0'; i++) {
s[i] = (char)toupper((unsigned char)s[i]);
}
}
/* Copies s into t; cuts it if it doesn't fit. Returns how many characters were needed.
Before: t != NULL, size > 0, s != NULL. */
int copy_text(char t[], size_t size, const char *s) {
return snprintf(t, size, "%s", s);
}
int main(void) {
char name[20] = "Ona";
printf("name: \"%s\"\n", name);
printf("sizeof: %zu, strlen: %zu, length: %d\n", sizeof name, strlen(name), length(name));
const char *word = "ąžuolas";
printf("word: strlen %zu\n", strlen(word));
char city[20];
snprintf(city, sizeof city, "%s", "Kaunas");
to_upper(city);
printf("Upper case: %s\n", city);
char color[20] = "žalias";
to_upper(color);
printf("Upper case: %s\n", color);
char full[30];
snprintf(full, sizeof full, "%s %s", name, "Petraite");
printf("Full: %s, strlen %zu\n", full, strlen(full));
char *space = strchr(full, ' ');
if (space != NULL) {
printf("Surname: %s\n", space + 1);
}
char tiny[8];
int needed = copy_text(tiny, sizeof tiny, "Konstantinas");
printf("Fit: \"%s\", needed %d\n", tiny, needed);
const char *a = "Jonas";
const char *b = "Ona";
if (strcmp(a, b) < 0) {
printf("%s comes before %s\n", a, b);
}
if (strcmp(name, b) == 0) {
printf("name and b are equal\n");
}
return 0;
}