How many bytes does char s[] = "Hello"; occupy?
The compiler sizes the array from the literal, including the terminating '\0': 5 letters + 1 = 6 bytes. strlen(s) would be 5.
Strings · Exercise 1 of 4
Twelve multiple-choice questions on how C strings work: the terminator, literals vs arrays, safe input, string.h, ctype, UTF-8 and C23. Pick an option to check your answer instantly.
How many bytes does char s[] = "Hello"; occupy?
The compiler sizes the array from the literal, including the terminating '\0': 5 letters + 1 = 6 bytes. strlen(s) would be 5.
What happens with char s[5] = "Hello"; in C?
C (unlike C++) allows the literal to fill the array exactly, dropping the terminator. s is then just 5 chars, and passing it to strlen() or printf("%s") reads past the end.
Why was gets() removed from the C standard in C11?
gets(buf) is never told how big buf is, so a long line overflows it – a classic security hole. Use fgets(buf, sizeof buf, stdin).
Which statement about char *p = "hi"; and char a[] = "hi"; is true?
a is a new 3-byte array initialised from the literal. p points at the literal itself, which compilers usually place in read-only memory. Declare it const char *p. Also sizeof p is 8, sizeof a is 3.
What does strcmp(a, b) return when a comes before b?
The standard only promises the sign: negative, zero or positive. Many libraries return the difference of the first differing bytes, so test < 0, never == -1.
After char d[4]; strncpy(d, "LearnFrenzy", 4);, what is true?
strncpy() copies at most n characters and adds a terminator only if the source is shorter than n. Here it copies "Lear" and stops, so d is not a string until you set d[3] = '\0'.
Which function writes formatted text into a buffer without ever overflowing it?
snprintf(buf, size, fmt, …) writes at most size bytes including the terminator, and returns the length it needed so truncation can be detected. sprintf() has no size limit.
Which statement about strtok() is true?
strtok() replaces each delimiter it stops at with '\0' and remembers where it stopped. So the input must be writable, empty fields are skipped, and it is not safe to use for two strings at once.
Why is toupper((unsigned char) c) recommended over toupper(c) for a char c?
The ctype functions take an int that must be EOF or a value of unsigned char. Where char is signed, a byte such as 0xE9 (é in Latin-1) becomes −23 – undefined behaviour, and a real crash on some libraries.
Source files are UTF-8. What does strlen("héllo") return?
strlen() counts bytes up to '\0'. In UTF-8, é is two bytes (0xC3 0xA9), so the 5 visible characters take 6 bytes. Counting characters needs UTF-8 decoding.
Timed online tests with instant scores and explanations.