Enumeration Types and typedef

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
1 Lab Session-III CSIT121 Fall 2000 Setting Your Own Paths Setting Project Information Browse Information Generation Data types revisited Enumerated data.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
/3024/ SUN MON TUE WED THU FRI SAT JANUARY 2011 February 2011 SMTWTFS
프로그래밍 기초와 실습 Chapter 7 Enumeration Types and typedef.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
CS 1430: Programming in C++.
Enumerated Types Mr. Dave Clausen La Cañada High School.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
5 Day Forecast Mon Tues Wed Thu Fri.
Enumerated Data Types Data type created by programmer
Runestone Area Education District #
GANTT CHARTS Example Example Example Example text Tasks Example 1
Enumerations.
Instructor: Ioannis A. Vetsikas
Mon – 2Tue – 3Wed – 4Thu - 5Fri - 6Sat - 7Sun - 8 Mon – 9Tue – 10Wed – 11Thu - 12Fri – 13Sat – 14Sun -15 Mon – 16Tue – 17Wed – 18Thu - 19Fri – 20Sat –
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
Data Types Chapter 8.
Enumerations.
MON TUE WED THU
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
2017 Jan Sun Mon Tue Wed Thu Fri Sat
ANNUAL CALENDAR HOLIDAYS JANUARY FEBRUARY MARCH APRIL MAY JUNE
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Chapter 1: Introduction to Data Structures(8M)
Jan Sun Mon Tue Wed Thu Fri Sat
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
2008 Calendar.
S M T W F S M T W F
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
2 0 X X s c h e d u l e 1 MON TUE WED THU JANUARY 20XX FRI SAT SUN MEMO.
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Calendar
Calendar – 2010 (October, November & December)
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
January MON TUE WED THU FRI SAT SUN
S M T W F S M T W F
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
1 January MON TUE WED THU FRI SAT SUN MEMO 2 February MON TUE WED THU FRI SAT SUN.
Structures Chapter 4.
2008 Calendar.
S M T W F S M T W F
S M T W F S M T W F
1 January MON TUE WED THU FRI SAT SUN MEMO 2 February MON TUE WED THU FRI SAT SUN.
Presentation transcript:

Enumeration Types and typedef

enum Used to declare enumeration types. Allows to name a finite set and to declare identifiers, called enumerators.

Creates the user-defined type enum day. tag name constants of type int enum day {sun, mon, tue, wed, thu, fri, sat}; enum day d1, d2; … d1 = fri; if (d1 == d2) 1 6

Enumerators can be initialized. We can declare variables directly after the enumerator declaration. enum suit {clubs = 1, diamonds, hearts, spades} a, b, c; 2 3 4 enum fruit {apple = 7, pear, orange = 3, lemon} frt; 8 4

It is allowed, but the identifiers must be unique. enum veg {beet = 17, corn = 17} vege1, vege2; It is allowed, but the identifiers must be unique. enum {fir, pine} tree; The tag name need not be present. But no other variables of type enum {fir, pine} can be declared.

typedef An identifier can be associated with a specific type. typedef int color; color red, blue, green; The typedef facility allows the programmer to use type names that are appropriate for a specific application. Also, helps to control complexity when programmers are building complicated or lengthy user-defined types.

Example – Compute the next day enum day {sun, mon, tue, wed, thu, fri, sat}; typedef enum day day; day find_next_day(day d) { day next_day; switch (d) { case sun: next_day = mon; break; tag name type

case mon: next_day = tue; break; … case sat: next_day = sun; } return next_day;

Another Version cast enum day {sun, mon, tue, wed, thu, fri, sat}; typedef enum day day; day find_next_day(day d) { return((day) (((int) d + 1)%7)); } cast

Style Enumerators can be mnemonic. => Their use tends to be self-documenting. The use of enumeration types is considered good programming style. enum bool {false, true}; enum off_on {off, on}; enum no_yes {no, yes} enum speed {slow, fast} Put such declarations in header files. (good programming style)

Style All acceptable. Which one is used is a matter of personal taste. enum lo_hi {lo, hi}; typedef enum lo_hi lo_hi; All acceptable. Which one is used is a matter of personal taste. typedef enum lo_hi {lo, hi} lo_hi; typedef enum {lo, hi} lo_hi;