Structs & Unions Questions with Answers

28 practice questions on Structs & Unions (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 28 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <stdio.h>

struct P { int x, y; };

int main(void) {
    struct P a = {3, 4}, *p = &a;
    p->x += 2;
    (*p).y *= 3;
    printf("%d %d\n", a.x, a.y);
    return 0;
}
  1. 3 12
  2. 5 4
  3. 5 12
  4. 3 4

Answer: C. 5 12

Explanation

p->x and (*p).y both access a through the pointer, so x becomes 5 and y becomes 12.

Step-by-step solution

  1. P->x is same as (*p).x, so a.x = 3 + 2 = 5.
  2. (*p).y is same as p->y, so a.y = 4 * 3 = 12.
  3. Printf prints a.x and a.y: 5 12.

Why the other options are wrong

  • A. The x value stays 3 is wrong; p->x += 2 changes a.x to 5 through the pointer.
  • B. The y value stays 4 is wrong; (*p).y *= 3 changes a.y to 12.
  • D. Neither changed is wrong; both updates go through p to the same struct a.

Tip: Use p-> for direct member access via pointer.

Question 2Easy

What is the output?

#include <stdio.h>

struct S { int a; char b; float c; };

int main(void) {
    struct S s = {1, 'x'};
    printf("%d %c %.1f\n", s.a, s.b, s.c);
    return 0;
}
  1. Compilation error
  2. 1 x 1.0
  3. 1 x 0.0
  4. 1 120 0.0

Answer: C. 1 x 0.0

Explanation

When an initialiser list is shorter than the struct, the remaining members are zero-initialised, so c is 0.0.

Step-by-step solution

  1. S is initialised with 1 and 'x'; the initialiser list is shorter than the struct.
  2. The member c is not given a value, so it is zero-initialised to 0.0.
  3. Printf shows 1, x and 0.0, so the output is 1 x 0.0. The answer is option C.

Why the other options are wrong

  • A. The code is valid C; a shorter initialiser list is allowed.
  • B. C is not set to 1.0; it is zero-initialised.
  • D. B is the character 'x', so %c prints x, not the number 120.

Tip: Members left out of a brace initialiser are set to zero.

Question 3Easy

What does typedef struct { int x, y; } Point; allow you to do?

  1. Make x and y private to Point
  2. Use Point as a template for any type
  3. Declare variables as Point p; without writing the struct keyword
  4. Create a Point object on the heap automatically

Answer: C. Declare variables as Point p; without writing the struct keyword

Explanation

typedef creates an alias for the (anonymous) struct type, so Point can be used as a type name directly.

Step-by-step solution

  1. Typedef gives a new name to a type.
  2. Here the anonymous struct gets the name Point.
  3. So Point p; declares a variable without writing the struct keyword. The answer is option C.

Why the other options are wrong

  • A. Typedef has no access control; C struct members are not private to a name.
  • B. Typedef names one type; it is not a template and C has no templates.
  • D. Typedef creates no objects; memory is used only when a Point is declared or allocated.

Tip: Typedef struct { ... } Name; lets you write Name v; instead of struct-tag v.

Question 4Easy

What is the output?

#include <stdio.h>

struct A { int v; };
struct B { struct A *pa; };
struct C { struct B b; };

int main(void) {
    struct A a = {7};
    struct C c = {{&a}};
    struct C *pc = &c;
    pc->b.pa->v *= 2;
    printf("%d\n", a.v);
    return 0;
}
  1. 14
  2. 0
  3. 7
  4. Compilation error

Answer: A. 14

Explanation

pc->b is a struct (use .), and b.pa is a pointer (use ->), so the chain reaches a.v and doubles it.

Step-by-step solution

  1. Pc->b accesses the struct B inside struct C, which contains pa.
  2. Pa is a pointer to struct A, so pc->b.pa points to a.
  3. Pc->b.pa->v accesses the v member of struct A, which is 7.
  4. Pc->b.pa->v *= 2 changes the value of a.v to 14.
  5. Printf prints the final value of a.v, which is 14.

Why the other options are wrong

  • B. The value is changed through the pointer, so a.v is 14 after the doubling, not 0.
  • C. Seven is the starting value, but *= 2 doubles it through pa, so the printed value is 14.
  • D. The code compiles; pc->b.pa->v is valid access through nested members and pointers.

Tip: Use -> for pointers and. for structs when accessing nested members.

Question 5Medium

What is the output?

#include <stdio.h>

struct P { int x, y; };

struct P mid(struct P a, struct P b) {
    return (struct P){(a.x + b.x) / 2, (a.y + b.y) / 2};
}

int main(void) {
    struct P m = mid((struct P){2, 9}, (struct P){6, 4});
    printf("%d %d\n", m.x, m.y);
    return 0;
}
  1. 4 7
  2. 4 6.5
  3. 8 13
  4. 4 6

Answer: D. 4 6

Explanation

Compound literals create the struct arguments.

(2 + 6) / 2 = 4 and (9 + 4) / 2
= 13 / 2
= 6 with integer division.

Step-by-step solution

  1. Both compound literals are passed in: a is {2, 9} and b is {6, 4}.
  2. X is (2 + 6) ÷ 2 = 8 ÷ 2 = 4.
  3. Y is (9 + 4) ÷ 2 = 13 ÷ 2, and int division drops the fraction, giving 6.
  4. Printf prints 4 and 6, so the correct answer is option D, 4 6.

Why the other options are wrong

  • A. Y would be 7, but 9 + 4 = 13 and 13 ÷ 2 is 6 with int division.
  • B. 6.5 needs floating-point division; both fields are int, so the result is an int.
  • C. 8 13 are the sums before dividing by 2.

Tip: In C, int ÷ int drops the fraction, so 13 ÷ 2 gives 6, never 6.5.

Question 6Medium

Assume there is no padding between the two int members (true on common compilers). What is the output?

#include <stdio.h>

union V {
    struct { int x, y; } p;
    int arr[2];
};

int main(void) {
    union V v;
    v.arr[0] = 3;
    v.arr[1] = 4;
    printf("%d %d\n", v.p.x, v.p.y);
    return 0;
}
  1. 3 4
  2. 4 3
  3. 3 3
  4. 0 0

Answer: A. 3 4

Explanation

All union members start at the same address, so p.x and p.y overlay arr[0] and arr[1].

Step-by-step solution

  1. Union V shares memory between struct p and int arr[2].
  2. arr[0] = 3 writes to address of p.x.
  3. arr[1] = 4 writes to address of p.y.
  4. Printf reads p.x and p.y which are now 3 and 4.
  5. Final output is 3 4.

Why the other options are wrong

  • B. The order is reversed; arr[0] is 3 and that is stored in p.x, so the first value is 3.
  • C. Y is 4, not 3; arr[1] is stored in p.y, so the second value is 4.
  • D. The union shares storage, so the values written to arr are read back, giving 3 and 4, not 0.

Question 7Medium

Why can sizeof a struct be larger than the sum of the sizes of its members?

  1. The compiler inserts padding so members and arrays of the struct stay aligned
  2. Every member is rounded up to the size of a pointer
  3. sizeof also counts the struct tag name
  4. The struct stores a hidden pointer to its type

Answer: A. The compiler inserts padding so members and arrays of the struct stay aligned

Explanation

Padding may be added between members and at the end so that each member sits at an address suited to its alignment.

Step-by-step solution

  1. Struct members need aligned memory addresses for efficient access.
  2. Compiler adds padding between members to ensure proper alignment.
  3. Padding increases struct size beyond sum of individual member sizes.
  4. This ensures faster data retrieval and avoids misalignment penalties.
  5. Final answer: The compiler inserts padding so members and arrays of the struct stay aligned.

Why the other options are wrong

  • B. Members are not rounded up to pointer size; padding is added only to meet each member's alignment.
  • C. The tag name is not part of the struct's size in memory; sizeof counts only members and padding.
  • D. A struct does not store a hidden pointer to its type; the extra bytes come from padding.

Tip: Remember: Struct size includes padding for alignment, not just member sizes.

Question 8Medium

What is the output?

#include <stdio.h>

struct Val {
    char tag;
    union { int i; double d; } u;
};

int main(void) {
    struct Val v[2] = {{'i', .u.i = 7}, {'d', .u.d = 1.5}};
    double sum = 0;
    for (int k = 0; k < 2; k++)
        sum += v[k].tag == 'i' ? v[k].u.i : v[k].u.d;
    printf("%.1f\n", sum);
    return 0;
}
  1. 8.5
  2. 8.0
  3. 1.5
  4. 7.0

Answer: A. 8.5

Explanation

The tag records which union member is valid, so the code reads u.i = 7 from the first element and u.d = 1.5 from the second.

Step-by-step solution

  1. First element's tag is 'i', so add 7 to sum.
  2. Second element's tag is 'd', so add 1.5 to sum.
  3. Sum = 7 + 1.5 = 8.5.
  4. Print format %.1f rounds 8.5 to 8.5.
  5. Final output is 8.5.

Why the other options are wrong

  • B. The second value is wrong; 1.5 is added for the d element, so the sum is 8.5, not 8.0.
  • C. The first element is an int tagged i, so 7 is added; the sum is not just the 1.5 from the second element.
  • D. The second element has tag d, so 1.5 is added to 7, giving 8.5, not 7.0.

Tip: Check union access via tags and use correct print formatting.

Question 9Hard

Assume char is 1 byte, short is 2 bytes and double is 8 bytes with 8-byte alignment. What is the output?

#include <stdio.h>
#include <stddef.h>

struct R { char a; double b; short c; };

int main(void) {
    printf("%zu %zu %zu\n", offsetof(struct R, b), offsetof(struct R, c), sizeof(struct R));
    return 0;
}
  1. 4 12 16
  2. 1 9 11
  3. 8 16 18
  4. 8 16 24

Answer: D. 8 16 24

Explanation

b must start at a multiple of 8 (offset 8), c follows at 16, and the struct is padded to a multiple of 8: 18 rounds up to 24.

Step-by-step solution

  1. offsetof(struct R, b) = 8 because double needs 8-byte alignment.
  2. offsetof(struct R, c) = 16 as it follows double and is 2 bytes.
  3. sizeof(struct R) = 24 to align with 8-byte boundary after short c.
  4. Struct size rounds up to the next multiple of 8: 18 becomes 24.
  5. Final output is 8 16 24.

Why the other options are wrong

  • A. Offset of b is 8, not 4, because the double needs 8-byte alignment after the char.
  • B. There is padding after the char, so offset of b is 8 and the total size is 24, not 9 and 11.
  • C. The struct size must be a multiple of 8 because of the double alignment, so 18 is rounded up to 24.

Tip: Round the struct size up to the next multiple of the largest member alignment.

Question 10Hard

What is the output?

#include <stdio.h>

struct F {
    unsigned int a : 3;
    unsigned int b : 1;
};

int main(void) {
    struct F f = {0, 0};
    unsigned int v = 9;
    f.a = v;
    f.b = v;
    printf("%u %u\n", f.a, f.b);
    return 0;
}
  1. 7 1
  2. 9 1
  3. 1 1
  4. 1 9

Answer: C. 1 1

Explanation

An unsigned bit-field keeps only its low bits: 9 mod 8 = 1 for the 3-bit field and 9 mod 2 = 1 for the 1-bit field.

Step-by-step solution

  1. The 3-bit field a stores only the low 3 bits of 9.
  2. 9 is 1001 in binary; its low 3 bits are 001, so a = 1.
  3. The 1-bit field b stores the low bit, which is 1, so b = 1.
  4. Output is 1 1: option C.

Why the other options are wrong

  • A. The 3-bit field cannot show 7 from the value 9; its low bits are 001, so a is 1.
  • B. A is 3 bits and holds 1, not 9.
  • D. A is 1 and b is 1, so the output is 1 1, not 1 9.

Tip: Assigning to a bit-field keeps only its low bits, the value modulo 2 to the power of the field width.

Practise all 28 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics