1 / 10

Introduction to Computer Algorithmics and Programming Ceng 113

Introduction to Computer Algorithmics and Programming Ceng 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{

sissy
Download Presentation

Introduction to Computer Algorithmics and Programming Ceng 113

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. Introduction to Computer Algorithmics and ProgrammingCeng 113 Custom Data Types “Unions, Enumaration”

  2. 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;

  3. 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

  4. Unions union DWORD { unsigned char byte; unsigned int word; unsigned long dword; }; ... Union DWORD a; a.byte a.word a.dword

  5. 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]

  6. 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

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

  8. 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.

  9. 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

  10. 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

More Related