Question discussion

What is the output of the following C program?#include int main() { int a[] = {10, 20, 30, 40, 50}; int *p = a + 2; printf("%d %d %ld", *p,…

920Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int main() {
    int a[] = {10, 20, 30, 40, 50};
    int *p = a + 2;
    printf("%d %d %ld", *p, p[-1], (long)(&a[4] - p));
    return 0;
}
  • A30 20 8
  • B30 30 2
  • C20 10 3
  • D30 20 2
Correct answer: D

Explanation

*p is a[2] = 30, p[-1] is a[1] = 20 and the pointer difference &a[4] − p counts elements, giving 2.

Discussion 0

Nobody has replied yet. Start the discussion!