Drills
Drills: check yourself
Answer on your own first, and only then open the answer.
1. What does it print?
char t[] = "Hello";
printf("%zu %zu\n", sizeof t, strlen(t));
Answer
6 5
The array's size comes from the literal: 5 letters and '\0'.
2. What ends up in s, and what does snprintf return?
char s[10];
int r = snprintf(s, sizeof s, "%d-%s", 2026, "ruduo");
Answer
s is "2026-rudu" and r is 10. The text needed 10 characters; 9 fit, plus '\0'. r >= sizeof s is the sign it was cut.
3. What does strtol return, and where does end point?
char *end;
long x = strtol(" 15kg", &end, 10);
Answer
x is 15 and end points to "kg". strtol skips the spaces at the start and stops at k. read_int wouldn't accept such a line: the end isn't only spaces.
4. What's wrong here?
char t[5];
fgets(t, 10, stdin);
Answer
fgets believes the array has 10 bytes and may write 9 characters and '\0' into a 5-byte array. Always pass the size as sizeof t, not as a number.
5. What does strlen return?
strlen("Šiauliai")
Answer
9: 8 letters, but Š takes 2 bytes in UTF-8.