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

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