ProgrammingHardTCS NQT · Wipro
What is printed?
What is printed?
#include <stdio.h>
void inc(int *p, int n) {
while (n--) (*p++)++;
}
int main(void) {
int a[] = {1, 2, 3};
inc(a, 2);
printf("%d", a[0] + a[1] + a[2]);
}- A. 6
- B. 8
- C. 9
- D. Undefined: p and *p are both modified in one expression
Correct answer: B. 8
(*p++)++ increments the int p points at, then advances p; these are two different objects, so it is well defined. With n = 2 the array becomes {2, 3, 3}, sum 8; 9 runs the loop three times, and 6 thinks only the pointer moves.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned short s = 1; s <<= 15; printf("%d", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'a' < 'b' && 'b' < 'c'); }
- char d[4]; char s[] = "abc"; strncpy(d, s, sizeof d - 1); d[sizeof d - 1] = '0'; printf("%s", d); What is printed?
- char w[16]; scanf("%15s", w); Why is the width 15 used?
- int c = 0; const char s = "Queue"; for (int i = 0; s[i]; i++) if (strchr("aeiou", tolower((unsigned char)s[i]))) c++; printf("%d", c)…
- printf("%.s", 2, "hello"); What is printed?