DOC PREVIEW
Princeton COS 217 - C Examples

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 1 C Examples!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…"2 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"3 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?"4 7 Read and Write Ten Characters!• Loop to repeat a set (block) of statements (e.g., for loop)"• Three expressions: initialization, condition, and increment"• E.g., start at 0, test for less than 10, and increment per iteration"#include <stdio.h> int main(void) { int c, i; for (i=0; i<10; i++) { c = getchar(); putchar(c); } 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?"5 9 Read and Write Until End-Of-File!• Test for end-of-file"• EOF is a global constant, defined in stdio.h"• 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; } do some stuff done yet? before the loop do more stuff after the loop 10 Many Ways to Do the Same Job!for (;;) { c = getchar(); if (c == EOF)! break; !putchar(c); }!for (c=getchar(); c!=EOF; c=getchar()) " putchar(c); while ((c=getchar())!=EOF) putchar(c);"Typical idiom in C, but messy side-effect in loop test"c = getchar(); while (c!=EOF) { ! putchar(c); c = getchar(); } Which approach is best?"6 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 character7 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 #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; } Implementation in C!8 That works great. Your grade is …!B-"15 16 Why a B-minus?!• A good program is:"• Clean"• Readable"• Maintainable"• Itʼs not enough that your program works!"• We take this seriously in this class"9 17 #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; } Avoid Mysterious Numbers!Ugly;"ASCII only"18 #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; } Improvement: Character Constants!Better; but assumes that alphabetic character codes are contiguous; not true in EBCDIC"10 19 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); Improvement:


View Full Document

Princeton COS 217 - C Examples

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download C Examples
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Examples and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Examples 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?