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

Similar documents
C Examples! Jennifer Rexford!

Memory is implemented as an array of electronic switches

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

ASCII Code. Numerous codes were invented, including Émile Baudot's code (known as Baudot

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Sources: On the Web: Slides will be available on:

BARCODE READER V 2.1 EN USER MANUAL

The C Programming Language course syllabus associate level

So far we have considered only numeric processing, i.e. processing of numeric data represented

Voyager 9520/40 Voyager GS9590 Eclipse 5145

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

BAR CODE 39 ELFRING FONTS INC.

DEBT COLLECTION SYSTEM ACCOUNT SUBMISSION FILE

The ASCII Character Set

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Symbols in subject lines. An in-depth look at symbols

BI-300. Barcode configuration and commands Manual

Xi2000 Series Configuration Guide

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Chapter 1. Binary, octal and hexadecimal numbers

URL encoding uses hex code prefixed by %. Quoted Printable encoding uses hex code prefixed by =.

CHAPTER 8 BAR CODE CONTROL

Chapter 5. Binary, octal and hexadecimal numbers

Command Emulator STAR Line Mode Command Specifications

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

DL910 SERIES. Instruction Manual

Characters & Strings Lesson 1 Outline

TELOCATOR ALPHANUMERIC PROTOCOL (TAP)

Create!form Barcodes. User Guide

Tutorial on C Language Programming

File Handling. What is a file?

Representação de Caracteres

ASCII CODES WITH GREEK CHARACTERS

Model 200 / 250 / 260 Programming Guide

System Calls and Standard I/O

Part No. : MUL PROGRAMMING GUIDE

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Operating Systems. Privileged Instructions

Barcode Magstripe. Decoder & Scanner. Programming Manual

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

/* File: blkcopy.c. size_t n

C Strings and Pointers

7-Bit coded Character Set

Informatica e Sistemi in Tempo Reale

The programming language C. sws1 1

CP Lab 2: Writing programs for simple arithmetic problems

Talk Encoding Issues. An overview to understand and be able to handle encoding issues in a better way. Susanne Ebrecht

1 Abstract Data Types Information Hiding

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

ESPA Nov 1984 PROPOSAL FOR SERIAL DATA INTERFACE FOR PAGING EQUIPMENT CONTENTS 1. INTRODUCTION 2. CHARACTER DESCRIPTION

El Dorado Union High School District Educational Services

Teletypewriter Communication Codes

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How To Write Portable Programs In C

Barcode Scanning Made Easy. WWS500 Programming Guide

BRMO 80 / ETH-IP. User Manual. Réf : MU-BRMO 80-ETH-IP-1.4-EN

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

S302D. Programming Guide. 2D Imaging Barcode Scanner. Advanced Handheld High-Speed Laser Scanner

Barcode reader setup manual


MK-SERIE 1000/1500/2000 AllOfBarcode.de Michael Krug Traunstein BARCODE SCANNER

INTERNATIONAL STANDARD

1 Description of The Simpletron

C Compiler Targeting the Java Virtual Machine

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Barcode Scanning Made Easy. Programming Guide

HANDHELD LASER SCANNER

Enter/Exit programming

MINIMAG. Magnetic Stripe Reader Keyboard Wedge. User s Manual

Java Interview Questions and Answers

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Project 2: Bejeweled

Simple C Programs. Goals for this Lecture. Help you learn about:

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Chapter 13 - The Preprocessor

Decision Logic: if, if else, switch, Boolean conditions and variables

Digital Logic Design. Introduction

Compiler Construction

Scanner Configuration

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Regular Expressions and Automata using Haskell

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

An Incomplete C++ Primer. University of Wyoming MA 5310

Index...1. Introduction...3. Installation- Keyboard Wedge...3 RS USB...3. Default Setting for each barcode shown as below:...

Security Protection of Software Programs by Information Sharing and Authentication Techniques Using Invisible ASCII Control Codes

C++ Programming Language

Pemrograman Dasar. Basic Elements Of Java

PHP Debugging. Draft: March 19, Christopher Vickery

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

Scanner. tokens scanner parser IR. source code. errors

ESC/POS Command Specifications

FEEG Applied Programming 5 - Tutorial Session

ASCII control characters (character code 0-31)

Applied Data Communication Lecture 14

ASCII Characters. 146 CHAPTER 3 Information Representation. The sign bit is 1, so the number is negative. Converting to decimal gives

Computer Programming Tutorial

Transcription:

Overview of Today s Lecture Lecture 2: Character Input/Output in C Prof. David August COS 217 http://www.cs.princeton.edu/courses/archive/fall06/cos217/ Goals of the lecture o Important C constructs Program flow (if/else, loops, and switch) Character input/output (getchar and putchar) o Deterministic finite automata (i.e., state machine) o Expectations for programming assignments C programming examples 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 Glossing over some details related to pointers o which will be covered in the next lecture 1 2 Echo Input Directly to Output Including the Standard Input/Output (stdio) library o Makes names of functions, variables, and macros available o Defining procedure main() o Starting point of the program, a standard boilerplate o int main(void) o int main(int argc, char **argv) o Hand-waving: argc and argv are for input arguments Read a single character o Returns a single character from the text stream standard in (stdin) o Write a single character o Writes a single character to standard out (stdout) Putting it All Together Why an int? Why a return value? o 3 4

Why is the Character an int Read and Write Ten Characters Meaning of a data type o Determines the size of a variable o and how it is interpreted and manipulated Loop to repeat a set of lines (e.g., for loop) o Three arguments: initialization, condition, and re-initialization o E.g., start at 0, test for less than 10, and increment per iteration Difference between char and int o char: character, a single byte o int: integer, machine-dependent (e.g., -32,768 to 32,767) One byte is just not big enough o Need to be able to store any character o plus, special value like End-Of-File (typically -1 ) o We ll see an example with EOF in a few slides int c, i; for (i=0; i<10; i++) { 5 6 Read and Write Forever Read and Write Till End-Of-File Infinite for loop o Simply leave the arguments blank o E.g., for ( ; ; ) Test for end-of-file (EOF) o EOF is a special global constant, defined in stdio o The break statement jumps out of the current scope if (c == EOF) before the loop do some stuff done yet? do more stuff 7 after the loop 8

Many Ways to Say the Same Thing Review of Example #1 for (c=getchar(); c!=eof; c=getchar()) while ((c=getchar())!=eof) for (;;) { if (c == EOF) Very typical idiom in C, but messy side-effects in loop test while (c!=eof) { Character I/O o Including stdio.h o Functions getchar() and putchar() o Representation of a character as an integer o Predefined constant EOF Program control flow o The for loop and while loop o The break statement o The return statement Assignment and comparison o Assignment: = o Increment: i++ o Comparing for equality == o Comparing for inequality!= 9 10 Example #2: Convert Upper Case Problem: write a program to convert a file to all upper-case (leave nonalphabetic characters alone) Program design: repeat read a character if it s lower-case, convert to upper-case write the character until end-of-file 11 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) 12

Implementation in C That s a B-minus if (c == EOF) if ((c >= 97) && (c < 123)) c -= 32; 13 Programming well means programs that are o Clean o Readable o Maintainable It s not enough that your program works! o We take this seriously in COS 217. 14 Avoid Mysterious Numbers Correct, but ugly to have all these hardwired constants in the program. if (c == EOF) if ((c >= 97) && (c < 123)) c -= 32; 15 Improvement: Character Literals if (c == EOF) if ((c >= a ) && (c <= z )) c += A - a ; 16

Improvement: Existing Libraries Standard C Library Functions ctype(3c) NAME ctype, isdigit, isxdigit, islower, isupper, isalpha, isalnum, isspace, iscntrl, ispunct, isprint, isgraph, isascii - character handling SYNOPSIS 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. 17 Using the ctype Library if (c == EOF) if (islower(c)) c = toupper(c); Returns 1 (true) if c is between a and z 18 Compiling and Running % ls get-upper.c % gcc get-upper.c % ls a.out get-upper.c % a.out We ll be on time today! WE LL BE ON TIME TODAY! ^D % 19 Run the Code on Itself % a.out < get-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; 20

Output Redirection % a.out < get-upper.c > test.c % gcc test.c test.c:1:2: invalid preprocessing directive #INCLUDE test.c:2:2: invalid preprocessing directive #INCLUDE test.c:3: syntax error before "MAIN" etc... Review of Example #2 Representing characters o ASCII character set o Character constants (e.g., A or a ) Manipulating characters o Arithmetic on characters o Functions like islower() and toupper() Compiling and running C code o Compile to generate a.out o Invoke a.out to run program o Can redirect stdin and/or stdout 21 22 Example #3: Capitalize First Letter Deterministic Finite Automaton (DFA) not-letter letter 1 2 not-letter letter State #1: before the 1 st letter of a word State #2: after the 1 st letter of a word Capitalize on transition from state 1 to 2 Implementation Skeleton int main (void) { if (c == EOF) <process one character> cos 217 rocks Cos 217 Rocks 23 24

Implementation <process one character> = case 1: <state 1 action> case 2: <state 2 action> default: <this should never happen> not-letter letter letter 1 2 not-letter if (isalpha(c)) { putchar(toupper(c)); state = 2; else if (!isalpha(c)) state = 1; 25 Complete Implementation int state=1; if (c == EOF) case 1: if (isalpha(c)) { putchar(toupper(c)); state = 2; else case 2: if (!isalpha(c)) state = 1; 26 Running Code on Itself OK, That s a B+ % gcc upper1.c % a.out < 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; 27 Works correctly, but o Mysterious integer constants ( magic numbers ) o No modularization o No checking for states besides 1 and 2 What now? o States should have names, not just 1,2 o Should handle each state in a separate function o Good to check for unexpected variable value 28

Improvement: Names for States Improvement: Names for States Define your own named constants o Enumeration of a list of items o enum Statetype {NORMAL,INWORD; o Declare a variable of that type o enum Statetype state; 29 enum Statetype {NORMAL,INWORD; enum Statetype state = NORMAL; if (c == EOF) case NORMAL: if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else case INWORD: if (!isalpha(c)) state = NORMAL; 30 Improvement: Modularity Improvement: Modularity enum Statetype {NORMAL,INWORD; enum Statetype handlenormalstate(int c) {... enum Statetype handleinwordstate(int c) {... enum Statetype state = NORMAL; if (c == EOF) case NORMAL: state = handlenormalstate(c); case INWORD: state = handleinwordstate(c); 31 enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { state = NORMAL; return state; 32

Improvement: Modularity enum Statetype handleinwordstate(int c) { enum Statetype state; if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; Improvement: Defensive Programming Assertion checks for diagnostics o Check that that an expected assumption holds o Print message to standard error (stderr) when expression is false o E.g., assert(expression); o Makes program easier to read, and to debug case NORMAL: case INWORD: default: assert(0); Should never, ever get here. 33 34 Putting it Together: An A Effort Putting it Together: An A Effort enum Statetype handleinwordstate(int c) { enum Statetype {NORMAL,INWORD; enum Statetype state; enum Statetype handlenormalstate(int c) { enum Statetype state; if (isalpha(c)) { putchar(toupper(c)); state = INWORD; else { state = NORMAL; return state; 35 if (!isalpha(c)) state = NORMAL; else state = INWORD; return state; 36

Putting it Together: An A Effort Review of Example #3 enum Statetype state = NORMAL; if (c == EOF) case NORMAL: state = handlenormalstate(c); case INWORD: state = handleinwordstate(c); 37 Deterministic Finite Automaton o Two or more states o Actions in each state, or during transition o Conditions for transitioning between states Expectations for COS 217 assignments o Modularity (breaking into distinct functions) o Readability (meaningful names for variables and values) o Diagnostics (assertion checks to catch mistakes) o See K&P book for style guidelines specification 38