0% found this document useful (0 votes)
21 views4 pages

Structures and Union 4 Year CRT

Uploaded by

konadalokesh143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Structures and Union 4 Year CRT

Uploaded by

konadalokesh143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Structures

A structure is a user-defined data type that groups related variables of different data
types. It allows you to create complex data types that represent a single entity.

#include <stdio.h>
#include <string.h>

struct Student {
int id;
char name[50];
float gpa;
};

int main() {
struct Student student1;
student1.id = 1;
strcpy(student1.name, "Alice");
student1.gpa = 3.5;

printf("ID: %d, Name: %s, GPA: %.2f\n", student1.id, student1.name,


student1.gpa);
return 0;
}

ID: 1, Name: Alice, GPA: 3.50

Unions
A union is similar to a structure in that it can also hold multiple data types, but it
can only store one member at a time. The size of a union is determined by the size
of its largest member.

#include <stdio.h>

union Data {
int intValue;
float floatValue;
char charValue;
};

int main() {
union Data data;

data.intValue = 10;
printf("Int: %d\n", data.intValue);

data.floatValue = 220.5;
printf("Float: %.2f\n", data.floatValue); // Previous value of intValue is
overwritten.

data.charValue = 'A';
printf("Char: %c\n", data.charValue); // Previous values are overwritten.

return 0;
}

Int: 10
Float: 220.50
Char: A

Key Differences
Feature Structure Union
Memory Allocates memory for the largest
Allocates memory for all members
Allocation member only
Data Storage Stores all members simultaneously Stores only one member at a time
When you need to save memory
When you need to group related
Use Case and only one value is needed at a
data that can exist independently
time
When to Use
 Structures: Use when you need to group related data that will be used
together and need to access all members simultaneously.
 Unions: Use when you need a variable that can hold different data types, but
only one at a time, which helps in saving memory.
Common Questions
1. What is the difference between a structure and a union?
A: A structure allocates memory for all its members, allowing simultaneous
access to all data fields. In contrast, a union allocates memory equal to the size
of its largest member and can only hold one member at a time. This makes
structures suitable for grouping related data while unions are more memory-
efficient when only one value is needed.

How do you define a structure in C? Provide an example.


 Answer: You define a structure using the struct keyword.

Write a C program that uses a structure to store student details (ID, name,
and GPA) and displays them.
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float gpa;
};
int main() {
struct Student student1;
student1.id = 101;
strcpy(student1.name, "John Doe");
student1.gpa = 3.8;

printf("ID: %d, Name: %s, GPA: %.2f\n", student1.id, student1.name,


student1.gpa);
return 0;
}

Can you access union members directly? How? Give an example.


 Answer: Yes, you can access union members using the dot operator.
union Data {
int intValue;
float floatValue;
};
int main() {
union Data data;
data.intValue = 10; // Assigning int
printf("Int: %d\n", data.intValue);
data.floatValue = 20.5; // Assigning float, overwrites intValue
printf("Float: %.2f\n", data.floatValue);
return 0;
}

What happens if you try to access a structure member without


initializing it?
 Answer: Accessing an uninitialized member of a structure will lead to
undefined behavior, as the value could be anything (garbage value). It’s
always good practice to initialize structure members before use.

#include <stdio.h>
struct Employee {
int id;
char name[50];
};

void displayEmployee(struct Employee emp) {


printf("ID: %d, Name: %s\n", emp.id, emp.name);
}
int main() {
struct Employee emp1 = {1, "Alice"};
displayEmployee(emp1);
return 0;
}
1. Explain how memory is allocated for structures and unions.
o Answer: For structures, memory is allocated for the total size of all
members combined. For unions, memory is allocated based on the size
of the largest member only. For example, if a structure has an int, a
float, and a char, the size will be the sum of their sizes, while a union
containing the same types will only occupy space for the largest type.

Practice Scenarios
 Create a complex structure: Design a structure that represents a book with
fields for title, author, and publication year, and write a function to display
this information.
 Union for different data types: Design a union that can hold either a
temperature in Celsius or Fahrenheit, and write functions to convert between
the two.
Tips for Preparation
 Practice writing small programs using structures and unions.
 Be familiar with memory allocation and how to manipulate structure and
union members.
 Understand real-world scenarios where structures and unions can be
effectively used.

You might also like