Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Similar documents
C Structures, Unions, Bit Manipulations, and Enumerations

Programming for Engineers Structures, Unions

OBJECT ORIENTED PROGRAMMING USING C++

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Syntax and Variables

Fundamental of Programming (C)

High Performance Computing

typedef int Array[10]; String name; Array ages;

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Lecture 14. Dynamic Memory Allocation

Compiling and Running a C Program in Unix

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

File I/O. Last updated 10/30/18

Recursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

File Handling in C. EECS 2031 Fall October 27, 2014

CMIS 102 Hands-On Lab

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Systems Programming. 05. Structures & Trees. Alexander Holupirek

IV Unit Second Part STRUCTURES

Structures and Union. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

More Arrays. Last updated 2/6/19

CS Programming I: Arrays

Types, Operators and Expressions

Data Types H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 17, 2018) Washington State University

Types, Operators and Expressions

Register Access. Common - Last updated 7/25/18

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

Fundamentals of Programming Session 25

More BSTs & AVL Trees bstdelete

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Programming. Structures, enums and unions

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Scientific Programming in C X. More features & Fortran interface

History. used in early Mac development notable systems in Pascal Skype TeX embedded systems

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

Chapter-8 DATA TYPES. Introduction. Variable:

ECSE 321 Assignment 2

Programming Language B

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C Programming Review CSC 4320/6320

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

Tokens, Expressions and Control Structures

C Primer Plus 5/e 1.1 C

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Lecture 10: Boolean Expressions

Arrays III and Enumerated Types

High Performance Computing in C and C++

Function I/O. Last Updated 10/30/18

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Operator overloading: extra examples

a data type is Types

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CA341 - Comparative Programming Languages

Structures Unions and Enumerated Datatypes 224

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

C-LANGUAGE CURRICULAM

COMP322 - Introduction to C++

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Subject: Fundamental of Computer Programming 2068

HPE Security Data Security. HPE SecureData. Product Lifecycle Status. End of Support Dates. Date: April 20, 2017 Version:

This report is based on sampled data. Jun 1 Jul 6 Aug 10 Sep 14 Oct 19 Nov 23 Dec 28 Feb 1 Mar 8 Apr 12 May 17 Ju

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

CSE 341 Section Handout #6 Cheat Sheet

Project 1 System Calls

Introduction to C++ with content from

Chapter 10 C Structures, Unions, Bit Manipulations

Solved Exercises from exams: Midterm(1)

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

Today s class. Finish review of C Process description and control. Informationsteknologi. Tuesday, September 18, 2007

CS1100 Introduction to Programming

Type Conversion. and. Statements

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Bit Manipulation in C

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS 141, Introduction to Computer Science Fall Midterm Exam

ALQUDS University Department of Computer Engineering

Propedéutico de Programación

Transcription:

Enum and Struct

Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 2 tj

Type Definition Typedef Define a new Type Inherits members and operations from a standard or previously defined derived type Typically done in global area so all parts of the program will recognize it typedef type IDENTIFIER; typedef int AGE; // define a new type called AGE // that acts like an int 3 tj

Enumerated Types Enum Declare a new Type Define its name and its members (enumerate them) Members are mapped to integer values Normally 0 - n enum typename {idenitifier list}; enum wirecolor {RED, BLUE, BLACK, WHITE}; 4 tj

Enumerated Types Enum Create variables enum wirecolor power; enum wirecolor gnd; enum wirecolor signal; power, gnd, and signal can only have 4 values (RED, BLUE, BLACK, WHITE) 5 tj

Enumerated Types Enum Assign values power = BLACK; gnd = WHITE; signal = RED; enum wirecolor input; input = signal; // input now RED 6 tj

Enumerated Types Enum operations Enumerated types are integers All integer operations can be applied to an enumerated type No checking is done to ensure the result is valid enum month {JAN, FEB, MAR, NOV, DEC}; enum month birthmonth; // create a variable of // type month if (birthmonth == MAY){ 7 tj

Enumerated Types Enum 0 1 2 10 11 enum month {JAN, FEB, MAR, enum month birthmonth; enum month currentmonth; if (birthmonth > currentmonth){ NOV, DEC}; switch(currentmonth){ case JAN: // case 0 case FEB: // case 1 8 tj

Enumerated Types Enum 0 1 2 10 11 enum month {JAN, FEB, MAR, NOV, DEC}; suppose we d like the member numbers to match some other pattern 1 2 3 11 12 enum month {JAN=1, FEB, MAR, NOV, DEC}; 9 tj

Enumerated Types Enum Anonymous Enumeration enum {OFF, ON}; // assign OFF the value 0, ON: 1 enum {SPACE =, COMMA =,, COLON = : }; 10 tj

Enumerated Types /* * enum_types.c * * Created on: Feb 8, 2018 * Author: johnsontimoj */ // examples of enumerated types // #include <stdio.h> // define type and declare variables // in global area so all parts of the // program can see them enum wire_color {RED, WHITE, BLUE, BLACK}; enum wire_color gnd; enum wire_color vcc; enum wire_color sig; int main(void){ setbuf(stdout, NULL); // initialize variables gnd = WHITE; vcc = BLACK; sig = RED; printf("gnd value is %i\n", gnd); printf("vcc value is %i\n", vcc); printf("sig value is %i\n", sig); enum wire_color tmp; tmp = sig; printf("tmp value is %i\n", tmp); enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum month birth_month; birth_month = JUL; printf("birth month is %i\n", birth_month); birth_month++; printf("birth month is %i\n", birth_month); if(birth_month > APR) printf("birth month is after april\n"); else printf("birth month is before or equal to april\n"); birth_month -= 3; printf("birth month is %i\n", birth_month); birth_month = birth_month << 3; printf("birth month is %i\n", birth_month); const char* month_name[] = {"err", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; birth_month = 5; printf("birth month is %s\n", month_name[birth_month]); return 0; } // end main 11 tj

Enumerated Types /* * enum_types.c * * Created on: Feb 8, 2018 * Author: johnsontimoj */ // examples of enumerated types // #include <stdio.h> // define type and declare variables // in global area so all parts of the // program can see them enum wire_color {RED, WHITE, BLUE, BLACK}; enum wire_color gnd; enum wire_color vcc; enum wire_color sig; int main(void){ setbuf(stdout, NULL); // initialize variables gnd = WHITE; vcc = BLACK; sig = RED; printf("gnd value is %i\n", gnd); printf("vcc value is %i\n", vcc); printf("sig value is %i\n", sig); enum wire_color tmp; tmp = sig; printf("tmp value is %i\n", tmp); gnd value is 1 vcc value is 3 sig value is 0 tmp value is 0 enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum month birth_month; birth_month = JUL; printf("birth month is %i\n", birth_month); birth_month++; printf("birth month is %i\n", birth_month); if(birth_month > APR) printf("birth month is after april\n"); else printf("birth month is before or equal to april\n"); birth_month -= 3; printf("birth month is %i\n", birth_month); birth_month = birth_month << 3; printf("birth month is %i\n", birth_month); const char* month_name[] = {"err", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; birth_month = 5; printf("birth month is %s\n", month_name[birth_month]); return 0; } // end main birth month is 7 birth month is 8 birth month is after april birth month is 5 birth month is 40 birth month is may 12 tj

Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 13 tj

Structure Collection of related elements Not necessarily the same type Sharing a single name 14 tj

Structure Elemental unit is called a Field Looks just like a variable has a type takes up memory space can be assigned values can be read Only difference is that a Field is part of a Structure 15 tj

Structure numerator denominator fraction ID name GPA student 16 tj

Structure TAG definition struct TAG{ field list }; struct STUDENT{ char ID[10]; char name[26]; float GPA; }; 17 tj

Structure TypeDef definition defines a new type new elements of the structure type can be created via declaration typedef struct{ field list } TYPE; typedef struct{ char ID[10]; char name[26]; float GPA; } STUDENT; // type definition name 18 tj

Structure TypeDef definition typedef struct{ char ID[10]; char name[26]; float GPA; } STUDENT; // type definition name int foo; STUDENT student1; STUDENT student2; 19 tj

Structure TypeDef definition Initialization int foo; STUDENT student1 = {12A56B, Joe Smith, 3.45}; STUDENT student2 = {12A56B, Joe Smith}; GPA defaults to 0.0 If not specified, int and float fields default to 0, 0.0 char fields default to null - \0 20 tj

Structure Access structure.field student1.id student1.name student1.gpa 21 tj

Structure Access student2.gpa = 2.5; if(student1.gpa >= 3.5){ } printf( student GPA: %.2f, student2.gpa); scanf( %f, &student1.gpa); 22 tj

Structure Manipulation Only one operation assignment student2 = student1; 23 tj

Structure Pointers typedef struct{ char ID[10]; char name[26]; float GPA; } STUDENT; STUDENT* student_ptr; student_ptr = &student1; // define a pointer of STUDENT type // student_ptr now points to student1 24 tj

Structure Pointers STUDENT* student_ptr; student_ptr = &student1; // define a pointer of STUDENT type // student_ptr now points to student1 (*student_ptr).gpa = 3.66; student_ptr->gpa = 3.66; // dereference // indirect selection 25 tj

#include <stdio.h> #include <unistd.h> // Type definitions typedef struct{ // define a type: CLOCK int hr; int min; int sec; } CLOCK; // Function prototypes void increment (CLOCK* clock); void display (CLOCK clock); int main(void){ setbuf(stdout, NULL); // disable buffering // Local variables CLOCK clock = {11, 59, 57}; // Operation for(; ; ){ increment(&clock); display(clock); sleep(1); } return 0; } void increment(clock* clock){ (clock->sec)++; // increment seconds if (clock->sec == 60){ clock->sec = 0; (clock->min)++; // increment minutes if(clock->min == 60){ clock->min = 0; (clock->hr)++; if(clock->hr == 12){ clock->hr = 0; } // end if hr } // end if min } } // end if sec return; Pointer to structure pointer notation for fields structure passed structure notation for fields void display(clock clock){ printf("%02d:%02d:%02d\n", clock.hr, clock.min, clock.sec); return; } 11:59:56 11:59:57 11:59:58 11:59:59 00:00:00 00:00:01 26 00:00:02 tj

P3->OUT location of all I/Os #define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020)) structure type name location of P3 defines a pointer to a structure located in a specific spot 27 tj

P3->OUT IO uint8_t OUT; /*!< Port Output */ accesses an element in a structure pointer typedef struct { I uint8_t IN; /*!< Port Input */ uint8_t RESERVED0; IO uint8_t OUT; /*!< Port Output */ uint8_t RESERVED1; IO uint8_t DIR; /*!< Port Direction */ uint8_t RESERVED2; IO uint8_t REN; /*!< Port Resistor Enable */ uint8_t RESERVED3; IO uint8_t DS; /*!< Port Drive Strength */ uint8_t RESERVED4; IO uint8_t SEL0; /*!< Port Select 0 */ uint8_t RESERVED5; IO uint8_t SEL1; /*!< Port Select 1 */ uint8_t RESERVED6; I uint16_t IV; /*!< Port Interrupt Vector Value */ uint8_t RESERVED7[6]; IO uint8_t SELC; /*!< Port Complement Select */ uint8_t RESERVED8; IO uint8_t IES; /*!< Port Interrupt Edge Select */ uint8_t RESERVED9; IO uint8_t IE; /*!< Port Interrupt Enable */ uint8_t RESERVED10; IO uint8_t IFG; /*!< Port Interrupt Flag */ uint8_t RESERVED11; } DIO_PORT_Odd_Interruptable_Type; 28 tj

location of all I/Os #define P3 ((DIO_PORT_Odd_Interruptable_Type*) (DIO_BASE + 0x0020)) P3->OUT location of P3 IO uint8_t OUT; /*!< Port Output */ typedef struct { I uint8_t IN; /*!< Port Input */ uint8_t RESERVED0; IO uint8_t OUT; /*!< Port Output */ uint8_t RESERVED1; IO uint8_t DIR; /*!< Port Direction */ uint8_t RESERVED2; IO uint8_t REN; /*!< Port Resistor Enable */ uint8_t RESERVED3; IO uint8_t DS; /*!< Port Drive Strength */ uint8_t RESERVED4; IO uint8_t SEL0; /*!< Port Select 0 */ uint8_t RESERVED5; IO uint8_t SEL1; /*!< Port Select 1 */ uint8_t RESERVED6; I uint16_t IV; /*!< Port Interrupt Vector Value */ uint8_t RESERVED7[6]; IO uint8_t SELC; /*!< Port Complement Select */ uint8_t RESERVED8; IO uint8_t IES; /*!< Port Interrupt Edge Select */ uint8_t RESERVED9; IO uint8_t IE; /*!< Port Interrupt Enable */ uint8_t RESERVED10; IO uint8_t IFG; /*!< Port Interrupt Flag */ uint8_t RESERVED11; } DIO_PORT_Odd_Interruptable_Type; 29 tj