C programming tutorials for Beginners – C Unions

(C Programming Tutorials)

C Unions

In this tutorial, you’ll learn about unions in C programming. More specifically, how to create unions, access its members and learn the differences between unions and structures.


How to define a union?

We use the union keyword to define unions. Here’s an example:

union car
{
  char name[50];
  int price;
};

The above code defines a derived type union car.


Create union variables

When a union is defined, it creates a user-defined type. However, no memory is allocated. To allocate memory for a given union type and work with it, we need to create variables.

Here’s how we create union variables.

union car
{
  char name[50];
  int price;
};

int main(){
  union car car1, car2, *car3;
  return 0;
}

Another way of creating union variables is:

union car
{
  char name[50];
  int price;
} car1, car2, *car3;

In both cases, union variables car1car2, and a union pointer car3 of union car type are created.


Access members of a union

We use the . operator to access members of a union. To access pointer variables, we use also use the -> operator.

In the above example,

  • To access price for car1car1.price is used.
  • To access price using car3, either (*car3).price or car3->price can be used.

Difference between unions and structures

Let’s take an example to demonstrate the difference between unions and structures:

#include <stdio.h>
union unionJob
{
   //defining a union
   char name[32];
   float salary;
   int workerNo;
} uJob;

struct structJob
{
   char name[32];
   float salary;
   int workerNo;
} sJob;

int main(){
   printf("size of union = %d bytes", sizeof(uJob));
   printf("nsize of structure = %d bytes", sizeof(sJob));
   return 0;
}

Output

size of union = 32
size of structure = 40

Why this difference in the size of union and structure variables?

Here, the size of sJob is 40 bytes because

  • the size of name[32] is 32 bytes
  • the size of salary is 4 bytes
  • the size of workerNo is 4 bytes

However, the size of uJob is 32 bytes. It’s because the size of a union variable will always be the size of its largest element. In the above example, the size of its largest element, (name[32]), is 32 bytes.

With a union, all members share the same memory.


Example: Accessing Union Members

#include <stdio.h>
union Job {
   float salary;
   int workerNo;
} j;

int main(){
   j.salary = 12.3;

   // when j.workerNo is assigned a value,
   // j.salary will no longer hold 12.3
   j.workerNo = 100;

   printf("Salary = %.1fn", j.salary);
   printf("Number of workers = %d", j.workerNo);
   return 0;
}

Output

Salary = 0.0
Number of workers = 100

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

SQL tutorials for Business Analyst – SQL | UNIONS CLAUSE

MySQL Tutorials for Business Analyst: How to use Unions in MySQL

PostgreSQL tutorial for Beginners – PostgreSQL – UNIONS Clause