structure program in c

C Program to find the size of Structure and Union                 



Code: 

#include <stdio.h>

union union_job
{
    char name[20];
    int salary;
    int workerno;
}
ujob;
struct structure_job
{
    char name[30];
    int salary;
    int workerno;
}sjob;
int main() {
    printf("size of union=%d",sizeof(ujob));
    printf("\nsize of Structure=%d",sizeof(sjob));
    return 0;
}

 

Sample Input and Output: 

size of union=20
size of Structure=40


 In this article,we are going to see how we can program to find the size of user_defined data type created using structure and union

What is Structure ? 

Structure is a user-defined data type in C Programming Language it creates user-defined by using multiple primitive data  type like int,char etc.All structure element are stored at contiguous memory locations.Basically it is used to create complex data structure

What is Union ?

It is very similar to Structure Data types like it is also used to create user-defined data type with the combination of different primitive data type.The only difference between them is structure is assigned a unique memory location while in union a memory location is shared by all the data members

Key Difference 

Structure support flexible array while union does not support flexible array.

We have to use struct keyword to define structure while to define union we have to use union keyword only.

Structure provides us the ability to initialize several objects at once while union allow us to only  initialize only the first member of union

We can retrieve any member at a time in structure and in union we can access one member at a time.

Pros and Cons of Structure

Pros of Structure

It is easy to maintain and we can represent whole record by using a single name.

We can use an array of structure to store more records with similar data types

In structure, we can pass complete set of records to any function using a single parameter.

Cons of Structure 

It is slow as compare to union

we can retrieve any member at a time in structure whereas you can access one member at a time in the union.

If the complexity of Projects goes beyond the limit it become harder to manage.

Pros and Cons of Union

Pros of Union

When we use union it occupies less memory compared to structure

In union only the last variable can be directly accessed.

When we use union, Its allocated space is equal to maximum size of the data member.

Cons of Union

In Union assigns one common storage space for all its members.

You can use only one union member at a time.

Union assigns one common storage space for all its members

So Now we have understood what is structure and union theoretically or we can use it definition and pros and cons so now Let's jump to the coding part

In the first line of code we have included stdio header file which will help us to use printf() and scanf() to be used.

In the next line of code we are creating one user_defined data type with name union_job  with the help of union keyword.Most of you must be thinking that why we are using union_job as data type name ,So I would like to tell them that we can use anything for data type name anything means anything like you can create data type with your name as well The only thing we have to keep in our mind is we have to follow the rules for defining any identifiers.

All the name defined by developer like union name variable name ,function name ,class name etc is known as identifier.

In the next line we are creating character array with name  'name' and size 20 and In the next line we are creating one integer type variable with name 'salary' and in the next linewe have declared one variable workno of type int(Integer).All this data type variable are combined and used as one user-defined data type variable.

In the next line we are creating object of type union_job data type with name u_job we can have have multiple instance u_job with anyname(because it is identifier).

After that we have did the same thing for structure which we have did for union.So I will skip that part.

In the next line of our program we have defined main function which will help are program to start the execution.In the first two line of our main function we are doing what we are intended to do so this program is for program to find to size of structure and union so we are doing that thing only the first two line of main function function.Let's understand more clearly.

printf("size of union=%d",sizeof(ujob));

In this line 'size of union' statement will be printed and after that we are using sizeof operator for finding the size of our data type.

 

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎   

  Previous Next

Post a Comment

Previous Post Next Post