Professional Documents
Culture Documents
Unit 5 - Vibha
Unit 5 - Vibha
USING C
UNIT -5
Vibha Masti
Feedbacklcorrectionsivibha@pesu.pes.edu
Union
•
not all members required
memory saved
•
•
used for sharing of memory
•
similar to structure
xyz l
Yhtar
union instead of
)
in union ,
int x
; 5
bytes ,
4 bytes are
datatype size)
union
xyz xy;
-
memory
Teri;¥:
y sharing
←
nations:*
x -
.
under the assumption that not all members will be used
shared
memory
•
Assigning
III: I}÷÷
y gets stored
su ] 256 c-
xyx 's
-
-
anarkali
"
2563 .
each byte :
- - - - - - - -
co -
25J )
(8-bit)
321=000000002 000000002 0000000£
010000010
l 65 o
-
C.32 bit)
-
"
T'
c allows
OUTPUT
321 65
Assigniagothervaluexy.is
ii. →g←
-
'
-
"
x =
346
"
T'
c allows
OUTPUT
346 90
Example
union l
xyz
) tiareianfes
int
xy
s
int:b;
-
.
-
double d;
float f
;
3;
union AD
xyz ;
f
..
'
Note:
•
even if union contains 5 chars and I int the
,
size of union variables =
4
bytes and not 5
•
if array structure stored inside union the
/ ,
intel
processors
registers e- 16 -
bit -
Ax -
I
BX AH AL
CX high low
DX
Demonstrationofregrsters
int al; -
3 a
'
I
-
} Al ;
( 64 bits
-
Structureandnion
Structure :
memory explicitly a uniquely allocated for
each field
Union :
memory allocated
for largest member field
fields share the
and
remaining allocated
memory
structures can be self -
referential but unions cannot
be
Bitfields
struct xyz l
int 4
x
; →
bytes ) 64 bib
int → 4
y; bytes total
is xy ;
of ints
Restricting size to save
space
struct
xyz l
unsigned int x: 4
;
4-bits
] to bits
unsigned int y :b
; co -
bits
"
} X o 15 (2 )
Xy ;
-
:
o 65 ( 2b )
y
'
-
-
25 to
IO l O O l
,
I 0 I 0
/ l l l
/
→
byte xy y byte xyx
-
;
-
.
IO; → allowed
xy x
-
.
-
25;
xy y
-
-
-
(
only for integers)
better in hexadecimal
note:
try
•
can be used anywhere in program
Advantage save memory
; Must
:
be careful