CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.

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
CMPE-013/L Expressions and Control
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
CMPE 293Gabriel Hugh Elkaim 1 CMPE 293: Intro to Feedback Control Design Gabriel Hugh Elkaim.
1 Lab Session-III CSIT121 Fall 2000 Setting Your Own Paths Setting Project Information Browse Information Generation Data types revisited Enumerated data.
Trey Mack James Moore Osa Osar-Emokpae
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Systems Programming Concepts
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012.
C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Pointers Gabriel Hugh Elkaim Spring 2012.
C++ Programming Lecture 10 Functions – Part II
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Computer Systems and “C” Programming Gabriel Hugh Elkaim Spring 2013.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Derived Types C allows a number of derived types: –Array - group of similar elements (same type) –Pointer - location of a variable –Enumerated - set of.
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.
Enumeration.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CS 1430: Programming in C++.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Exercises in MPLAB-X Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Enumerated Types Mr. Dave Clausen La Cañada High School.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Powerpoint Templates Page 1 Programming in C/C++ PS04CINS02.
1 dimensional static Array Int[] a = new int[4]; A[0]A[1]A[2]A[3] Int[] b= new int[1]; B[0] Array is a list of variables having same name and same data.
Programming Fundamentals Enumerations and Functions.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
Lecture 9 Symbol Table and Attributed Grammars
C++ Lesson 1.
LESSON 06.
Lecture 16: Introduction to Data Types
Enumeration Types and typedef
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Chapter 3: Understanding C# Language Fundamentals
C Arrays Systems Programming.
MON TUE WED THU
2008 Calendar.
Sun Mon Tue Wed Thu Fri Sat
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
2008 Calendar.
Presentation transcript:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 DefinitionEnumerations Enumerations: –Are unique integer data types –May only contain a specified list of values –Values are specified as symbolic constants Enumerations are integer data types that you can create with a limited range of values. Each value is represented by a symbolic constant that may be used in conjunction with variables of the same enumerated type.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax Enumerations How to Create an Enumeration Type enum weekday {SUN, MON, TUE, WED, THR, FRI, SAT}; enum typeName {label 0, label 1,…,label n } Where compiler sets label 0 = 0, label 1 = 1, label n = n Creates an ordered list of constants Each label’s value is one greater than the previous label SUN = 0, MON = 1, TUE = 2, WED = 3, THR = 4, FRI = 5, SAT = 6 Label Values:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Enumerations How to Create an Enumeration Type Any label may be assigned a specific value The following labels will increment from that value Syntax enum typeName {label 0 = const 0,…,label n } Where compiler sets label 0 = const 0, label 1 = ( const 0 + 1),... enum people {Rob, Steve, Paul = 7, Bill, Gary}; Rob = 0, Steve = 1, Paul = 7, Bill = 8, Gary = 9 Label Values:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax enum typeName {const-list} varname 1,…; Enumerations How to Declare an Enumeration Type Variable enum weekday {SUN, MON, TUE, WED, THR, FRI, SAT} today; enum weekday day; //day is a variable of type weekday Declared along with type: enum typeName varName 1,…,varName n ; Declared independently:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax enum {const-list} varName 1,…,varName n ; Enumerations How to Declare a ‘Tagless’ Enumeration Variable enum {SUN, MON, TUE, WED, THR, FRI, SAT} today; No type name specified: Only variables specified as part of the enum declaration may be of that type No type name is available to declare additional variables of the enum type later in code

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax typedef enum {const-list} typeName; Enumerations How to Declare an Enumeration Type with typedef typedef enum {SUN, MON, TUE, WED, THR, FRI, SAT} weekday; weekday day; //Variable of type weekday Variables may be declared as type typeName without needing the enum keyword The enumeration may now be used as an ordinary data type (compatible with int )

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Example Syntax varName = label n ; Enumerations How to Use an Enumeration Type Variable enum weekday {SUN, MON, TUE, WED, THR, FRI, SAT}; enum weekday day; day = WED; day = 6; //May only use values from 0 to 6 if (day == WED) { … If enumeration and variable have already been defined: The labels may be used as any other symbolic constant Variables defined as enumeration types must be used in conjunction with the type’s labels or equivalent integer

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 18 Enumerations

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 18 Enumerations Open the project’s workspace: 1 1 Open MPLAB ® and select Open Workspace… from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. /Examples/Lab18.zip -> Load “Lab18.mcw” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 18 Enumerations Compile and run the code: 2 2 Click on the Build All button. Compile (Build All) Run If no errors are reported, click on the Run button. Halt Click on the Halt button.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 18 Enumerations typedef enum {BANDSTOP, LOWPASS, HIGHPASS, BANDPASS} filterTypes; filterTypes filter; /*============================================================================ FUNCTION: main() ============================================================================*/ int main(void) { filter = BANDPASS; switch (filter) { case BANDSTOP: BandStopFilter(); break; case LOWPASS: LowPassFilter(); break; case HIGHPASS: HighPassFilter(); break; case BANDPASS: BandPassFilter(); break; } while(1); } Enum Definition and Use

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 18 Conclusions Enumerations provide a means of associating a list of constants with one or more variables Make code easier to read and maintain Variables declared as enum are essentially still int types

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Questions?

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013