The gentlest struct question in the bank — declare, initialize, add two members. But “easy” questions earn their place by checking the mental model: does {1, 2} land where you think it lands, and is s.a + s.b doing arithmetic on values or something stranger? This question from our C Programming Quiz App settles the basics that every later struct trap builds on.
The Quiz Question
struct S { int a; int b; };
struct S s = {1, 2};
printf("%d", s.a + s.b);
What is printed by this code?
- 12
- 3
- Compile error
- Undefined behavior
The Correct Answer: 3
Brace initializers fill struct members in declaration order: a gets 1, b gets 2. The dot operator reads each member as an ordinary int, and + adds the values: 1 + 2 = 3. Verified on gcc 13.3 and Apple clang 21, clean under -ansi -Wall -Wextra:
$ gcc -ansi -Wall -Wextra struct.c && ./a.out 3
Why Each Wrong Answer Is Wrong
Why not 12?
12 is the concatenation reading — gluing “1” and “2” together. Struct members are typed values, not text, and + on two ints is always arithmetic (the same misconception behind the “34” option in the add(3, 4) question). You’d get the text 12 only from printing the members side by side: printf("%d%d", s.a, s.b).
Why not a compile error?
Everything here is textbook-legal C89: the struct definition, the ordered brace initializer, member access with ., and integer arithmetic. Suspicion sometimes attaches to the initializer syntax — but {1, 2} for a two-member struct has been valid since the language’s beginning. (Too many initializers — {1, 2, 3} — would be the error.)
Why not undefined behavior?
Both members are explicitly initialized before use, the reads are properly typed, and 1 + 2 overflows nothing. Even if the initializer were partial — {1} — there’d still be no UB: the remaining members are zero-filled, exactly like partial array initialization. UB would need an uninitialized struct (struct S s; then reading s.a).
Struct Initialization, From C89 to Designated
The positional form in this question is the original syntax, and its weakness is the position itself: reorder the struct’s members in a refactor and every {1, 2} in the codebase silently changes meaning. C99 fixed that with designated initializers:
struct S s1 = {1, 2}; /* positional: order-dependent */
struct S s2 = {.a = 1, .b = 2}; /* designated: order-free, C99 */
struct S s3 = {.b = 2}; /* .a is zero-filled */
struct S s4 = {0}; /* everything zeroed — the idiom */
Designated initializers name their targets, tolerate reordering, and make partial initialization readable. The {0} idiom zeroes every member (including padding-adjacent values you’d otherwise forget) and is the standard way to start a clean struct. One more basic worth locking in: s.a works because s is a struct object — the moment you hold a pointer to a struct, member access becomes p->a, and mixing up . and -> is the next rung on this ladder. The structures guide climbs the whole thing — nesting, arrays of structs, and passing structs to functions.
Frequently Asked Questions
How does {1, 2} initialize a struct in C?
Positionally, in member declaration order: the first value goes to the first member, the second to the second. For struct S { int a; int b; }, a becomes 1 and b becomes 2.
What happens if a struct initializer has fewer values than members?
The remaining members are zero-initialized — {1} sets a to 1 and b to 0. Only a struct with no initializer at all (automatic storage) has indeterminate members.
What are designated initializers?
C99 syntax that names the member being initialized: {.b = 2, .a = 1}. Order no longer matters, unlisted members zero-fill, and the code survives struct-member reordering that would silently break positional initializers.
Related Reading
- Structures in C – Complete Guide
- Union Endianness in C – Why u.c[0] Is Platform-Dependent
- Partial Array Initialization in C – Why a[3] Is 0
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #151 in the C Programming Quiz App — 155 questions with explanations covering structs, unions, memory, and more.
Download on Google Play →