Govt. Exams
Entrance 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.
On 32-bit systems, int is typically 4 bytes. 5 elements × 4 bytes = 20 bytes total.
arr+1 points to second element (arr[1]). Dereferencing gives value 20.
Option B shows two valid ways: pointer to string literal or character array. Option A is wrong (single char), C is wrong (no String type in C).
A 2D array with dimensions [4][5] contains 4 × 5 = 20 elements total.
The syntax char *str[5] declares an array of 5 pointers, each pointing to a character (or string). The brackets bind tighter than asterisk.
Option A is valid - 2D arrays can be initialized with all elements in a single brace. Options B and C are invalid as at least one dimension must be specified.
'Hello' has 5 characters plus 1 null terminator (\0), making total 6 bytes.
Array names decay to pointers to their first element in most contexts. 'arr' is equivalent to &arr[0] and cannot be reassigned.
int arr[5]; for(int i=0; i
Loop runs from i=0 to i=4, executing 5 times total. All array elements are initialized to 0.