C Strings & Functions - Tricky MCQ
Tricky Questions on Strings & Functions
What is the fundamental difference between a character array and a string in C?
In C, a string is a character array that ends with null terminator '\0'. Character arrays without null terminator are not considered strings. String functions rely on '\0' to determine end. Strings don't have built-in length tracking.
What does strlen() return for an uninitialized character array?
strlen() counts characters until '\0'. Uninitialized arrays contain garbage values. strlen() continues counting until random '\0' is encountered, returning unpredictable length. This can cause buffer overruns or segmentation faults.
What is the return value of strcmp("Hello", "Hello")?
strcmp() returns 0 when strings are identical, negative if first string is lexicographically smaller, positive if first string is larger. Zero doesn't mean false; in C, 0 is false but for strcmp, 0 means match, non-zero means mismatch.
What is wrong with: char *str = "Hello"; str[0] = 'h';?
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior (often segmentation fault). Should use: const char *str = "Hello"; or char str[] = "Hello"; for modifiable strings.
What does strcpy() return?
strcpy() returns pointer to destination string. This allows chaining: strcpy(strcpy(dest, src1), src2). It doesn't check destination buffer size, leading to buffer overflow if source is larger than destination.
What is the difference between strcmp() and strncmp()?
strncmp() limits comparison to n characters, making it safer against buffer overflows. It stops at null terminator or after n characters. Useful for comparing fixed-length strings or prefixes.
What happens with: char str[5] = "Hello"; printf("%s", str);?
"Hello" needs 6 bytes (5 chars + '\0'). str[5] only holds 5 bytes, so null terminator is omitted. printf() continues printing beyond array until random '\0', causing undefined behavior.
What does strcat() do if destination has no null terminator?
strcat() finds end of destination by searching for '\0'. Without null terminator, it continues past array bounds, causing buffer overflow and undefined behavior. Always ensure destination is properly null-terminated.
What is strchr() return value if character not found?
strchr() returns pointer to first occurrence of character, or NULL if not found. Always check return value before dereferencing. Similar functions: strrchr() (last occurrence), strstr() (substring search).
What is difference between strdup() and strcpy()?
strdup() allocates memory with malloc() and copies string. Not part of C standard but common extension (POSIX). Caller must free() returned memory. strcpy() requires pre-allocated destination buffer.
What is strtok()'s limitation with nested calls?
strtok() uses static internal pointer, making it non-reentrant. Can't tokenize multiple strings in nested loops. Replaces delimiters with '\0', modifying original string. strtok_r() (reentrant version) solves some issues.
What does sprintf() return?
sprintf() returns number of characters written (excluding null terminator). snprintf() is safer as it limits characters written. Neither checks if destination buffer is large enough - can cause buffer overflow.
What is memcpy() vs strcpy() difference?
memcpy() copies specified number of bytes regardless of content. strcpy() stops at null terminator. memcpy() works with any data type, strcpy() only null-terminated strings. memcpy() doesn't add null terminator.
What happens with: char *p = malloc(10); strcpy(p, "Hello World");?
"Hello World" needs 12 bytes (11 chars + '\0'). malloc(10) allocates only 10 bytes. strcpy() causes buffer overflow, writing beyond allocated memory, leading to undefined behavior (corruption, crash).
What is the safe alternative to strcat()?
strncat() limits characters appended. snprintf() formats safely with size limit. Manual implementation with explicit bounds checking is most controlled. Always ensure destination has enough space including null terminator.