KwickAcademy C++ · 6 min · free
Structures, Unions, Enumerations and typedef
A structure groups members of different types; a union makes all members share one memory space. enum names integer constants from 0, and typedef gives a type a new name.
On screen in this lesson
What is a structure?
| A user-defined type that groups variables |
| Members can have different data types |
| Keyword: struct |
| Example: roll, name and marks of a student |
Rules for initialising
| Values go to members in the order declared |
| Missing values become zero |
| Named style: {.marks = 90, .roll = 3} |
| Assign one member later: s.marks = 91; |
Pause and predict
| What is c[1].roll? |
| What is c[2].marks? |
| Index starts at 0, not 1 |
Structure vs union
| Point | Structure | Union |
|---|---|---|
| Keyword | struct | union |
| Memory | each member | shared by all |
| Size | sum of members | largest member |
| Values at once | all members | only one |
Quick recap
| struct groups members of different types |
| Dot for variables, arrow for pointers |
| Union members share one memory space |
| enum names integer constants from 0 |
| typedef gives a type a shorter name |
Quick answers
Why is sizeof(union u) 4 but struct s 8?
Union members share one space; a structure needs room for both.
What is WED in enum day { MON, TUE, WED }?
2, because counting starts at 0.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Where is the semicolon needed?41 sec
When do you use ->?40 sec
What is the size of a union?44 sec
Where does enum counting start?41 secThe full lesson, in text
Hello students, welcome to Kwickprep. A report card has a roll number, a name and marks, all of different types. An array can hold only one type. So how does C keep them together? Today we will learn structures, unions, enum and typedef, and see why a union can be smaller than a structure.
A structure lets us build our own data type. It is a user-defined type, which means we design it, and it groups several variables under one name. Each variable inside it is called a member, and members can have different types. We create a structure with the keyword struct. A student record with roll number, name and marks is the classic example.
Here is how we declare a structure. We write struct, then the tag name student, then curly brackets. Inside, we list the members, roll as an int, name as a character array, and marks as a float. Notice the semicolon after the closing bracket. Forgetting it is a very common compile error. This declaration is only a design, and it creates no variable yet.
To fit the screen, the tag st is short for student. Inside main, struct st s creates a variable named s. The values in curly brackets go to the members in order. So roll gets twelve and marks gets eighty eight point five. The program prints twelve and eighty eight point five.
Keep these rules in mind. Values in the brackets go to the members in the order they were declared. If you give fewer values, the remaining members become zero. You can also name the members, with a dot before each name, and then any order works. Or you can assign a single member later, like s dot marks equals ninety one.
We reach a member with the dot operator, like a dot marks. Here a is Aman's record, with roll five and marks seventy. The pointer p stores the address of a. With a pointer, we use the arrow operator, a minus sign and a greater than sign. So a dot marks becomes seventy five, and p arrow roll changes the same record to six. It prints six and seventy five.
One record is useful, but a class has many students. So we make an array of structures. Here c is the class, holding three records, and t is the total. Each inner pair gives a roll and marks. The loop adds c of i dot marks for every student. Sixty plus eighty five plus seventy two is two hundred seventeen.
Pause the video and predict two values from the same program. What is c of one dot roll? It is two, because index one is the second student. What is c of two dot marks? It is seventy two, the third student. Always remember that array indexes start at zero.
A union looks like a structure, but it stores members in a different way. We declare it with the keyword union instead of struct. In a structure, each member gets its own memory, but in a union, all members share the same memory. So a structure is at least the sum of its members, while a union is the size of its largest member. That means a structure keeps every value, but a union holds only one member's value at a time.
Let us prove it. Both types have an int and a float. On most computers today, each of these takes four bytes. The structure needs room for both, so its size is eight. The union shares one space, so its size is four. Percent z u is the format used to print a size.
Here x is a union with roll and marks. First we store twelve in roll and print it right away. Then we store eighty eight point five in marks. That write uses the same memory, so roll is no longer valid. We print marks while it is the active member. Unions save memory when only one value is needed at a time.
An enumeration, written enum, gives names to whole number constants. Names are easier to read than bare numbers. In enum day, MON is zero, TUE is one and WED is two, because counting starts at zero. We can also set values ourselves, so LOW is one and HIGH is five. The program prints two and five.
The keyword typedef gives a new name to an existing type. It does not create a new type. Here Marks becomes another name for int. Then we name a structure St, short for student, with members roll and m for marks. Now we write St s, without the word struct. It prints seven and ninety one.
Let us revise what we learned today. A structure groups members of different types under one name. We use the dot operator with a variable, and the arrow operator with a pointer. In a union, all members share one memory space, so only one value is valid at a time. An enum names integer constants, starting from zero by default. And typedef gives an existing type a new, shorter name.
Courses that teach this
| Course | Unit |
|---|---|
| GSEB Std 10 Computer Studies | Programming Fundamentals & C Language |
| Programming All levels C | Introduction to C and Program Structure |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

