1 / 19

Unions A union is a custom data type used for storing several variables in the same memory space.

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

keira
Download Presentation

Unions A union is a custom data type used for storing several variables in the same memory space.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_floatq; q.i = 14; } 14 ???? f float 9/15/2014 3

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

  5. Unions tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_floatq; q.i = 14; q.f = 175.543; q.i = q.f; } 175 ???? f float 9/15/2014 5

  6. Unions tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_floatq; q.i = 14; q.f = 175.543; } ???? 175.543 f float 9/15/2014 6

  7. Unions tag name // Declare a union union int_or_float { int i; float f; }; fields q i int int main() { union int_or_floatq; q.i = 14; q.f = 175.543; q.i = q.f; } 175 ???? f float 9/15/2014 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 Variablesat 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: 3.14159 }; union numbers first_number = { .f = 3.14159 }; 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.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.ch[1]=2 512 is an integer, a 2 byte number. Its binary equivalent will be 0000 0010 0000 0000 CPU’s follow little endian format- low byte stored before high byte.

  17. 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. #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); }

More Related