ProgrammingHardTCS NQT · Infosys
What is printed? Assume short is 2 bytes, int is 4 bytes and both use natural alignment.
What is printed? Assume short is 2 bytes, int is 4 bytes and both use natural alignment.
#include <stdio.h>
struct S { char c; short s; int i; };
int main(void) {
struct S arr[3];
printf("%ld", (long)((char *)&arr[2] - (char *)&arr[0]));
}- A. 8
- B. 16
- C. 12
- D. 10
Correct answer: B. 16
struct S has 1 + 1 + 2 + 4 bytes of members plus padding that makes its size 8, so arr[2] lies 2 * 8 = 16 bytes past arr[0]. Using the unpadded size of 7 would give 14.
More C Programming questions
- How many steps does this Collatz loop take to reach 1 starting from 27? c #include <stdio.h> int main(void) { unsigned n = 27; int steps =…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return (const int )a - (const int )b; }…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { int a = -1; unsigned b = 2; printf("%u"…
- What is printed? c #include <stdio.h> int main(void) { unsigned k = 0; int len = -1; printf("%d", k > len); }
- How many integers in 0..15 are a power of two? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for (unsigned x…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2}, swaps = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 2…