ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3};
int *p = a;
p += 2;
printf("%d", *p - *(p - 2));
}- A. 2
- B. 1
- C. 3
- D. 4
Correct answer: A. 2
p moves to a[2] = 3, and p - 2 points at a[0] = 1, so the difference is 2. Pointer arithmetic is in elements, not bytes.
More C Programming questions
- What is printed? Assume unsigned short is 16 bits and int is 32 bits. c #include <stdio.h> int main(void) { printf("%d", (unsigned…
- How many binary strings of length 4 contain no two consecutive 1s? Compute with this recursive program. c #include <stdio.h> int f(int n)…
- What is printed? c #include <stdio.h> int main(void) { unsigned char a = 100, b = 200; unsigned char s = (unsigned char)(a + b) / 2…
- How many times does the loop body run? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0, j = 0; i < 4 && j < 4; i++, j +=…
- What is printed? c #include <stdio.h> void inc(int p, int n) { while (n--) (p++)++; } int main(void) { int a[] = {1, 2, 3}; inc(a, 2)…
- What is printed? c #include <stdio.h> int main(void) { unsigned short s = 1; s <<= 15; printf("%d", s); }