🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In C programming, structures and unions are both user-defined data types used to store multiple data types. However, they differ in how they manage memory and how they're used. A structure allocates enough space to store all its members, whereas a union allocates space equal to its largest member and shares this memory space among all its members.
2. Key Points
1. Structures allocate separate memory locations for each of their members.
2. Unions allocate a single memory location shared by all their members.
3. Changing one member in a union changes the values of all members.
4. Structures are used when you want to store different values for each member.
3. Differences
| Structure | Union |
|---|---|
| Allocates separate memory for each member. | Allocates common shared memory for all members. |
| The size is the sum of the size of all members. | The size is the size of the largest member. |
| Each member has its own value. | Only one member can have a meaningful value at any given time. |
4. Example
#include <stdio.h>
// Structure example
struct StructExample {
int a;
char b;
float c;
};
// Union example
union UnionExample {
int a;
char b;
float c;
};
int main() {
struct StructExample se = {10, 'A', 3.14};
union UnionExample ue;
ue.a = 10;
printf("Structure a: %d, b: %c, c: %f\n", se.a, se.b, se.c);
printf("Union a: %d\n", ue.a);
ue.b = 'B';
printf("Union b: %c\n", ue.b);
printf("Union a (after modifying b): %d\n", ue.a);
return 0;
}
Output:
Structure a: 10, b: A, c: 3.140000 Union a: 10 Union b: B Union a (after modifying b): 66
Explanation:
1. The structure example shows distinct values for each member.
2. In the union, changing b also alters the value of a because they share the same memory space. Hence, after b is set to 'B', which has an ASCII value of 66, a shows 66 instead of 10.
5. When to use?
- Use a structure when you need to store multiple values of different types and use them independently.
- Use a union for memory-efficient storage when you only need one of its members to have a meaningful value at any given time.
Difference between malloc() and calloc()?
Difference between Local Variable and Global Variable in C
Difference between Global and Static Variables in C
Difference Between Call by Value and Call by Reference in C
Difference Between getch() and getche() in C
Difference between printf() and sprintf() in C
Difference between Arrays and Pointers in C
Difference between Structure and Union in C
Difference Between Stack and Heap Memory Allocation in C
Difference Between Macro and Function in C
Difference between = and == in C
Difference Between for loop and while loop in C
Difference Between Linked List and Array in C
Difference between fgets() and gets() in C
Difference between ++i and i++ in C
Difference between struct and typedef struct in C
Difference between int main() and void main() in C
Difference between Character Array and String in C
Difference between break and continue in C
Difference between exit() and return in C
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment