Fe1008 05
Fe1008 05
5-2
5-5 5-6
The characters you type at the keyboard This is used to flush away any unwanted
form an input stream. characters in the input buffer.
Consider
Usually this stream of characters first goes scanf("%d", &num);
into an area of memory known as the input scanf("%c", &ch);
buffer before being retrieved (e.g. by to read an integer and a character from the
scanf() or getchar()) in a running keyboard.
program. These statements don’t work as one may
expect! Why?
5-7 5-8
fflush(stdin) scanf()
Should write Some points to note about scanf():
scanf("%d", &num);
fflush(stdin); (a) When using %c to read in characters,
scanf("%c", &ch); remember that whitespaces (spaces, tabs,
newlines) are characters.
stdin refers to the input stream created by
input from the keyboard.
scanf("%c%c", &ch1, &ch2);
The following normally does not have this
problem: Be careful about how you input these two
scanf("%d", &num1); characters. Do not separate them using a
whitespace.
scanf("%d", &num2);
5-9 5-10
scanf() scanf()
(b) When reading in numbers using %d, %f or (c) When reading numeric data, it will
%lf , scanf() skips leading whitespaces. continue reading until it meets a trailing
whitespace.
In inputting two numbers using
scanf("%d", &a);
scanf("%d%d", &num1, &num2);
we can separate them using one or more
whitespaces which will be discarded by the Suppose you type 123 456. This input is
computer. considered as two numbers. scanf()will
only read 123, leaving the rest behind in the
input buffer.
5-11 5-12
scanf() getchar()
(d) When reading numeric data, it will stop getchar() reads the next available character
reading any subsequent characters in the from the input buffer and returns its value as
input stream if it meets an invalid character. an integer.
We can assign the character read to a variable as
scanf("%d", &a); in the following:
ch = getchar();
Suppose you type 123xyz. scanf() will
read the 123 part but leave xyz behind in or simply call the function without doing any
the input buffer. assignment:
getchar();
5-13 5-14
5-15 5-16
math.h math.h
Some mathematical functions: Hyperbolic Functions:
sinh(x), cosh(x), tanh(x)
abs(x) Absolute value of an integer (stdlib.h)
fabs(x) Abs value of a float/double
Other mathematical functions:
Trigonometric functions:
sin(x), cos(x), tan(x) x must be in radians pow(x,y) x raised to power y
asin(x), acos(x), atan(x) Inverse functions sqrt(x) Square root of x
ceil(x) Ceiling function
Exponential and log functions
floor(x) Floor function
exp(x) ex
log(x) loge x
log10(x) log10 x
5-17 5-18
double x = 2.718;
y = pow(x,6.0);
z = pow(log(x),2.5);
5-19 5-20
ceil() & floor() ctype.h
double x; Some character functions:
ceil(x) returns the integer (as a value of • isalnum Alphanumeric character?
type double!) that is just larger than or equal
to x. • isalpha Alphabetic character?
ceil(1.4) returns 2.0 • isdigit Decimal digit?
How about ceil(-1.4)? • islower Lower case character?
• isupper Upper case character?
floor(1.4) returns 1.0 • tolower Upper case --> lower case
How about floor(-1.4) ?
• toupper Lower case --> upper case
5-21 5-22
Example (isdigit)
Example (isdigit)
#include <ctype.h>
char ch='1';
if (isdigit(ch) != 0)
Non-zero
'1' isdigit printf("%c is a digit.", ch);
value (True)
else
printf("%c is not a digit.", ch);
Note:
'a' isdigit 0 (False) • isdigit returns TRUE (not 0) or FALSE (0).
• != means not equal
• We’ll see later that the if can be simply written
as
if (isdigit(ch))
5-23 5-24
stdlib.h stdlib.h: rand()
We shall only consider 4 functions:
rand() and srand() are for generating
abs(), system(), rand() and srand()
random integers.
abs(x) returns the absolute value of an
integer x. The function rand() generates a random
system() enables us to execute operating integer lying in the range: 0 to 32767 (This
system commands. maximum value is defined as a macro
RAND_MAX in stdlib.h).
Example:
system("Pause");
causes program execution to pause until we
press any key.
5-25 5-26