TYPEDEF AND UNION IN C PROGRAMMING

TYPEDEF AND UNION IN C PROGRAMMING

typedef in c:

C programming language provides a keyword called “typedef”, which you can use to give a type a new name.  Its mostly used with user-defined datatypes, when names of the datatypes become slightly complicated to use in programs. The “typedef” is an advance feature in C language which allows us to create an alias or new name for an existing type or user-defined type.

syntax:

typedef <previous_name> <alias_name>
  • typedef: it’s a keyword
  • previous_name: It is the name of any existing type or user-defined type created using structure/union.
  • alias_name: new name you want to give to any existing type or user-defined type.

Let’s take an example:

typedef int myint;

Now “myint” is a new name of “int”. From now on, we can declare new int variables using myint instead of int keyword. Let’s do some coding and make it more clear:

PROGRAM-1  //simple program of typedef

#include<stdio.h>
int main()
{
  typedef int integer;
  integer a=4;
  printf("value of a is %d",a);

return 0;
}

output:

value of a is 4
PROGRAM-2 // Applying typedef in structure

#include<stdio.h>
typedef struct student
{ 
  int id;
  int marks;
  char fav_character;
  char name [34];
}std;

int main()
{
  typedef int* intpointer;
  intpointer a,b;
  int c=89;
  a=&c;
  b=&c;
  printf("value of a is %dn",a);
  printf("value of b is %d",b);
return 0;
}

output:

value of a is 6356740
value of b is 6356740

There is no need to learn each concept of “typedef” at a beginner level,

As a beginner, this is enough!

unions in c :

  • Union is a user-defined data type (very similar to structures).
  • The difference between structure and unions lied in the fact that in structure, each member has its own storage location. Whereas members of a union uses a single shared memory location.
  • This single shared memory location is equal to the size of its largest data member.
  • Union is a data type in C programming that allows different data types to be stored in the same memory locations.
  • Union provides an efficient way of reusing the memory location, as only one of its members can be accessed at a time. 
  • A union is used almost in the same way you would declare and use a structure.

Declaring and accessing union members:

  • Like structures, we access any member by using the member access operator (.) in unions.
  • We use keyword union to define a union.
  • Syntax is very similar to that of structure.
StructureUnion
struct structure
{
float marks; //4 bytes
int id; //4 bytes
}s1;
union student
{

float marks; //4 bytes
int id; //4 bytes

}s1;
[s1]—– total memory location use here is (4+4) 8 bytes.[s1]—– total memory location use here is 4 bytes.
Union cannot handle all member at once

syntax for unions :

union test
{
   int a;
   float b;
   char c;
}un;

un.a;   // for accessing members of union "un"
un.b;
un.c;

Advantage of union over structure:

It occupies less memory because it occupies the size of the largest member only.

Disadvantage of union over structure:

Only the last entered data can be stored in the union. It overwrites the data previously stored in the union.

defining a union:

union union_name   
{  
    data_type member1;  
    data_type member2;  
    .  
    .  
    data_type memeberN;  
};  

Now, let’s do programming to clear each concepts:

#include <stdio.h>  
#include <string.h>  
union employee    
{   int id;    
    char name[50];    
}e1;                                            //declaring e1 variable for union  
int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, "Sonoo Jaiswal");         //copying string into char array  
  
 //printing first employee information  
   printf( "employee 1 id : %dn", e1.id);  
   printf( "employee 1 name : %sn", e1.name);  
   return 0;  
}  

output:

employee 1 id : 1869508435 
employee 1 name : Sonoo Jaiswal

As you can see, id gets garbage value because name has large memory size. So only name will have actual value.

I wish... now each topic is clear to you. 
The more you do practice the more concepts you get cleared. So always keep practicing until you got a command on it. 
In our next artcle we will discuss about "VARIABLES IN C". 

I will see you next time... Also visit our previos articles listed below:

AND MANY MORE, SO VISIT OUR SITEā€¦https://justhinter.com/category/programming/

Scroll to Top