C Programming Tricky Questions

Tricky Questions on Strings and String Functions

What is the null terminator and why is it important in C strings?
The null terminator is '\0' (ASCII value 0) that marks the end of a string. All C string functions rely on it to determine where the string ends. Without it, functions will read beyond allocated memory, causing undefined behavior.
Tricky Point: The null terminator is not the same as '0' (character zero, ASCII 48). It's '\0' (ASCII 0).
What is the difference between strcpy() and strncpy()?
strcpy() copies the entire source string including null terminator, but doesn't check destination buffer size. strncpy() copies at most n characters, but may not add null terminator if source is longer than n, leading to non-terminated strings.
Why is gets() considered dangerous and what should be used instead?
gets() doesn't check buffer boundaries, leading to buffer overflow vulnerabilities. It's removed from C11 standard. Use fgets() instead, which allows specifying maximum characters to read, including space for null terminator.
What does strcmp() return and how should you interpret its return value?
strcmp() returns 0 if strings are equal, negative if first string is lexicographically smaller, positive if first string is larger. Many programmers mistakenly check for 1 or -1, but any non-zero value indicates inequality.
What is the difference between strlen() and sizeof() for strings?
strlen() returns number of characters before null terminator (string length). sizeof() returns total memory allocated for the character array, including null terminator and any unused space.
Can you modify string literals in C?
No, string literals are stored in read-only memory. Attempting to modify them causes undefined behavior (usually segmentation fault). Use character arrays if you need modifiable strings.
Warning: char *str = "hello"; creates a pointer to read-only literal. char str[] = "hello"; creates a modifiable array.
What is the difference between strcat() and strncat()?
strcat() appends entire source string to destination without checking buffer size. strncat() appends at most n characters from source and always adds null terminator, making it safer.
What happens when you pass NULL to string functions?
Passing NULL pointer to most string functions causes undefined behavior (usually crash). Some implementations may check for NULL, but it's not guaranteed by standard.
What is the difference between memcpy() and strcpy()?
memcpy() copies specified number of bytes regardless of content. strcpy() copies until null terminator. memcpy() is faster for known lengths, strcpy() for null-terminated strings.
Why does strtok() is considered tricky to use?
strtok() modifies the original string by replacing delimiters with null terminators. It maintains internal state between calls, making it non-reentrant and unsafe for multithreading.
What is the return value of strlen("")?
strlen("") returns 0 because empty string contains only null terminator. This is important for edge case handling in string processing.
Can strchr() find the null terminator?
Yes, strchr(string, '\0') returns pointer to the null terminator at end of string. This can be useful for appending or finding string end.
What is the difference between strstr() and strchr()?
strstr() finds first occurrence of a substring. strchr() finds first occurrence of a single character. strstr() is for multi-character searches, strchr() for single characters.
What happens when source and destination overlap in strcpy()?
Undefined behavior. When memory regions overlap, strcpy() may corrupt data. Use memmove() instead, which handles overlapping regions correctly.
What is the difference between atoi() and strtol()?
atoi() converts string to int but provides no error checking. strtol() provides extensive error checking, detects overflow, and allows specifying number base. Always prefer strtol() for robustness.
Why should you avoid using sprintf()?
sprintf() doesn't check buffer size, leading to buffer overflows. Use snprintf() instead, which limits number of characters written, preventing overflow.
What is the difference between strdup() and allocating memory manually?
strdup() allocates memory and copies string in one operation. It's convenient but not standard C (POSIX extension). Manual allocation with malloc() + strcpy() is standard but more verbose.
Can string functions work with wide characters (wchar_t)?
No, standard string functions work with char type. For wide characters, use wcslen(), wcscpy(), etc. from wchar.h. These handle multi-byte characters properly.
What is the difference between strspn() and strcspn()?
strspn() returns length of initial segment containing only characters from accept set. strcspn() returns length of initial segment containing no characters from reject set.
What happens when you compare strings from different character sets?
strcmp() compares byte values, so strings from different encodings (ASCII vs EBCDIC) compare incorrectly. Also, locale-specific comparisons may differ (e.g., case-insensitive comparisons).
Why is strncat() safer than strcat() but still has issues?
strncat() limits characters copied but doesn't consider destination buffer size. It assumes destination has enough space for n characters plus existing string plus null terminator.
What is the difference between strpbrk() and strchr()?
strpbrk() finds first occurrence of any character from a set. strchr() finds first occurrence of a specific character. strpbrk() is for searching multiple possible characters.
Can you concatenate a string to itself using strcat()?
No, this causes undefined behavior due to overlapping memory. The source and destination are the same, so as characters are copied, they overwrite the source being read.
What is the time complexity of common string functions?
strlen(): O(n) where n is string length. strcpy()/strcat(): O(n) where n is source length. strcmp(): O(min(m,n)) where m,n are string lengths. strstr(): O(m×n) in worst case.
What is the difference between character array and string in C?
A character array is simply an array of characters. A string is a character array that is null-terminated. All strings are character arrays, but not all character arrays are strings (if not null-terminated).
Tricky Point: This distinction is crucial - string functions expect null-terminated arrays. Non-terminated character arrays used as strings cause buffer overreads.
Note: These tricky questions cover important concepts about C strings and string functions, including null termination, buffer safety, function behaviors, and common pitfalls. Understanding these nuances is crucial for writing secure and correct string-handling code.
Previous Strings & String Functions Next