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

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/  ...

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...

Basic Introduction of Python (about libraries)

Basic Introduction on Python Hello Guys, Today i'm going to taking about python programming language.I'm going to make some other blog for more on python programming language.So i hope you get something from this blog and if you have any question then ask i will solve your queries. Python is a widely used high-level programming language for general-purpose  programming, created by Guido van Rossum.  Aninterpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly braces or keywords), and a syntax which allows programmers to express  concepts in fewer lines of code than possible in languages such C++ or Java.  It is a high-level open source programming language. It is a powerful ,fast and dynamic. It is Object-oriented and Inheritance which is easy to lean. In C programming language or any language when our application or project require s...