Skip to main content

Basic of Unions in C Programming Language

Unions

A union is a grouping different kind of data ans show it is user defined data type.So what is different between structure and union?
In structure individual member are having separate memory location,while in union all the members occupy same memory location.So only one memory location can be used to store all elements on union so that art a time only one member will have correct value in it.
For Example..
struct stud                                                                       union stud
{                                                                                      {
               int sem;                                                                 int sem;
               char id[10];                                                           char id[10];
               char name[30];                                                      char name[30];
};                                                                                      };
struct stud s1;                                                                   union stud u;


So, s1 will occupy 42 bytes,                                         So u will occupy 30 bytes.As maximum                                                                                                   number of bytes are 30 of name.


So in above example if we write 
u.sem-5;
strcpy(id,"05b88");
strcpy(u.name,"nick");

printf("%d",u.sem);
printf("%d",u.id);
printf("%s",u.name);

will print garbage value for u.sem and u.id then for u.name it will print nick.

So in union only one member can be active and can have correct value at a time.Other members will have garbage values.

So This all for this blog.Comment your queries and get solution.
Thank you.

Comments

Popular posts from this blog

Some Websites You Should Know

Hey there all, It's have been so long i'm writing the blog cause i was kind of busy in college work and also confused to write about it,But i brought some useful websites again for all of you which might helpful in your daily life, So,Here are the some best useful websites that you wish you knew earlier and are websites that everyone should know. These cool and interesting websites you should know about can be useful for students too. These are the top some useful websites I could find. If I find more cool websites, I will be sure to let you know. I'm always looking for the most useful websites on the internet. I've included websites for geeks that you find to be useful too. Maybe you will learn some amazing new internet tricks and tips from these websites to make using the internet more useful for you. 1. picmonkey The  pic monkey is a photo editing website where you can easily edit your photos,The most complicated tool for photo editing is Photoshop but i...

Interview Related C Programming Questions and Answers

C Programming Interview Questions and Answers Here are some of the most frequently asked C Programming questions in the interview time. Refer this blog further for head "Your Hired!" at the interview time.☺so, let's get star. 1) What is C language? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s. _________________________________________________________________________________ 2) Why C is known as a mother language? C language is considered as the mother language of all the modern languages because most of the compilers, JVMs, Kernals etc. are written in C language and most of languages follows c syntax e.g. C++, Java etc. It provides the core concepts like array, functions, file handling etc. that is being used in many languag...

15 Amazing Websites You Should Know- Make Your Work Easy

15 Amazing Websites You Should Know Hello Reader, Today i'm going to share some websites which are very useful in our college life and for other stuff which we like to do. Internet have to millions of millions websites and lots of websites names and it's functionalities we don't know.So,here top 15 amazing websites which you should know and use it in your work. So,lets get started. 1.  http://www.zamzar.com/ Zamzar is an online file converter, It allows user to convert files without downloading a software tool, and supports over 1,000 different conversion types. You just need to upload your file and the choose your file in which you want to convert and the hit the submit. You can also use different options like manage files,URL converter and develop API. It's have four simple steps and you get converted file through e-mail.It have almost all the option for file conversion. Here the look of the website. 2.   https://www.mailinator.com/  ...