Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Review of Class on Oct 12. 2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.

Similar presentations


Presentation on theme: "1 Review of Class on Oct 12. 2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The."— Presentation transcript:

1 1 Review of Class on Oct 12

2 2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The return Statement  More about function  Program Correctness: The assert() Macro  Function Declarations from the Compiler’s Viewpoint  Invocation and Call-by-Value  Developing a Large Problem

3 3 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

4 4 How to write a function? Preprocessing directives function prototype of fucntion1 function prototype of fucntion2 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } function invocations

5 5 How to write a function?  Example: write a code to computer the minimum value of three integers.  Define a function min2 which compute the minimum value of two integers.  Define a function min3 which calls function min2 to compute the minimum value of three integers.

6 6 #include Preprocessing directives Function prototype of min2 Function prototype of min3 int main(void) { ……. …… Call function min3 …… } Header of min2 definition { Body of function definition } Header of min3 definition { Body of function definition (Call function min2) } int min2(int a, int b); int min3(int a, int b, int c); int main(void) { printf("min of 11,23,24 is %d\n", min3(11,23,24)); return 0; } int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); }

7 7 Outline of Chapter 4  How to write a function?  Function Invocation  Function Definition  The return Statement  Function Prototypes  More about function  Program Correctness: The assert() Macro  Function Declarations from the Compiler’s Viewpoint  Invocation and Call-by-Value  Developing a Large Program

8 8 Program Correctness: The assert() Macro  C provides the assert() macro in assert.h to guarantee certain conditions  assert(exp) ensures the value of exp is true if exp is false, the system prints out a message and abort the program.

9 9 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion1 function prototype of fucntion2 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } Preprocessing directives Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } int main(void) { Body of function definition } A good program style is to give either the function definition or the function prototype or both before a function is used

10 10 Invocation and Call-by-Value  Function Invocation fun_name(exp1, exp2);  All arguments are passed call-by-value Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

11 11 #include int min2(int a, int b); int min3(int a, int b, int c); min.h #include “min.h” int main(void) { printf("%d\n", min3(1,3,4) ); return 0; } main.c int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); } min.c f.h: function prototypes f1.c, f2.c, f3.c  function definitions  in each.c file #include “f.h”  gcc f1.c f2.c f3.c gcc main.c min.c Developing a Large Program

12 12 End of Chapter 4: Function Read 4.1- 4.12

13 13 Chapter 5 Character Processing

14 14 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  end-of-file signal: EOF

15 15 The Data Type char  How character is stored in a machine?  Each character is stored in one byte according to a specific encoding.  Examples of character codes: ASCII oA table of the ASCII code in Appendix E EBCDIC

16 16 The Data Type char Examples:  How character ‘a’ is stored in ASCII code?  How many bits in one byte? 8  Appendix E  A table of the ASCII code bit 7bit 6bit 5bit 4bit 3bit 2bit 1bit 0 01100001

17 17 The Data Type char  When a character is stored in a byte,  the contents of that byte can be thought of as either a character or as a small integer.

18 18 The Data Type char  Example:  char c=‘a’; c is character ‘a’ c is a small integer represented in binary form 01100001, oWhat is the decimal value of this small integer? 97 bit 7bit 6bit 5bit 4bit 3bit 2bit 1bit 0 01100001

19 19 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. ‘a’: 01100001 the decimal value of 0110001 is 97 #include int main() { printf("%c\n", 'a'); printf("%d\n", 'a'); printf("%d\n", 97); printf("%c\n", 97); if ('a'==97) printf("'a'==97\n"); else printf("'a'!=97\n"); } %a.out a 97 a 'a'==97

20 20 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. ‘A’: the decimal value of ‘A’ is 65 #include int main() { printf("%c\n", ‘A'); printf("%d\n", ‘A'); printf("%d\n", 65); printf("%c\n", 65); if (‘A'==65) printf("‘A'==65\n"); else printf("‘A'!=65\n"); } %a.out A 65 A ‘A'==65

21 21 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. #include int main() { printf("printf1: %c\n", '\n'); printf("printf2: %d\n", '\n'); printf("printf3: %c\n", 10); printf("printf4: %d\n", 10); } % a.out printf1: printf2: 10 printf3: printf4: 10 % ‘\n’: the decimal value of ‘A’ is 10

22 22 The Data Type char In ASCII, Character ‘1’ == int 1? #include int main() { printf("%c \n", '1'); printf("%d \n", '1'); if (1=='1') printf("1=='1' \n"); else printf("1!='1' \n"); } % a.out 1 49 1!='1'

23 23 The Data Type char In ASCII,  Code for 0 through 9 are contiguous, letters A through Z are contiguous, and letters a through z are contiguous.  the difference between a capital letter and the corresponding lowercase letter is 32. #include int main() { printf("%c %d\n", 'a', 'a'); printf("%c %d\n", 'A', 'A'); printf("%d \n", 'a'-'A'); printf("%c \n", 'A'+32); printf("%c \n", 'a'-32); printf("%c \n", 'a'+3); printf("%c \n", 'A'+3); printf("%c \n", 'Y'+32); } % a.out a 97 A 65 32 a A d D y %

24 24 The Data Type char  Summary  each character is stored in one byte  it can be interpreted as either a character or as a small integer.  In ASCII, Character ‘1’ != int 1 Code for o0 through 9 are contiguous, oletters A through Z are contiguous, and oletters a through z are contiguous. the difference between a capital letter and the corresponding lowercase letter is 32.

25 25 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  EOF: end-of-file

26 26 Input and output characters — getchar() and putchar()  Read and Write characters Use format %c in printf and scanf functions  printf(“%c”, ‘A’);  char c; scanf(“%c”, &c);

27 27 Input and output characters — getchar() and putchar()  putchar()  defined in stdio.h  To write a character to the screen #include int main(){ putchar('H'); putchar('e'); putchar('l'); putchar('o'); putchar('\n'); } % a.out Hello %

28 28 Input and output characters — getchar() and putchar()  getchar(): reads a character from the keyboard  defined in stdio.h  c = getchar() getchar() gets a character from the keyboard and assigns it to the variable c. #include int main(){ char c; c=getchar(); putchar(c); putchar('\n'); } % a.out d d % scanf(“%c”, &c);

29 29 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  EOF: end-of-file

30 30 end-of-file: EOF  Example:  Get inputs from the keyboard and print out each input twice on the screen #include int main(void){ char c; while (1){ c = getchar(); putchar(c); } return 0; } How many times the while loop is executed? Infinite loop How to indicate the input is ended? while(the input is not ended){

31 31 end-of-file: EOF  How to indicate the input is ended?  User can use end-of-file signal to indicate the end of the input. How to represent end-of-file signal in C code? How to enter an end-of-file signal?

32 32 end-of-file: EOF  EOF in C code:  end-of-file signal: EOF  the value of EOF is system-dependent int value -1 is often used oLibrary stdio.h provides the following line:  #define EOF (-1) different systems can have different values  In order to avoid confusion, whatever is used to signal the end of a file, it cannot be a value that represents a character.

33 33 end-of-file: EOF  Example:  Get inputs from the keyboard and print out each input twice on the screen;  User enters EOF to end the input. #include int main(void){ int c; while ((c=getchar())!=EOF){ putchar(c); } return 0; } The value of EOF cannot be a value that represents a character.  In order to store the value of both characters (char) and EOF, c’s type should be int instead of char. #include int main(void){ char c; while (1){ c = getchar(); putchar(c); } return 0; }

34 34 end-of-file: EOF  How to input an end-of-file signal (EOF)?  Unix: carriage return followed by a control-d  MS-DOS: control-z

35 35 end-of-file: EOF  Summary  end-of-file: EOF  the value of EOF is system-dependent, provided in stdio.h  different systems can have different values Whatever is used to signal the end of a file, it cannot be a value that represents a character.  Input EOF in Unix: carriage return followed by a control-d

36 36 Chapter 5  Summary  each character is stored in one byte, and it can be interpreted as either a character or as a small integer.  In ASCII, Code for 0 through 9 are contiguous, letters A through Z are contiguous, and letters a through z are contiguous. the difference between a capital letter and the corresponding lowercase letter is 32.  putchar() To write a character to the screen  c = getchar() getchar() gets a character from the keyboard and assigns it to the variable c.  EOF

37 37 End of Chapter 5: Character Processing Read 5.1 – 5.8


Download ppt "1 Review of Class on Oct 12. 2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The."

Similar presentations


Ads by Google