1
Review of Class on Oct 12
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
How to write a function?
#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)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 InvocationHow to specify the function?
Function DefinitionHow to get the control back?
return statement
4
How to write a function?Preprocessing directivesfunction prototype of fucntion1function prototype of fucntion2int 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
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
#include <stdio.h> Preprocessing directivesFunction prototype of min2Function prototype of min3int 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
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
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 trueif exp is false, the system prints out a
message and abort the program.
9
Function Declarations from the Compiler’s Viewpoint
Preprocessing directivesfunction prototype of fucntion1function prototype of fucntion2int main(void){ Body of function definition}Header of function1 definition{
Body of function definition}Header of function2 definition{
Body of function definition}
Preprocessing directivesHeader 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
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
#include <stdio.h>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 prototypesf1.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
End of Chapter 4: FunctionRead 4.1- 4.12
13
Chapter 5
Character Processing
14
Outline
The Data Type CharInput and output characters
getchar() putchar()
end-of-file signal: EOF
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
o A table of the ASCII code in Appendix E
EBCDIC
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 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
0 1 1 0 0 0 0 1
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
The Data Type char
Example: char c=‘a’;
c is character ‘a’c is a small integer represented in
binary form 01100001, o What is the decimal value of this small
integer? 97
bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
0 1 1 0 0 0 0 1
19
The Data Type charWhen a character is stored in a byte, it can be thought of as either
a character or as a small integer.
‘a’: 01100001the decimal value of 0110001 is 97
#include <stdio.h>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.outa9797a'a'==97
20
The Data Type charWhen 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 <stdio.h>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.outA6565A‘A'==65
21
The Data Type charWhen a character is stored in a byte, it can be thought of as either
a character or as a small integer.
#include <stdio.h>int main(){ printf("printf1: %c\n", '\n'); printf("printf2: %d\n", '\n'); printf("printf3: %c\n", 10); printf("printf4: %d\n", 10);}
% a.outprintf1: printf2: 10 printf3: printf4: 10%
‘\n’: the decimal value of ‘A’ is 10
22
The Data Type char
In ASCII, Character ‘1’ == int 1?
#include <stdio.h>int main(){ printf("%c \n", '1'); printf("%d \n", '1'); if (1=='1') printf("1=='1' \n"); else printf("1!='1' \n");}
% a.out1491!='1'
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 <stdio.h>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.outa 97A 6532aAdDy%
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 1Code for
o 0 through 9 are contiguous, o letters A through Z are contiguous, and o letters a through z are contiguous.
the difference between a capital letter and the corresponding lowercase letter is 32.
25
Outline
The Data Type CharInput and output characters
getchar() putchar()
EOF: end-of-file
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
Input and output characters — getchar() and putchar()
putchar() defined in stdio.h To write a character to the screen
#include <stdio.h>int main(){
putchar('H');putchar('e');putchar('l');putchar('l');putchar('o');putchar('\n');
}
% a.outHello%
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 <stdio.h>int main(){
char c;c=getchar();putchar(c);putchar('\n');
}
% a.outdd%
scanf(“%c”, &c);
29
Outline
The Data Type CharInput and output characters
getchar() putchar()
EOF: end-of-file
30
end-of-file: EOF
Example: Get inputs from the keyboard and print out
each input twice on the screen
#include <stdio.h>int main(void){ char c; while (1){ c = getchar(); putchar(c); 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
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
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 usedo Library 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
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 <stdio.h>int main(void){ int c; while ((c=getchar())!=EOF){ putchar(c); 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 <stdio.h>int main(void){ char c; while (1){ c = getchar(); putchar(c); putchar(c); } return 0;}
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
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
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
End of Chapter 5: Character ProcessingRead 5.1 – 5.8
Top Related