Introduction to Unions and Enumerations in C Programming
This document provides a comprehensive overview of unions and enumerations in C programming. It explains how unions allow multiple data types to occupy the same memory location and illustrates their usage with examples. Additionally, it covers enumerations, which define named integer constants, helping to specify legal variable values. The document includes practical code snippets demonstrating the implementation of unions and enumerations, making it a great resource for understanding these essential data types in C.
Introduction to Unions and Enumerations in C Programming
E N D
Presentation Transcript
Introduction to Computer Algorithmics and ProgrammingCeng 113 Custom Data Types “Unions, Enumaration”
Unions A union is a memory location that is shared by two or more different variables, generally different types, at different times. Its general form is; union tag{ type variable_name; type variable_name; …. } union_variables;
Unions union u_type { int i; char ch; } ; union u_type c; In union cnvt, both integer i and character ch share the same memory location. Byte 0 Byte 1 c.i = 10; ch i
Unions union DWORD { unsigned char byte; unsigned int word; unsigned long dword; }; ... Union DWORD a; a.byte a.word a.dword
Unions union DBL_FORM { double n; unsigned char s[8]; }; ... Union DBL_FORM x; x.s[0] x.s[1] x.s[2] x.n x.s[0]...x.s[7] x.s[7]
Unions struct WORD { unsigned char low_byte; unsigned char high_byte; }; union WORD_FORM { unsigned int x; struct WORD y; }; union WORD_FORM wf; wf.y.low_byte = 0x12; wf.y.high_byte = 0x34; printf(“%x \n”, wf.y); wf.y.low_byte 0x12 wf.x 0x34 wf.y.high_byte
Unions #include <stdio.h> #include <stdio.h> #include <string.h> union pw { unsigned int i; char ch[2]; }word; void main(void) { word.i = 512; // 2^8 = (00000010 00000000)2 printf("\n word.ch[0] = %d \n", word.ch[0]); printf("\n word.ch[1] = %d \n", word.ch[1]); }
Enumaration An enumaration is a set of named integer constants that specify all the legal values a variable of that type may have. The general form is; enum tag {enumaration list } variable_list; Example; enum coin { penny, nickel, dime, quarter, half_dollar, dollar}; enum coin money; The key point to understand about an enumaration is that each of the symbols stands for an integer value. The value of the first enumaration symbol is 0. Therefore; printf(“%d %d”, penny, dime); displays 0 and 2.
Enumaration enum coin { penny, nickel, dime, quarter=100, half_dollar, dollar}; Now, the values of these symbols are; penny 0 nickel 1 dime 2 quarter 100 half_dollar 101 dollar 102
Enumaration enum coin { penny, nickel, dime, quarter, half_dollar, dollar}; enum coin money; … switch(money) { case penny: printf(“penny”); break; case nickel: printf(“nickel”); break; case dime: printf(“dime”); break; case quarter:printf(“quarter”); break; case half_dollar:printf(“half_dollar); break; case dollar: printf(“dollar”); } enum coin { penny, nickel, dime, quarter, half_dollar, dollar}; enum coin money; .. char name[ ]= { “penny”, “nickel”, “dime”, “quarter”, “half_dollar”, “dollar” }; … printf(“%s”, name[money]); Enum_Sample.cpp