-4

I had the following C program

#include <stdio.h>

union student{
char name[20];
int id;
float percentage;
}student1;

int main()
{
    printf(" Enter name, id and then percentage \n");
    scanf("%s",student1.name);
    scanf("%d",&student1.id);
    scanf("%f",&student1.percentage);

    printf("Details of student are:");
    printf("Name %s, ID: %d, Percentage: %f", student1.name, student1.id, student1.percentage);
}

The output is as follows:

Enter name, id and then percentage  
Nik  
90  
7.9  
Details of student are:Name ═╠ⁿ@, ID: 1090309325, Percentage: 7.900000  
Process returned 0 (0x0)   execution time : 7.665 s  
Press any key to continue.  

The question is why this doesn't work for unions.

1 Answers1

1

What is wrong here.

Based on your example here, you need a structure, not a union.

From the C standard:

  • A structure type describes a sequentially allocated nonempty set of member objects (and, in certain circumstances, an incomplete array), each of which has an optionally specified name and possibly distinct type.
  • A union type describes an overlapping nonempty set of member objects, each of which has an optionally specified name and possibly distinct type.

In other words, a structure is a type consisting of a sequence of members, whose storage is allocated in an ordered sequence, and a union is a type consisting of a sequence of members whose storage overlap.

So, if you have to hold separate values, you use structure. For union, the members are overlapping, so they don't come with individual storage that the values can be stored and accessed individually.

To know more, google type punning.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • That should be a comment. And this doesnt answer the question. –  Oct 22 '20 at 11:24
  • 2
    This **is** the answer, and it's not a comment. – Sourav Ghosh Oct 22 '20 at 11:25
  • 1
    @NikhilSaxena If you use a wrong tool and someone suggests to use the right one, how does this not address your question? – Gerhardh Oct 22 '20 at 11:29
  • 1
    Oh, and a comment would be "Please choose your favourite C book and re-read the basic chapters about data types." – Sourav Ghosh Oct 22 '20 at 11:29
  • @Gerhardh Sometime folks cant accept the fact that the issue is something very primitive. Not every issue needs a complex solution. – Sourav Ghosh Oct 22 '20 at 11:30
  • @Gerhardh I don't disagree that this should be done using structures but my question is why this is not working for unions. This answer simply doesn't answers the question. –  Oct 22 '20 at 11:33
  • That was not your question, in first place. You never said you know how and where to use (or not to) structure or unions. – Sourav Ghosh Oct 22 '20 at 11:33
  • 1
    If you know what a union is, how should this ever work with unions? It is not even legal to read a member that was not the last member written. – Gerhardh Oct 22 '20 at 11:38
  • @NikhilSaxena Because your question is "I tossed my canary bird out the window from the 10th floor and it flew away. I tried the same with my cat but it crashed to the ground instead of flying. Why?" – Lundin Oct 22 '20 at 13:24