Structure Quiz and Interview Questions- Embedded C Training For Beginners

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Free C Programming Course · Chapter 10 · Lecture 5

Structure Quiz and Interview Questions

A hands-on thermal monitor lab, a 10-question quiz and 12 interview questions on C structures for embedded C programming roles.

You have learned how to define, declare, initialize and access structures. This final lecture for these pages of our free C programming course is about practice. First you will build a small embedded-style program that uses three structure variables, then you will test yourself with a quiz, and finally you will go through the C structure interview questions that come up again and again in embedded interviews.

Use this free C tutorial page as a checklist: if you can answer everything here without peeking, you are ready for the next topics of the free embedded C course.

What You Will Learn

In this lecture you will:

  • Combine definition, initialization and member access in one realistic program.
  • Predict program output involving partial initialization, struct copies and sizeof.
  • Answer common interview questions about structures, padding and memory.

Prerequisites

Lectures 1 to 4 of Chapter 10. Try to attempt the lab before reading the solution.

Hands-On Lab: Thermal Monitor

Task. A board has three temperature channels: a motor, a driver chip and a battery. Each reading arrives as a raw 12-bit ADC count (0 to 4095), which maps linearly to 0 to 100 degrees Celsius. Store each channel in a structure, convert the counts, flag any channel above 70 degrees, and print the average temperature.

Lab data flow

ADC count in adc_rawConvert to degreesStore in temp_cCompare with limitPrint and average
#include <stdio.h>

struct channel {
    char  name[10];
    int   adc_raw;      /* 12-bit ADC count */
    float temp_c;
};

int main(void)
{
    const float limit_c = 70.0f;
    struct channel s1 = { "motor",  2650, 0 };
    struct channel s2 = { "driver", 1980, 0 };
    struct channel s3 = { "batt",   1500, 0 };

    /* simple linear conversion: 0..4095 -> 0..100 C */
    s1.temp_c = s1.adc_raw * 100.0f / 4095.0f;
    s2.temp_c = s2.adc_raw * 100.0f / 4095.0f;
    s3.temp_c = s3.adc_raw * 100.0f / 4095.0f;

    float avg = (s1.temp_c + s2.temp_c + s3.temp_c) / 3.0f;

    printf("%-7s %5d  %6.2f C%s\n", s1.name, s1.adc_raw, s1.temp_c, s1.temp_c > limit_c ? "  OVER" : "");
    printf("%-7s %5d  %6.2f C%s\n", s2.name, s2.adc_raw, s2.temp_c, s2.temp_c > limit_c ? "  OVER" : "");
    printf("%-7s %5d  %6.2f C%s\n", s3.name, s3.adc_raw, s3.temp_c, s3.temp_c > limit_c ? "  OVER" : "");
    printf("average = %.2f C\n", avg);
    return 0;
}

Build and Run

$ gcc -std=c11 -Wall -Wextra -o ep_struct_lab ep_struct_lab.c
$ ./ep_struct_lab

Expected Output

motor    2650   64.71 C
driver   1980   48.35 C
batt     1500   36.63 C
average = 49.90 C

No channel passes the 70 degree limit, so no OVER flag appears. Change the motor count to 3000 and run again: it converts to 73.26 degrees and the line gains the OVER marker.

Challenges to try

  • Add a fourth channel and include it in the average.
  • Rename a member in the definition and see how many lines the compiler complains about.
  • Print sizeof(struct channel) and work out where the padding bytes are.
  • Rewrite the three initializers with designated initializers.

Quiz: Test Your Knowledge

Choose an answer first, then open the answer line to check yourself.

Q1. Which statement about a structure definition is true?

  1. It reserves memory for every member.
  2. It creates a new data type and reserves no memory.
  3. It creates one global variable.
  4. It initializes all members to zero.
Show answer

B. A definition is only a template. Memory is reserved when variables are declared.

Q2. What is wrong with the following code?

struct pt { int x; int y; }
int main(void) { struct pt p; return 0; }
  1. Nothing
  2. The semicolon after the closing brace is missing
  3. Members cannot be int
  4. main needs arguments
Show answer

B. A structure definition must end with a semicolon.

Q3. Which line correctly accesses the member rpm of a variable m1 of type struct motor?

  1. motor.rpm
  2. m1-rpm
  3. m1.rpm
  4. struct m1.rpm
Show answer

C. Use the variable, a dot, and the member name.

Q4. Given struct pair { int a; int b; }; struct pair p = { 3 };, what does printf("%d %d", p.a, p.b) print?

  1. 3 garbage
  2. 3 3
  3. 3 0
  4. Compile error
Show answer

C. Members without a value in the initializer list become zero.

Q5. What is the output of this code?

struct pair x = { 1, 2 };
struct pair y = x;
y.a = 10;
printf("%d %d", x.a, y.a);
  1. 10 10
  2. 1 1
  3. 1 10
  4. 10 1
Show answer

C. The copy is independent, so changing y does not change x. The program prints 1 10.

Q6. Why is a == b illegal for two structure variables?

  1. Structures are always different
  2. C defines no equality operator for structures
  3. Only pointers can be compared
  4. The variables must be global
Show answer

B. Compare members one by one instead.

Q7. Which declaration lets you create more variables of the same type later?

  1. struct { int x; } p;
  2. struct pt { int x; } p;
Show answer

B. The anonymous form in A has no tag, so its type cannot be named again.

Q8. On a typical 32-bit or 64-bit system, what is sizeof(struct item) for struct item { char c; int n; };?

  1. 5
  2. 6
  3. 8
  4. 4
Show answer

C. Three padding bytes follow c so that n is 4-byte aligned, giving 8 bytes. We ran this on GCC to confirm.

Q9. Which is the correct way to put text into a char name[12] member?

  1. v.name = "pump";
  2. strcpy(v.name, "pump");
  3. v.name[] = "pump";
  4. *v.name = "pump";
Show answer

B. Array members cannot be assigned with =; copy the characters.

Q10. Is this definition valid in C?

struct cfg { int baud = 9600; };
  1. Yes
  2. No, members cannot have default values in the definition
Show answer

B. No memory exists at definition time, so there is nowhere to store a default. Use a constant structure of defaults.

C Structure Interview Questions

These questions are commonly asked in embedded C interviews. Try to answer aloud before you open the model answer.

1. What is a structure and why do we need it?

A structure groups related data of different types under one name. We need it because arrays hold only one type, and real objects like a sensor or UART configuration have mixed properties.

2. What is the difference between a structure definition and a structure variable?

The definition is a template that reserves no memory. A variable is an instance of that template and owns real memory for all members.

3. Is the size of a structure always the sum of its members?

No. The compiler may add padding to align members, so sizeof can be larger than the sum.

4. What is padding and why does it exist?

Padding is unused bytes inserted so each member sits at an address that suits its type. Aligned access is faster and, on some processors, required.

5. How can you reduce structure size?

Order members from largest to smallest type, or in special cases use compiler packing, accepting the access-speed and portability trade-offs.

6. Why can’t you compare two structures with ==?

C does not define it, partly because of padding bytes with unspecified content and because members such as strings or floats need special comparison. Compare member by member.

7. Can you assign one structure to another? What gets copied?

Yes, if they have the same type. Every member is copied, including the contents of array members.

8. What happens to members that are not mentioned in an initializer list?

They are initialized to zero. This is why = { 0 } zeroes the whole structure.

9. What is a designated initializer and why is it useful?

A C99 syntax such as { .baud = 9600 } that sets members by name. It is order-independent and keeps working when the structure definition changes.

10. What is an anonymous structure and what is its drawback?

A structure with no tag. You cannot declare more variables of that type later, and two identical-looking anonymous structures are incompatible types.

11. Are the contents of a local structure variable zero by default?

No. Local variables without an initializer contain indeterminate values. Global and static ones are zero.

12. What is the risk of using strcpy on a structure member?

It does not check bounds, so a long string overflows the member and corrupts the following ones. Use snprintf or a size-limited copy.

Common Mistakes Recap

MistakeLecture
Forgetting the semicolon after the closing brace1
Using a local template from another function1
Using anonymous structures when a type name is needed2
Counting size by hand and ignoring padding2
Giving default values inside the definition3
Leaving local structures uninitialized3
Writing the tag instead of the variable before the dot4
Comparing structures with ==4

Summary and Key Takeaways

Chapter 10 so far

  • Structures bundle mixed types; the definition is a template, a variable owns memory.
  • Declare with the tag, avoid anonymous types, and check sizeof.
  • Initialize every local structure; prefer designated initializers.
  • Use the dot operator, copy with =, compare member by member.

Conclusion

Structures are the doorway from simple C exercises to real firmware. Once these basics feel natural, the rest of the chapter builds on them: arrays of structures, structures inside structures, passing structures to functions, pointers to structures with the arrow operator, typedef, and unions. Keep practicing with your own device descriptions; a sensor, a motor and a button are enough to find every beginner mistake yourself.

Frequently Asked Questions

Where can I practice C structures for free?

Use the lab and quiz on this page, then extend the thermal monitor with new channels and members. This free C programming course provides more exercises in later lectures.

Are structures important for embedded C interviews?

Very. Interviewers ask about padding, alignment, packing, copying and initialization because they affect memory use and hardware interfaces.

How should I prepare for C structure interview questions?

Write small programs, print sizeof and offsetof, and explain the results aloud. Understanding why beats memorizing answers.

What comes after basic structures in the course?

Arrays of structures, nested structures, passing structures to functions, pointers to structures, typedef, bit fields and unions.

Do I need hardware to learn embedded C?

No. The concepts here run on any computer. Hardware helps later, but this free embedded C course starts with plain GCC.

Is this free C tutorial suitable for beginners?

Yes. It starts from the basics and adds embedded context step by step.

Keep Learning for Free

Continue with the rest of Chapter 10, or explore our free embedded systems, free embedded Linux and free Linux kernel development courses.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →