
Learn About Unions, Enums, and More in C Programming
Discover the concepts of unions and enums in C programming, including how unions can hold multiple variables and enums can represent a specific number of values. Understand the syntax, issues, and applications of these fundamental data types.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CS& 131, Autumn 2022 Lecture 31: recursion Thank you to Marty Stepp and Stuart Reges for parts of these slides 1
Unions Unions contain more than one variable that share the same space The size of the union is the size needed to hold the largest type You can only store one value in a union at a time You can store a struct that contains multiple values but only one struct Example: union number { }; int x; double y; 2
Using unions You can make a type out of a union, just like with a struct If you initialize a union with {} as with a struct, the value will always be assigned to the first variable declared in the struct. Both the following calls will store the number as an int as int is declared first in the struct: union number { int x; double y; }; union number value = { 10 }; union number value = { 1.43 }; 3
Enums Sometimes it is really useful to have a type that can represent a specific number of values For example, months There are 12 months, no more, no less If we represent them with strings or integers there are many illegal values that our month could get set to Enum - a set of integers represented with names enum months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; Think of an emun as a group of constants 4
Enum syntax enum name{ VALUE, VALUE, }; The values start at 0 and each increases by 1. You can start them at another number with the following syntax: enum name{ VALUE = number, VALUE, }; Example: enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; Since January starts at 1, February is 2, March is 3, etc. 5
Enum issues enum moves { ROCK, PAPER, ROCK}; // ERROR! All values must be unique. enum moves { ROCK, PAPER, SCISSORS}; moves[0] = WATER; // ERROR! You cannot alter an enum once it is created enum moves { rock, paper, scissors}; // Bad style! All value names should be in all caps like constants. Using lowercase won't cause an error but it is bad style 6
Puzzle How many people are ahead of you in a long queue (line)? 7