Vous êtes sur la page 1sur 19

Introduction to Computer and Program Design

Lesson 2
Functions, scanf and EOF
James C.C. Cheng
Department of Computer Science
National Chiao Tung University
The return of scanf
The number of fields successfully converted and assigned
int a =1, b =2, c =3;
int n = scanf("%d %d %d", &a, &b, &c);
printf("%d\n", n );
printf("%d, %d, %d", a, b, c);

In the case of an input failure before any data could be successfully


read, EOF is returned.

2
Multiple input

int x = 0;
while( scanf("%d", &x) != EOF ){

}

EOF: end of file


usually EOF is defined as (-1)
It is a signal to tell the system that no more data can be read from a
input source.
In Windows, press Ctrl+Z and Enter
In UNIX, Linux and Mac OS X, press Ctrl+D

3
Abnormal Input

int x = 0;
while( scanf("%d", &x) != EOF ){

}

Please input a non-numeric line, ex: %T@#$@KI


A non-stop loop occurred! Why?
How to fix the problem?

4
Standard I/O Buffer
Buffer:
A memory block used to temporarily hold data while it is being moved
from one place to another.
There three I/O memory buffers in the computer
FILE* stdin : For the standard input device (generally, a keyboard).
FILE* stdout : For the standard output device (generally, the screen).
FILE* stderr : For output error and warning messages to the standard
output device
FILE* is a pointer to point a stream I/O buffer
address data
0x 26E81E00
26E81E00 04 A5 00 10
FILE* stdin
26E81E04 0B 00 56 4F
26E81E08 06 FF 00 00
26E81E0C B4 00 C7 FF

5
The Reading Mechanism of scanf -- 1
In the format string of scanf:
Format specifiers (%):
A sequence formed by an initial percentage sign (%) indicates a format specifier,
which is used to specify the type and format of the data to be retrieved from stdin
and stored in the locations pointed by the additional arguments.
The basic usage of format specifier:

%[*][l]type
where type can be c, d, e, E, f, g, G, o, s, x, X
EX: %d
Read but ignored

%*type
int x = 0;
while( scanf("%*d%d", &x) != EOF ){
printf("%d\n", x); // ?
}

6
The basic data types
There are 13 basic data types in C
Name Size (byte) Range
char 1 -128 to 127
unsigned char 1 0 to 255
short 2 -32768 to 32767
unsigned short 2 0 to 65535
int 4 -231 to 231 - 1
unsigned int 4 0 to 232 - 1
long 4 -231 to 231 - 1
unsigned long 4 0 to 232 - 1
__int64, long long 8 -263 to 263 1
unsigned __int64 8 0 to 264 - 1
float 4 (1.175494351e-38 to 3.402823466e38 )
double 8 (2.2250738585072014e-308 to
1.7976931348623158e308 )
long double 12 in DevC++, 8 in MSC The same as double

In C++, bool represents boolean value, true or false.


The size of bool depends on compiler, 1 byte in most case. 7
The type of format specifier
type Qualifying Input Type of argument
Single character: Reads the next character. If a width
different from 1 is specified, the function reads width
c characters and stores them in the successive locations char *
of the array passed as argument. No null character is
appended at the end.
Decimal integer: Number optionally preceded with a + or
d int *
- sign.
Floating point: Decimal number containing a decimal
point, optionally preceded by a + or - sign and optionally
e,E,f,g,G float *
folowed by the e or E character and a decimal number.
Two examples of valid entries are -732.103 and 7.12e4
o Octal integer. int *
String of characters. This will read subsequent
s characters until a whitespace is found (whitespace char *
characters are considered to be blank, newline and tab).
u Unsigned decimal integer. unsigned int *
x,X Hexadecimal integer. int *

8
The Reading Mechanism of scanf -- 2
It starts after one string line inputted.
a string which the last key is \n
In the format string of scanf:
White-space characters, WC:
blank (' '); tab ('\t'); or newline ('\n').
WC WC
format string WCWC
C.
The non-WCs in format, except the %
scanf stdin format string WC.
discarded.
scanf stdin
stdin

9
Standard Input in C
How to clear the stdin?
getchar()
read a character from stdin
Usage:
#include <stdio.h>
int getchar( void );
Returns the input character
Ex:
int c = getchar();
Using getchar() to clear stdin

int x=0;
scanf("%d", &x);
printf("%d\n", x);
{ int c; while((c=getchar())!='\n'&&c!=EOF); } // clear stdin
printf("press enter to exit");
getchar(); // Waiting an Enter key
10
EOF in Windows
Ctrl + z (^Z) in Windows:
^Z\n
^Z\n^Z\n
^Z!@#!@$!$!@\n^Z\n^Z\n^Z\n
EOF\n^Z
\nSubstituteASCII code26
EOF^ZASCII code
, ^Z\n?

int x=0, y=0;


a white space
while( scanf(" %d%d", &x, &y) != EOF ){
printf("%d, %d\n", x, y);
}
11
EOF in Windows
DO NOT let the last character of the format string be a WC
int x=0;
a white space

while( scanf("%d ", &x) != EOF ){


printf("%d\n", x);
}

12
Exercises
?

int x=0, y=0; :


while( scanf("%d,%d", &x, &y) != EOF ){ 1,2
printf("%d, %d\n", x, y); 3 ,4
}
5, 6
7 , 8
int x=0, y=0; 9 10
while( scanf("%d , %d", &x, &y) != EOF ){
printf("%d, %d\n", x, y);
}

int x=0, y=0;


while( scanf("%d ,%d", &x, &y) != EOF ){
printf("%d, %d\n", x, y);
}

13
Function declaration
Syntax:
return type + function name + (parameters);
where the parameters are:
1. nothing or void
2. typename1, typename2, typenameN
3. the names of parameters are not necessary
Function name must be unique , except C++.

Ex:

int F1(void);
int F2();
void F3(void);
void F3(int); // In C++, OK, but error in C
int F4(int, int, char);
or
int F4(int x, int y , char c);

14
Function declaration
In C++, the function declaration must be placed before calling

int main( void ){


int MyF(int); // declaration
int x = 0;
printf("%d\n", MyF(x));
return 0;
}
int MyF(int n){ return n+1 ;} // Function definition

15
Function definition
Function definition
Syntax:
return type + function name + (parameters){ }
where the name of the parameters not be ignored.

int MyF(int n){


return n+1 ;
}

Function definition can not be placed in any block (except C++).

16
Function definition
Declaration, definition and invocation.
Which of following are correct?
In C, all of them are correct
implicit declaration and the function is assumed to return int
In C++, the third is failed
int MyF (int a); int MyF (int a); int main( void ){
int MyF(int a){ return a+1; } int main( void ){ int x = MyF (1);
int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x);
int x = MyF (1); printf("1+1 = %d\n", x); return 0;
printf("1+1 = %d\n", x); return 0; }
return 0; } int MyF (int a);
} int MyF (int a){ return a+1;} int MyF (int a){ return a+1; }
int MyF (int a){ return a+1; } int MyF (int a){ return a+1; } int MyF (int a){ return a+1; }
int main( void ){ int MyF (int a); int main( void ){
int x = MyF (1); int main( void ){ int x = MyF (1);
printf("1+1 = %d\n", x); int x = MyF (1); printf("1+1 = %d\n", x);
return 0; printf("1+1 = %d\n", x); return 0;
} return 0; }
int MyF (int a); } 17
Function definition
Default argument
Only C++ provides default argument for functions
A default argument is a value given in the declaration that the compiler
automatically inserts if you dont provide a value in the function call.
A default argument cannot be placed before non-default argument
The declaration of a default argument either in global function
declaration or in function definition.
Local function declaration can has its own default argument list.
void F1(int a, int b =10, int c = 2){ }
void F2(int a, int b =10, int c){ } // Compiler error
void F3(int a, int b =10, int c=5); // Global function declaration
void F3(int a, int b =10, int c=5){ } // Compiler error
int main(){
F1(1, 2, 3); // a = 1; b= 2; c =3
F1(1); // a = 1; b= 10; c =2
void F1(int a, int b =20, int c = 30); // Local function declaration
F1(1); // a = 1; b= 20; c =30
F1(1, ,3); // Compiler error
} 18

1. functionCheckN
int CheckN(int n);
n > 0 digitreturndigit
return 010<=n<100returndigitn<10return0

2. functionTileN
int TileN(int RoomW, int RoomH, int TileW, int TileH);
RoomWRoomH: ()
TileWTileH : ()
TileNreturn

3. functionIsPrime
If n = ab, a <= b.
int IsPrime(int n); Assuming that a > . n .
n Then, n = ab >= aa > n causes contrary.
Therefore, a <= n
nreturn 1return 0 19

Vous aimerez peut-être aussi