ProgrammingVery hardTCS NQT · Accenture
What is printed? Assume short is 2 bytes with 2-byte alignment and char is 1 byte.
What is printed? Assume short is 2 bytes with 2-byte alignment and char is 1 byte.
#include <stdio.h>
struct S { short a; char b; };
int main(void) {
struct S arr[3];
printf("%ld", (long)((char *)&arr[2] - (char *)&arr[0]));
}- A. 6
- B. 4
- C. 8
- D. 7
Correct answer: C. 8
The struct holds a 2-byte short and a 1-byte char, padded to a 2-byte multiple, so sizeof(struct S) = 4. Two elements apart is 8 bytes.
More C Programming questions
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t a = 5, b = 10; uint8t d = a…
- Seven people stand in a circle and every third person is removed until one is left. The recursion J(n, k) gives the 1-based survivor…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5, 6}, best = 0; for (int i = 0; i + 3 <= 6; i++) { int s =…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (-17 / 5) 5 + (-17 % 5) == -17); }
- How many leading zero bits does the 16-bit value 0x0123 have? Count them with this program. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return atoi((const char const )a)…