construction program in c

C Program to seek out the dimensions of Construction and Union                 

Programmer's Academy

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 foremost() {

    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 knowledge sort created utilizing construction and union

What’s Construction ? 

Construction is a user-defined knowledge sort in C Programming Language it creates user-defined through the use of a number of primitive knowledge  sort like int,char and so on.All construction ingredient are saved at contiguous reminiscence places.Principally it’s used to create complicated knowledge construction

What’s Union ?

It is extremely much like Construction Knowledge varieties like it’s also used to create user-defined knowledge sort with the mixture of various primitive knowledge sort.The one distinction between them is construction is assigned a novel reminiscence location whereas in union a reminiscence location is shared by all the info members

Key Distinction 

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

We have now to make use of struct key phrase to outline construction whereas to outline union we’ve got to make use of union key phrase solely.

Construction gives us the flexibility to initialize a number of objects without delay 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.

Execs and Cons of Construction

Execs of Construction

It’s straightforward to keep up and we will signify entire document through the use of a single identify.

We will use an array of construction to retailer extra data with related knowledge varieties

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

Cons of Construction 

It’s gradual as evaluate to union

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

If the complexity of Initiatives goes past the restrict it grow to be tougher to handle.

Execs and Cons of Union

Execs of Union

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

In union solely the final variable may be straight accessed.

After we use union, Its allotted area is the same as most measurement of the info member.

Cons of Union

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

You need to use just one union member at a time.

Union assigns one widespread space for storing for all its members

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

Within the first line of code we’ve got 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 knowledge sort with identify union_job  with the assistance of union key phrase.Most of you have to be pondering that why we’re utilizing union_job as knowledge sort identify ,So I want to inform them that we will use something for knowledge sort identify something means something like you may create knowledge sort along with your identify as nicely The one factor we’ve got to maintain in our thoughts is we’ve got to observe the foundations for outlining any identifiers.

All of the identify outlined by developer like union identify variable identify ,perform identify ,class identify and so on 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 sort variable with identify ‘wage’ and within the subsequent linewe’ve got declared one variable workno of sort int(Integer).All this knowledge sort variable are mixed and used as one user-defined knowledge sort variable.

Within the subsequent line we’re creating object of sort union_job knowledge sort 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 we’ve got did the identical factor for construction which we’ve got did for union.So I’ll skip that half.

Within the subsequent line of our program we’ve got outlined foremost perform which is able to assist are program to start out the execution.Within the first two line of our foremost perform we’re doing what we’re supposed 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 foremost 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 knowledge sort.

 

In case you any have doubt,suggestion concerning this put up or web site then be happy to share with us.

 

In case you have realized one thing new from this put up then share this put up with your loved ones and
pals.

 

Completely satisfied Studying :)?   

 
Earlier
Subsequent

(Visited 9 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
0
Would love your thoughts, please comment.x
()
x