In C programming, arrays are great for storing multiple items, but they can only store items of the same data type (e.g., all integers). To group different data types together (like an integer ID, a float Salary, and a string Name), C provides two user-defined data types: Structures (struct) and Unions (union).
Example calculation: A struct containing a char (1 byte), an int (4 bytes), and a double (8 bytes) will take 13 bytes of memory. A union with the same members will only take 8 bytes.
Changing the value of one member in a union will unpredictably alter the values of all other members.
struct.union.You might wonder why anyone would use a Union if it destroys data. Unions are highly specialized. They are used in Embedded Systems programming (like microcontrollers in microwaves or cars) where RAM is extremely scarce (sometimes only 2 Kilobytes), and memory conservation is more important than holding multiple values simultaneously.
Yes, C++ fully supports both. However, in C++, a `struct` is basically identical to a `class`, except its members are public by default.
What is the Full Form of EDVAC in Computers?
Learn the full form of EDVAC in computer science. Discover the history of this massive, early electronic computer and its revolutionary stored-program architecture.
Evolution of Computers: The 5 Generations
Learn about the evolution of computers from massive room-sized vacuum tubes to modern AI. Explore the 5 generations of computer history.
What are Output Devices? (Definition and Examples)
Learn the definition of computer output devices. Find clear examples like monitors, printers, speakers, and projectors, explained for students.
What is a Web Browser? (With Examples)
Learn what a web browser is and see popular examples of web browsers like Google Chrome, Safari, Mozilla Firefox, and Microsoft Edge.
Fibonacci Series in C (Code and Logic)
Learn how to write a Fibonacci series program in C. Understand the logic with both iterative and recursive methods with complete code examples.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.