Entrance Exams
Govt. Exams
char str[] = "HELLO";
int len = 0;
while(str[len] != '\0') {
len++;
}
printf("%d", len);
What will be the output?
The string "HELLO" has 5 characters. The while loop counts characters until it encounters the null terminator '\0'. The output will be 5. The null terminator is not counted in the length.
String functions expect null terminator. This array has no '\0', so it's a character array but NOT a proper string for str* functions.
strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.
First dimension can be omitted, but second must be specified: int arr[][4]. Option C (int **arr) is not equivalent - it's pointer to pointer.
strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.
%s format specifier stops reading at whitespace. To read entire line including spaces, use fgets() or %[^\n].
Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.
On 32-bit systems, int is typically 4 bytes. 5 elements × 4 bytes = 20 bytes total.
gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.
arr+1 points to second element (arr[1]). Dereferencing gives value 20.