Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Similar documents
C Examples. Goals of this Lecture. Overview of this Lecture

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Goals of this Lecture

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

LESSON 4. The DATA TYPE char

Appendix A Developing a C Program on the UNIX system

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming. Lecture 11: C Characters and Strings

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Chapter 8. Characters and Strings

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

ASSIGNMENT 5 TIPS AND TRICKS

Strings. Steven R. Bagley

Fundamentals of Programming

Fundamentals of Programming (C)

Chapter 8 - Characters and Strings

Chapter 2 Bits, Data Types, and Operations

SWEN-250 Personal SE. Introduction to C

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Lecture (09) x86 programming 8

today cs3157-fall2002-sklar-lect05 1

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Simple Data Types in C. Alan L. Cox

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Fundamental Data Types

CS3157: Advanced Programming. Outline

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Do not start the test until instructed to do so!

Chapter 8: Character & String. In this chapter, you ll learn about;

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Number System

Oberon Data Types. Matteo Corti. December 5, 2001

Number Representations

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Number System (Different Ways To Say How Many) Fall 2016

Chapter 8 C Characters and Strings

Number Systems Base r

Tail recursion. Decision. Assignment. Iteration

C: How to Program. Week /May/28

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Chapter 3. Information Representation

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Contents. Preface. Introduction. Introduction to C Programming

Variables and data types

Strings and Library Functions

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

Midterm CSE 131 Winter 2013

Do not start the test until instructed to do so!

Midterm CSE 131 Winter 2012

Do not start the test until instructed to do so!

Connecting UniOP to Datalogic Barcode Readers

Midterm CSE 131 Fall 2014

LECTURE 15. String I/O and cstring library

Characters and Strings

Iteration. Side effects

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Appendix B. Comparison of Bourne Shell and C Shell Constructs. Bourne shell. C shell

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Midterm CSE 131 Winter 2015

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Midterm CSE 131 Winter 2014

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

COS 226 Algorithms and Data Structures Fall Final

C Libraries. Bart Childs Complementary to the text(s)

CSE 30 Fall 2012 Final Exam

Fundamentals of Computer Programming Using C

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Final CSE 131 Fall 2014

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Approximately a Test II CPSC 206

Positional Number System

VARIABLES AND CONSTANTS

Midterm Exam, Fall 2015 Date: October 29th, 2015

CSE 30 Fall 2007 Final Exam

The Binary Number System

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Transcription:

EE209: C Examples 1

Reminder Sign up for ee209 mailing list If you haven t received any email from ee209 yet Follow the link from our class homepage Precept 7:00-8:15pm, every Wednesday 창의학습관 (Creative Learning building), Room 308 New room has computers Be a Google chat friend with me kyoungsoo@gmail.com Ask any questions (assignments, C grammar, compile errors, ) For the last 3 days before each assignment deadline 2

Goals of this Lecture Help you learn about: The fundamentals of C Overall program structure, control statements, character I/O functions Deterministic finite state automata (DFA) Expectations for programming assignments Why? The fundamentals of C provide a foundation for the systematic coverage of C that will follow A power programmer knows the fundamentals of C well DFA are useful in many contexts (e.g. Assignment 1) How? Through some examples 3

Overview of this Lecture C programming examples Echo input to output Convert all lowercase letters to uppercase Convert first letter of each word to uppercase Glossing over some details related to pointers which will be covered subsequently in the course 4

Example #1: Echo Problem: Echo input directly to output Program design Include the Standard Input/Output header file (stdio.h) #include <stdio.h> Make declarations of I/O functions available to compiler Allow compiler to check your calls of I/O functions Define main() function int main(void) { int main(int argc, char *argv[]) { Starting point of the program, a standard boilerplate Hand-waving: argc and argv are for input arguments 5

Example #1: Echo (cont.) Program design (cont.) Read a single character c = getchar(); Read a single character from the standard input stream (stdin) and return it Write a single character putchar(c); Write a single character to the standard output stream (stdout) 6

Putting it All Together #include <stdio.h> int main(void) { int c; c = getchar(); putchar(c); return 0; Why int instead of char? Why return a value? 7

Read and Write Ten Characters #include <stdio.h> int main(void) { int c, i; for (i=0; i<10; i++) { c = getchar(); putchar(c); Loop to repeat a set of lines (e.g., for loop) Three expressions: initialization, condition, and increment E.g., start at 0, test for less than 10, and increment per iteration return 0; Why not this instead: for (i = 1; i <= 10; i++) 8

Read and Write Forever Infinite for loop Simply leave the expressions blank E.g., for ( ; ; ) #include <stdio.h> int main(void) { int c; for ( ; ; ) { c = getchar(); putchar(c); return 0; When will this be executed? How would you terminate this program? 9

Read and Write Until End-Of-File Test for end-of-file EOF is a global constant, defined in stdio.h (#define EOF (-1) ) The break statement jumps out of the innermost enclosing loop #include <stdio.h> int main(void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; putchar(c); return 0; before the loop do some stuff done yet? do more stuff after the loop 10

Many Ways to Do the Same Job for (c=getchar(); c!=eof; c=getchar()) putchar(c); Which approach is best? while ((c=getchar())!=eof) putchar(c); for (;;) { c = getchar(); if (c == EOF) break; putchar(c); Typical idiom in C, but messy side-effect in loop test c = getchar(); while (c!=eof) { putchar(c); c = getchar(); 11

Review of Example #1 Character I/O Including stdio.h Functions getchar() and putchar() Representation of a character as an integer Predefined constant EOF Program control flow The for and while statements The break statement The return statement Operators Assignment operator: = Increment operator: ++ Relational operator to compare for equality: == Relational operator to compare for inequality:!= 12

Example #2: Convert Uppercase Problem: Write a program to convert a file to all uppercase Leave non-alphabetic characters alone Program design: repeat Read a character If unsuccessful, break out of loop If the character is lower-case, convert to upper-case Write the character 13

ASCII American Standard Code for Information Interchange 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 32 SP! " # $ % & ' ( ) * +, -. / 48 0 1 2 3 4 5 6 7 8 9 : ; < = >? 64 @ A B C D E F G H I J K L M N O 80 P Q R S T U V W X Y Z [ \ ] ^ _ 96 ` a b c d e f g h i j k l m n o 112 p q r s t u v w x y z { ~ DEL Lower case: 97-122 and upper case: 65-90 E.g., a is 97 and A is 65 (i.e., 32 apart) 14

Implementation in C #include <stdio.h> int main(void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; if ((c >= 97) && (c < 123)) c -= 32; putchar(c); return 0; 15

That s a B-minus A good program is: Clean Readable Maintainable It s not enough that your program works! We take this seriously in EE 209 16

Avoid Mysterious Numbers #include <stdio.h> int main(void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; if ((c >= 97) && (c < 123)) c -= 32; putchar(c); return 0; Ugly; ASCII only 17

Improvement: Character Constants #include <stdio.h> int main(void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; if ((c >= a ) && (c <= z )) c += A - a ; putchar(c); return 0; Better; but assumes that alphabetic character codes are contiguous 18

Improvement: Existing Functions Standard C Library Functions ctype(3c) NAME ctype, isdigit, isxdigit, islower, isupper, isalpha, isalnum, isspace, iscntrl, ispunct, isprint, isgraph, isascii - character handling SYNOPSIS #include <ctype.h> int isalpha(int c); int isupper(int c); int islower(int c); int isdigit(int c); int isalnum(int c); int isspace(int c); int ispunct(int c); int isprint(int c); int isgraph(int c); int iscntrl(int c); int toupper(int c); int tolower(int c); DESCRIPTION These macros classify charactercoded integer values. Each is a predicate returning non-zero for true, 0 for false... The toupper() function has as a domain a type int, the value of which is representable as an unsigned char or the value of EOF... If the argument of toupper() represents a lower-case letter... the result is the corresponding upper-case letter. All other arguments in the domain are returned unchanged. 19

Using the ctype Functions #include <stdio.h> #include <ctype.h> int main(void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; if (islower(c)) c = toupper(c); putchar(c); return 0; Returns non-zero (true) iff c is a lowercase character 20

Building and Running % ls upper.c % gcc209 upper.c o upper % ls upper upper.c % upper We ll be on time today! WE LL BE ON TIME TODAY! ^D % 21

Run the Code on Itself % upper < upper.c #INCLUDE <STDIO.H> #INCLUDE <CTYPE.H> INT MAIN(VOID) { INT C; FOR ( ; ; ) { C = GETCHAR(); IF (C == EOF) BREAK; IF (ISLOWER(C)) C = TOUPPER(C); PUTCHAR(C); RETURN 0; 22

Output Redirection % upper < upper.c > junk.c % gcc209 junk.c o junk junk.c:1:2: invalid preprocessing directive #INCLUDE junk.c:2:2: invalid preprocessing directive #INCLUDE junk.c:3: syntax error before "MAIN" etc... 23

Review of Example #2 Representing characters ASCII character set Character constants (e.g., A or a ) Manipulating characters Arithmetic on characters Functions like islower() and toupper() Compiling and running C code Compile to generate executable file Invoke executable to run program Can redirect stdin and/or stdout 24

Example #3: Capitalize First Letter Capitalize the first letter of each word ee 209 is fun Ee 209 Is Fun Sequence through the string, one letter at a time Print either the character, or the uppercase version Challenge: need to remember where you are Capitalize e in ee, but not o or c in rocks Solution: keep some extra information around Whether you ve encountered the first letter in the word 25

Deterministic Finite Automaton Actions are not Deterministic Finite Automaton (DFA) part of DFA formalism; but they re helpful letter (print uppercase equivalent) not-letter (print) 1 not-letter 2 (print) letter (print) States Transitions labeled by characters (or categories) Optionally, transitions labeled by actions 26

Implementation Skeleton #include <stdio.h> #include <ctype.h> int main (void) { int c; for ( ; ; ) { c = getchar(); if (c == EOF) break; <process one character> return 0; 27

Implementation <process one character> = switch (state) { case 1: <state 1 action> break; case 2: <state 2 action> break; default: <this should never happen> not-letter letter letter 1 2 not-letter if (isalpha(c)) { putchar(toupper(c)); state = 2; else putchar(c); if (!isalpha(c)) state = 1; putchar(c); 28

Complete Implementation #include <stdio.h> #include <ctype.h> int main(void) { int c; int state=1; for ( ; ; ) { c = getchar(); if (c == EOF) break; switch (state) { case 1: if (isalpha(c)) { putchar(toupper(c)); state = 2; else putchar(c); break; case 2: if (!isalpha(c)) state = 1; putchar(c); break; return 0; 29

Running Code on Itself % gcc209 upper1.c -o upper1 % upper1 < upper1.c #Include <Stdio.H> #Include <Ctype.H> Int Main(Void) { Int C; Int State=1; For ( ; ; ) { C = Getchar(); If (C == EOF) Break; Switch (State) { Case 1: If (Isalpha(C)) { Putchar(Toupper(C)); State = 2; Else Putchar(C); Break; Case 2: If (!Isalpha(C)) State = 1; Putchar(C); Break; Return 0; 30

OK, That s a B Works correctly, but Mysterious integer constants ( magic numbers ) What now? States should have names, not just 1, 2 31

Improvement: Names for States Define your own named constants enum Statetype {NORMAL,INWORD; Define an enumeration type enum Statetype state; Define a variable of that type 32

Improvement: Names for States #include <stdio.h> #include <ctype.h> enum Statetype {NORMAL,INWORD; int main(void) { int c; enum Statetype state = NORMAL; for ( ; ; ) { c = getchar(); if (c == EOF) break; switch (state) { case NORMAL: if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else putchar(c); break; case INWORD: if (!isalpha(c)) state = NORMAL; putchar(c); break; return 0; 33

OK, That s a B+ Works correctly, but No modularity What now? Should handle each state in a separate function 34

Improvement: Modularity #include <stdio.h> #include <ctype.h> enum Statetype {NORMAL,INWORD; enum Statetype handlenormalstate(int c) {... enum Statetype handleinwordstate(int c) {... int main(void) { int c; enum Statetype state = NORMAL; for ( ; ; ) { c = getchar(); if (c == EOF) break; switch (state) { case NORMAL: state = handlenormalstate(c); break; case INWORD: state = handleinwordstate(c); break; return 0; 35

Improvement: Modularity enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { putchar(c); state = NORMAL; return state; 36

Improvement: Modularity enum Statetype handleinwordstate(int c) { enum Statetype state; putchar(c); if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; 37

OK, That s an A- Works correctly, but No comments What now? Should add (at least) function-level comments 38

Function Comments A function s comment should: Describe what the function does Describe input to the function Parameters, input streams Describe output from the function Return value, output streams Not describe how the function works 39

Function Comment Examples Bad main() function comment Read a character from stdin. Depending upon the current DFA state, pass the character to an appropriate state-handling function. The value returned by the state-handling function is the next DFA state. Repeat until end-of-file. Describes how the function works Good main() function comment Read text from stdin. Convert the first character of each "word" to uppercase, where a word is a sequence of letters. Write the result to stdout. Return 0. Describes what the function does from caller s point of view 40

An A Effort #include <stdio.h> #include <ctype.h> enum Statetype {NORMAL, INWORD; /*------------------------------------------------------------*/ /* handlenormalstate: Implement the NORMAL state of the DFA. */ /* c is the current DFA character. Return the next state. */ /*------------------------------------------------------------*/ enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { putchar(c); state = NORMAL; return state; 41

An A Effort /*------------------------------------------------------------*/ /* handleinwordstate: Implement the INWORD state of the DFA. */ /* c is the current DFA character. Return the next state. */ /*------------------------------------------------------------*/ enum Statetype handleinwordstate(int c) { enum Statetype state; putchar(c); if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; 42

An A Effort /*------------------------------------------------------------*/ /* main: Read text from stdin. Convert the first character */ /* of each "word" to uppercase, where a word is a sequence of */ /* letters. Write the result to stdout. Return 0. */ /*------------------------------------------------------------*/ int main(void) { int c; enum Statetype state = NORMAL; /* Use a DFA approach. state indicates the state of the DFA. */ for ( ; ; ) { c = getchar(); if (c == EOF) break; switch (state) { case NORMAL: state = handlenormalstate(c); break; case INWORD: state = handleinwordstate(c); break; return 0; 43

Review of Example #3 Deterministic finite state automaton Two or more states Transitions between states Next state is a function of current state and current character Actions can occur during transitions Expectations for EE 209 assignments Readable Meaningful names for variables and values Modular Multiple functions, each of which does one well-defined job Function-level comments Should describe what function does See K&P book for style guidelines specification 44

Another DFA Example Does the string have nano in it? banano yes nnnnnnnanofff yes banananonano yes bananananashanana no n n n a n o S 1 2 3 a F 45

Yet Another DFA Example Identify whether or not a string is a floating-point number Valid numbers -34 78.1 +298.3-34.7e-1 34.7E-1 7..7 999.99e99 Invalid numbers abc -e9 1e + 17.9A 0.38+. 38.38f9 46

Summary Examples illustrating C Overall program structure Control statements (if, while, for, and switch) Character input/output (getchar() and putchar()) Deterministic finite state automata (i.e., state machines) Expectations for programming assignments 47