ProgrammingHardTCS NQT · Wipro
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int a[] = {5, 6, 7}, *p = a + 1;
printf("%d", p[-1] + *p + p[1]);
}- A. 20
- B. Compile error: a subscript cannot be negative
- C. Undefined: p[-1] reads before the array
- D. 18
Correct answer: D. 18
p[-1] means *(p - 1), which is a[0] because p points at a[1]; that stays inside the array, so it is legal and defined. The sum is 5 + 6 + 7 = 18, while 20 treats p[-1] as the last element, Python-style.
More C Programming questions
- How many tilings does a 2 by 5 board have with 2 by 1 dominoes? Compute with this recursive program. c #include <stdio.h> int f(int n) {…
- What is printed? c #include <stdio.h> #include <stdlib.h> int v[] = {30, 10, 20}; int cmp(const void a, const void b) { int i = (const int…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { unsigned x = (const unsigned )a, y =…
- How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = 60000; uint16t y = x 2…
- How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program. c #include <stdio.h> int ways(int…