ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int globalArr[5] = {1, 2, 3, 4, 5};
void resetArr(void) {
for (int i = 0; i < 5; i++) globalArr[i] = 0;
}
int main(void) {
int sumBefore = 0;
for (int i = 0; i < 5; i++) sumBefore += globalArr[i];
resetArr();
int sumAfter = 0;
for (int i = 0; i < 5; i++) sumAfter += globalArr[i];
printf("%d %d", sumBefore, sumAfter);
}- A. 0 0
- B. 15 0
- C. 0 15
- D. 15 15
Correct answer: B. 15 0
globalArr has static storage duration with file scope, directly accessible from resetArr() without needing a parameter; before the reset it sums to 1+2+3+4+5=15, and after resetArr() zeroes every element, the sum is 0.
More C Programming questions
- What is printed? c int main(void) { char c = 200; printf("%d", c); }
- What is printed? c int main(void) { int a = 10; int p = &a; int pp = &p; printf("%d %d %d", a, p, pp); a = 20; printf(" %d %d %d", a, p…
- What is printed? c enum Color { RED, GREEN, BLUE }; enum Color c = GREEN; printf("%d", c); enum Status { OK = 1, FAIL = 5, PENDING }…
- What is printed? c int main(void) { int x = 1; int p = &x; p += 1; (p)++; ++p; printf("%d", x); }
- What is printed? c int main(void) { int x = 0; int y = (x = 5, x + 1); printf("%d %d", x, y); }
- What is printed? c int main(void) { int arr[3] = {1, 2, 3}; int p = arr; for (int i = 0; i < 3; i++, p++) { printf("%d ", p); } }