Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any.

Similar presentations


Presentation on theme: "Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any."— Presentation transcript:

1 Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any time, you should only read from one of them at a time—assigning a value to one of them overwrites the values in the others.

2 Defining Unions define a union using the union keyword followed by the declarations of the union's members, enclosed in braces. declare a variable—using the data type followed by one or more variable names separated by commas, and ending with a semicolon. Then end the union definition with a semicolon after the closing brace.

3 Unions 14 ???? tag name // Declare a union union int_or_float { int i;
float f; }; fields q i int int main() { union int_or_float q; q.i = 14; } 14 ???? f float 1/2/2019 3

4 Unions ???? 175.543 tag name // Declare a union union int_or_float {
int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = ; } ???? f float 1/2/2019

5 Unions 175 ???? tag name // Declare a union union int_or_float {
int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = ; q.i = q.f; } 175 ???? f float 1/2/2019 5

6 Unions ???? 175.543 tag name // Declare a union union int_or_float {
int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = ; } ???? f float 1/2/2019 6

7 Unions 175 ???? tag name // Declare a union union int_or_float {
int i; float f; }; fields q i int int main() { union int_or_float q; q.i = 14; q.f = ; q.i = q.f; } 175 ???? f float 1/2/2019 7

8 union numbers { int i; float f; }; That defines a union named numbers, which contains two members, i and f, which are of type int and float, respectively.

9 Declaring Union Variables at Definition
declare variables of a union type when you define the union type by putting the variable names after the closing brace of the union definition, but before the final semicolon union numbers { int i; float f; } first_number, second_number; That example declares two variables of type union numbers, first_number and second_number.

10 Declaring Union Variables After Definition
You can declare variables of a union type after you define the union by using the union keyword and the name you gave the union type, followed by one or more variable names separated by commas. union numbers { int i; float f; }; union numbers first_number, second_number; That example declares two variables of type union numbers, first_number and second_number.

11 Initializing Union Members
You can initialize the first member of a union variable when you declare it: union numbers { int i; float f; }; union numbers first_number = { 5 };

12 Another way to initialize a union member
Union numbers first_number = { f: }; union numbers first_number = { .f = }; union numbers { int i; float f; } first_number = { 5 };

13 Accessing Union Members
You can access the members of a union variable using the member access operator. union numbers { int i; float f; }; union numbers first_number; first_number.i = 5; first_number.f = 3.9; Notice in that example that giving a value to the f member overrides the value stored in the i member.

14 Size of Unions This size of a union is equal to the size of its largest member. Consider the first union example from this section: union numbers { int i; float f; }; The size of the union data type is the same as sizeof (float), because the float type is larger than the int type.

15 Example: #include<stdio
Example: #include<stdio.h> Void main() { union a int I; char ch[2]; }; union a key; key.i=512; Printf(“\n key.i=%d”,key.i); Printf(“\n key.ch[0]=%d”,key.ch[0]); Printf(“\n key.ch[1]=%d”,key.ch[1]); }

16 Output: Key. i=512 Key. ch[0]=0 Key
Output: Key.i=512 Key.ch[0]=0 Key.ch[1]=2 512 is an integer, a 2 byte number. Its binary equivalent will be CPU’s follow little endian format- low byte stored before high byte.

17 Example: #include<stdio
Example: #include<stdio.h> int main() { union a int i; char ch[2]; }; union a u; u.ch[0]=3; u.ch[1]=2; printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); return 0; } Output: 3, 2, 515

18

19 #include<stdio.h>
#include<conio.h> union marks { float perc; char grade; } void main () { union marks student1; student1.perc = 98.5; printf( "Marks are %f address is %16lu\n", student1.perc, &student1.perc); student1.grade = 'c'; printf("Grade is %c address is %16lu\n", student1.grade, &student1.grade);


Download ppt "Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any."

Similar presentations


Ads by Google