answersLogoWhite

0


Best Answer
Answer

quite a low level view required ......everything stored in memmory is in form of bits (i.e. a sequence of 01). It dependes upon how you are going to read a value. Same sequence of bits can give you different values..try printing x=10; printf("%c %d",x,x);


AnswerThis is the same problem as getting the previous value of a regular variable. For example:
int x = 5;
x = 2;

How do you get the previous value of x (5)? If you didn't store the value in another variable before overwriting it with 2, then you can't. The old value is overwritten and cannot be recovered. In a union, it's possible that you might overwrite only part of a previous value if you store a value to a smaller data type. For example:
union { long a; short b; } c;
c.a = 5;
c.b = 2;

It's possible that you can recover some of the old data of c.a, but for most practical purposes that value would be garbage (and it really depends on the exact alignment of the variables and byte endianness).

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: If the variables in a union use the same address so the previous value will be lost if a new value is entered how do you get the previous value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.


What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


How can the union member share memory?

All union members are allocated the same memory address. The amount of memory allocated is determined by the largest member of the union. Only one value can be stored in a union. If we change the value of any one member, we change the value for all members. The member type determines how the current value is interpreted.


What is anonymous union in c?

union is similar to structure .The distinction is in terms of storage. In structure each member has it own storage location but in union all the members use the same location. It can handle only one member at a time. example. union u { int a; char b[4]; }; A union may contain structure(s) and a structure may contain union(s). A structure (or an union) may contain unnamed unions (and structures), a useful feature and worth getting to know. Example: typedef union MYUNION { . struct { . . int x; . . int y; . } int_coord; . struct { . . double x; . . double y; . } dbl_coord; } MYUNION; It's a fragment of memory shared by multiple variables.


What is variable in c plus plus programming?

You declare a variable by first defining its data type and then its name followed by a semi-colon. Here is an example: int variable; The example above declares an uninitialized integer variable. You can initialize the variable by giving it a value such as "int variable = 1;". It is important to initialize your variables, because you can get errors when executing your program that the variable does not have a value or is uninitialized. Variables that are uninitialized have whatever garbage value happens to be when the program is executed. Here are all of the data types that a variable can be: *int - integer value *char - character value *bool - boolean value

Related questions

How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.


What day was Georgia entered into the union?

January 2, 1788 Georgia entered the Union.


What states entered the union between 1851 and 1876?

Which states entered the Union between 1851 and 1876


Which state entered the Union at the same time as Missouri?

None, Maine was the only state to join the union in 1820.


When was South Dakota entered in the Union?

South Dakota entered the Union and became a US state on November 2, 1889.


Which was the first state entered the union?

Which state entered the union in 1916 as an officially bilingual state?


Where is Guinness office in Cork Ireland?

It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.It is on Union Quay in Cork. The address is:Guinness House6 Union QuayCorkIreland.


Washington was what state in the union?

Washington was the 42nd state that entered into the union.


How many states entered the union during Richard Nixon's presidency?

No states entered the Union during the Presidency of Richard M. Nixon.


What are the dates and states that entered the Union while John Adams was President?

there are no states that entered the union while President John Adams was in office.


What year was Colorado entered in the union?

1876


What state entered the Union in 1876?

Colorado.