ProgrammingHardTCS NQT · Infosys
char buf[10]; char *a = buf + 2, *b = buf + 9; What does printf("%td", b - a) print?
- A. 7
- B. 8
- C. 11
- D. 10
Correct answer: A. 7
Pointer subtraction gives the number of elements between the two positions, 9 - 2 = 7, with type ptrdiff_t (hence %td). 8 counts both endpoints inclusively, 11 adds the offsets 2 + 9, and 10 confuses the result with the array size.
More C Programming questions
- struct P { int x, y; } arr[4]; struct P p = arr; p += 2; What is p - arr?
- #define MIN(a,b) ((a)<(b)?(a):(b)). What does printf("%d", MIN(MIN(3,1),2)) print?
- int x = 5; printf("%d", x > 3 ? x - 3 : x + 3); What is printed?
- What does printf("%d", (4, 5) 2) print?
- int x = 0, y = 1; What does printf("%d", !x == y) print?
- printf("%d %d", 1 < 2 < 3, 3 > 2 > 1); What is printed?