ProgrammingHardCompetitive coding (CP) · C internals
What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs aligned to their widest member with no packing pragma)? #include <stdio.h> int findIndex(int *a, int n, int target) { for (int i = 0; i < n; i++) if (a[i] == target) return i; return -1; } int main(void) { int arr[6] = {4, 8, 15, 16, 23, 42}; printf("%d %d", findIndex(arr, 6, 16), findIndex(arr, 6, 99)); return 0; }
- A. 3 -1
- B. 4 0
- C. 4 -1
- D. 3 0
Correct answer: A. 3 -1
16 sits at index 3 (0-indexed) in the array; 99 is not present, so the function returns the sentinel -1.
More C Programming questions
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- In C, sizeof(char) is:
- Which storage class makes a local variable keep its value between calls to the function?
- The end of a C string is marked by: