Lecture 2-Character Input-Output in C.ppt
《Lecture 2-Character Input-Output in C.ppt》由会员分享,可在线阅读,更多相关《Lecture 2-Character Input-Output in C.ppt(42页珍藏版)》请在麦多课文档分享上搜索。
1、1,Lecture 2: Character Input/Output in C,Professor Jennifer Rexford COS 217,http:/www.cs.princeton.edu/courses/archive/spring08/cos217/,2,Overview of Todays Lecture,Goals of the lecture Important C constructs Program flow (if/else, loops, and switch) Character input/output (getchar and putchar) Dete
2、rministic finite automata (i.e., state machine) Expectations for programming assignments C programming examples Echo the input directly to the output Put all lower-case letters in upper case Put the first letter of each word in upper case Glossing over some details related to “pointers” which will b
3、e covered in the next lecture,3,Echo Input Directly to Output,Including the Standard Input/Output (stdio) library Makes names of functions, variables, and macros available #include Defining procedure main() Starting point of the program, a standard boilerplate int main(void) int main(int argc, char
4、*argv) Hand-waving: argc and argv are for input arguments Read a single character Returns a single character from the text stream “standard in” (stdin) c = getchar(); Write a single character Writes a single character to “standard out” (stdout) putchar(c);,4,Putting it All Together,#include int main
5、(void) int c;c = getchar();putchar(c);return 0; ,5,Why is the Character an “int”,Meaning of a data type Determines the size of a variable and how it is interpreted and manipulated Difference between char and int char: character, a single byte (256 different values) int: integer, machine-dependent (e
6、.g., -32,768 to 32,767) One byte is just not big enough Need to be able to store any character plus, special value like End-Of-File (typically “-1”) Well see an example with EOF in a few slides,6,Read and Write Ten Characters,Loop to repeat a set of lines (e.g., for loop) Three arguments: initializa
7、tion, condition, and re-initialization E.g., start at 0, test for less than 10, and increment per iteration,#include int main(void) int c, i;for (i=0; i10; i+) c = getchar();putchar(c);return 0; ,7,Read and Write Forever,Infinite for loop Simply leave the arguments blank E.g., for ( ; ; ),#include i
8、nt main(void) int c;for ( ; ; ) c = getchar();putchar(c);return 0; ,8,Read and Write Till End-Of-File,Test for end-of-file (EOF) EOF is a special global constant, defined in stdio The break statement jumps out of the current scope,#include int main(void) int c;for ( ; ; ) c = getchar();if (c = EOF)
9、break;putchar(c);return 0; ,9,Many Ways to Say the Same Thing,for (;) c = getchar();if (c = EOF)break;putchar(c); ,for (c=getchar(); c!=EOF; c=getchar() putchar(c);while (c=getchar()!=EOF)putchar(c);,Very typical idiom in C, but messy side-effects in loop test,c = getchar(); while (c!=EOF) putchar(c
10、);c = getchar(); ,10,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 loop and while loop The break statement The return statement Assignment and comparison Assignme
11、nt: “=” Increment: “i+” Comparing for equality “=” Comparing for inequality “!=”,11,Example #2: Convert Upper Case,Problem: write a program to convert a file to all upper-case (leave nonalphabetic characters alone) Program design:repeatread a characterif its lower-case, convert to upper-casewrite th
12、e characteruntil end-of-file,12,ASCII,American Standard Code for Information Interchange0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 150 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US32 SP ! “ # $ % ? 64 A B C D E F G H I J K L M N O 80 P Q
13、 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),13,#include int main(void) int c;for ( ; ; ) c = getchar();if (c = EOF) break;if (c = 97) ,Implementation in C,14,Thats a B-minus,
14、Programming well means programs that are Clean Readable Maintainable Its not enough that your program works! We take this seriously in COS 217.,15,#include int main(void) int c;for ( ; ; ) c = getchar();if (c = EOF) break;if (c = 97) ,Avoid Mysterious Numbers,Correct, but ugly to have all these hard
15、-wired constants in the program.,16,#include int main(void) int c;for ( ; ; ) c = getchar();if (c = EOF) break;if (c = a) ,Improvement: Character Literals,17,Standard C Library Functions ctype(3C)NAMEctype, isdigit, isxdigit, islower, isupper, isalpha, isalnum, isspace, iscntrl, ispunct, isprint, is
16、graph, isascii - character handlingSYNOPSIS#include 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);,Improvement: Existi
17、ng Libraries,DESCRIPTIONThese macros classify character-coded 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() repre
18、sents a lower-case letter . the result is the corresponding upper-case letter. All other arguments in the domain are returned unchanged.,18,Using the ctype Library,#include #include int main(void) int c;for ( ; ; ) c = getchar();if (c = EOF) break;if (islower(c)c = toupper(c);putchar(c);return 0; ,1
19、9,% ls get-upper.c % gcc get-upper.c % ls a.out get-upper.c % a.out Well be on time today! WELL BE ON TIME TODAY! D %,Compiling and Running,20,% a.out #INCLUDE INT MAIN(VOID) INT C;FOR ( ; ; ) C = GETCHAR();IF (C = EOF) BREAK;IF (ISLOWER(C)C = TOUPPER(C);PUTCHAR(C);RETURN 0; ,Run the Code on Itself,
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LECTURE2CHARACTERINPUTOUTPUTINCPPT
