Skip to main content

Structures and Union in C

 Structures and Unions

Introduction: Structures and unions are composite data types in C that allow the grouping of multiple variables of different data types under a single name. They provide a way to represent complex data structures in a program, making it easier to organize and manipulate data.

Definition:

  1. Structure:

    • A structure is a collection of variables (of different data types) grouped together under a single name.
    • Each variable within a structure is called a member or field.
    • Structures enable the creation of custom data types to represent real-world entities.
    • They are defined using the struct keyword.
  2. Union:

    • A union is similar to a structure in that it also groups variables of different data types under a single name.
    • However, unlike structures, unions allocate memory that is only as large as the largest member.
    • This means that only one member of the union can be accessed at a time.
    • Unions are useful when memory conservation is a concern, and only one member needs to be accessed at a time.
    • They are defined using the union keyword.





Programming Code in C Explaining the Working of Both:

1. Structure Example:

c
#include <stdio.h> // Defining a structure to represent a student 
struct Student
{ int rollNumber; 
char name[50]; 
float marks; }; 
int main()
{ // Creating an instance of the structure 
struct Student student1; // Assigning values to the members of the structure student1.rollNumber = 101
strcpy(student1.name, "John"); 
 student1.marks = 85.5; // Accessing and printing the values of structure members printf("Student Details:\n"); 
printf("Roll Number: %d\n", student1.rollNumber); 
printf("Name: %s\n", student1.name); 
printf("Marks: %.2f\n", student1.marks);
return 0; }

Explanation:

  • In this example, a structure named Student is defined to represent information about a student, including roll number, name, and marks.
  • An instance of the structure named student1 is created in the main() function.
  • Values are assigned to the members of the structure using dot (.) operator.
  • Finally, the values of the structure members are accessed and printed to the console.

2. Union Example:

c
#include <stdio.h> // Defining a union to represent a book 
union Book 
{ char title[50]; 
float price; }; 
int main()
{ // Creating an instance of the union 
union Book book1; // Assigning values to the members of the union strcpy(book1.title, "Introduction to C Programming"); 
printf("Book Title: %s\n", book1.title); 
 book1.price = 29.99
printf("Book Price: %.2f\n", book1.price); 
return 0; }

Explanation:

  • In this example, a union named Book is defined to represent information about a book, including title and price.
  • An instance of the union named book1 is created in the main() function.
  • Initially, the title of the book is assigned to the title member of the union.
  • Later, the price of the book is assigned to the price member of the union.
  • When the price is assigned, the previous value of the title member is overwritten.
  • Finally, both the title and price of the book are printed to the console.

Conclusion: Structures and unions are powerful features of C programming that enable the creation of custom data types to represent complex entities. By grouping variables of different data types under a single name, structures and unions enhance code organization, readability, and maintainability. Understanding how to define, declare, and use structures and unions is essential for developing efficient and scalable C programs.

Comments

Popular posts from this blog

Header Files and their Examples in C

  What Is A Header File In C? A header file in C language is a text file containing definitions of functions, variables, and macros that are shared among multiple source files. It provides an interface to the variety of functions and data structures that can be used by other parts of the program. Essentially, a header file serves as a contract that specifies what features/ operations are available to the rest of the program without exposing the implementation details. Standard Header File In C & Their Uses In C, there are many standard header files, each of which has a unique collection of utilities and methods. Naturally, there are a few standard C header files that are most typically used in applications/ C code.  We have compiled a detailed explanation of all 25 standard header files in C. The <stdio.h> Header File In C The name stands for  standard input/output header , and it contains functions for standard input and output operations such as...

Recursive Functions in C

  Lecture Notes: Recursive Functions in C Introduction to Recursive Functions: Recursive functions are functions that call themselves directly or indirectly to solve a problem by dividing it into smaller instances of the same problem. They are a powerful programming technique used to solve problems that can be broken down into simpler subproblems. Definition: A recursive function is a function that calls itself either directly or indirectly during its execution. Types of Recursive Functions: Direct Recursion: In direct recursion, a function calls itself directly. c Copy code // Example of direct recursion void countdown ( int n) { if (n > 0 ) { printf ( "%d " , n); countdown(n - 1 ); // Recursive call } } Indirect Recursion: In indirect recursion, a function calls another function, which in turn calls the first function, creating a cycle of function calls. c Copy code // Example of indirect recursion void func1 ( int n) ; void func2 ( ...