Question discussion

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

2319Medium 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[5] = {10, 20, 30, 40, 50};
    int *p = &a[4], *q = &a[1];
    printf("%d", (int)(p - q));
    return 0;
}
  • A12
  • B3
  • C1
  • D4
Correct answer: B

Explanation

Subtracting two pointers into the same array gives the number of elements between them, 4 − 1 = 3, and not the difference in bytes.

Discussion 0

Nobody has replied yet. Start the discussion!