Tuesday, September 27, 2022
HomeC Programmingconstruction program in c

construction program in c


C Program to seek out the dimensions of Construction and Union                 

Code: 

#embrace <stdio.h>

union union_job

{

    char identify[20];

    int wage;

    int workerno;

}

ujob;

struct structure_job

{

    char identify[30];

    int wage;

    int workerno;

}sjob;

int essential() {

    printf(“measurement of union=%d,sizeof(ujob));

    printf(nmeasurement of Construction=%d,sizeof(sjob));

    return 0;

}

 

Pattern Enter and Output: 

measurement of union=20

measurement of Construction=40

 On this article,we’re going to see how we will program to seek out the dimensions of user_defined information kind created utilizing construction and union

What’s Construction ? 

Construction is a user-defined information kind in C Programming Language it creates user-defined through the use of a number of primitive information  kind like int,char and many others.All construction factor are saved at contiguous reminiscence areas.Principally it’s used to create complicated information construction

What’s Union ?

It is extremely just like Construction Knowledge sorts like additionally it is used to create user-defined information kind with the mixture of various primitive information kind.The one distinction between them is construction is assigned a singular reminiscence location whereas in union a reminiscence location is shared by all the information members

Key Distinction 

Construction assist versatile array whereas union doesn’t assist versatile array.

Now we have to make use of struct key phrase to outline construction whereas to outline union now we have to make use of union key phrase solely.

Construction offers us the flexibility to initialize a number of objects directly whereas union permit us to solely  initialize solely the primary member of union

We can retrieve any member at a time in construction and in union we can entry one member at a time.

Professionals and Cons of Construction

Professionals of Construction

It’s simple to take care of and we will symbolize entire file through the use of a single identify.

We will use an array of construction to retailer extra information with comparable information sorts

In construction, we will cross full set of information to any perform utilizing a single parameter.

Cons of Construction 

It’s gradual as examine to union

we will retrieve any member at a time in construction whereas you possibly can entry one member at a time within the union.

If the complexity of Tasks goes past the restrict it change into more durable to handle.

Professionals and Cons of Union

Professionals of Union

Once we use union it occupies much less reminiscence in comparison with construction

In union solely the final variable may be straight accessed.

Once we use union, Its allotted house is the same as most measurement of the information member.

Cons of Union

In Union assigns one frequent space for storing for all its members.

You should utilize just one union member at a time.

Union assigns one frequent space for storing for all its members

So Now now we have understood what’s construction and union theoretically or we will use it definition and professionals and cons so now Let’s soar to the coding half

Within the first line of code now we have included stdio header file which is able to assist us to make use of printf() and scanf() for use.

Within the subsequent line of code we’re creating one user_defined information kind with identify union_job  with the assistance of union key phrase.Most of you should be pondering that why we’re utilizing union_job as information kind identify ,So I wish to inform them that we will use something for information kind identify something means something like you possibly can create information kind along with your identify as nicely The one factor now we have to maintain in our thoughts is now we have to comply with the foundations for outlining any identifiers.

All of the identify outlined by developer like union identify variable identify ,perform identify ,class identify and many others is named identifier.

Within the subsequent line we’re creating character array with identify  ‘identify’ and measurement 20 and Within the subsequent line we’re creating one integer kind variable with identify ‘wage’ and within the subsequent linenow we have declared one variable workno of kind int(Integer).All this information kind variable are mixed and used as one user-defined information kind variable.

Within the subsequent line we’re creating object of kind union_job information kind with identify u_job we will have have a number of occasion u_job with anyname(as a result of it’s identifier).

After that now we have did the identical factor for construction which now we have did for union.So I’ll skip that half.

Within the subsequent line of our program now we have outlined essential perform which is able to assist are program to start out the execution.Within the first two line of our essential perform we’re doing what we’re meant to take action this program is for program to seek out to measurement of construction and union so we’re doing that factor solely the primary two line of essential perform perform.Let’s perceive extra clearly.

printf(“measurement of union=%d”,sizeof(ujob));

On this line ‘measurement of union’ assertion shall be printed and after that we’re utilizing sizeof operator for locating the dimensions of our information kind.

 

When you any have doubt,suggestion relating to this put up or web site then be at liberty to share with us.

 

When you have realized one thing new from this put up then share this put up with your loved ones and
associates.

 

Comfortable Studying :)😎   

 
Earlier
Subsequent

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments