ProgrammingModerateProduct-company MCQ pattern
With 4-byte int, what is printed?
With 4-byte int, what is printed?
int a[] = {1, 2, 3};
char *p = (char *)a;
p += 4;
printf("%d", *(int *)p);- A. A garbage value
- B. 3
- C. 1
- D. 2
Correct answer: D. 2
As a char pointer, p += 4 moves 4 bytes, exactly one int, to the start of a[1]. Casting back to int * reads a[1] = 2. Had p stayed an int *, += 4 would have moved 16 bytes.
More C Programming questions
- What is printed? c int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d %d", ((a + 1) + 2), a + 1);
- What is printed? c int x = 3; if (x = 0) printf("A%d", x); else printf("B%d", x);
- What is printed? c int add(int a, int b) { return a + b; } ... int (fp)(int, int) = add; printf("%d", (fp)(2, 3) + fp(4, 5));
- What is printed? c #define MAX(a, b) ((a) > (b) ? (a) : (b)) int x = 3, y = 5; int z = MAX(x++, y++); printf("%d %d %d", x, y, z);
- What is printed? c int i = 0, j = 1, k; k = i++ && j++ || ++j; printf("%d %d %d", i, j, k);
- What is printed (the code compiles)? c int x = (5, 10, 15); int y; y = 5, 10; printf("%d %d", x, y);