Question discussion

What will be the output of the C program given below?

359247Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the C program given below?
#include <stdio.h>
int* func()
{
    static int arr[5] = { 1, 2, 3, 4, 5 };
    return arr;
}
int main()
{
    int* ptr = func();
    printf("Array Elements: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", *ptr);
    }
    return 0;
}
  • A1 1 3 4 1
  • B2 3 4 5 0
  • C1 2 3 4 5
  • D1 1 1 1 1
Correct answer: D

Explanation

ptr is never incremented, so *ptr is arr[0] = 1 each time, giving "1 1 1 1 1".

Discussion 0

Nobody has replied yet. Start the discussion!