EE1005-L05-Standard Library Functions
EE1005-L05-Standard Library Functions
Thinking to Programming
Lesson 5
Standard Library Functions
(Reference: Harry H. Cheng, Chapter 6, 9, 12)
Dr. Tan Chee Wah, Wesley (CoPaCE/School of EEE)
Email: wesleytan@ntu.edu.sg
Office: S1-B1b-54
Phone: 6790 6009
5-2
Header Files
The library has a set of associated header files
such as stdio.h. These contain
• prototypes of functions in the library
(Prototypes will be covered in a later lesson). .
• macro definitions
• other programming elements.
We need to know:
• The name of the function
• What the function does
• Input Arguments (if any) of the function
• Data type of Output Result returned (if any)
• The associated header file name
5-6
stdio.h
Some I/O functions/macros:
• printf()
• putchar() Displays one char on screen
Example: putchar('A');
• scanf()
• getchar() Gets one char from input stream
• fflush() Flushes I/O buffers
5-7
Input Stream & Input Buffer
The characters you type at the keyboard
form an input stream.
5-8
Why Use fflush(stdin)?
This is used to flush away any unwanted
characters in the input buffer.
Consider
scanf("%d", &num);
scanf("%c", &ch);
to read an integer and a character from the
keyboard.
The second statement will not execute as
expected (i.e. character not captured)! Why?
5-11
scanf()
(c) When reading numeric data, it will
continue reading until it meets a trailing
whitespace.
scanf("%d", &a);
5-12
scanf()
(d) When reading numeric data, it will stop
reading any subsequent characters in the input
stream if it meets an invalid character.
scanf("%d", &a);
5-15
getchar()
5-16
Example: getchar()
getchar() can be used to stop program
execution temporarily until the user presses the
ENTER key.
{ . . .
fflush(stdin); /* Need to flush if
there is an input statement before this */
getchar(); /* Program pauses when
nothing is entered. When ENTER is pressed,
getchar() will read this whitespace */
Trigonometric functions:
sin(x), cos(x), tan(x) x must be in radians
asin(x), acos(x), atan(x) Inverse functions
Exponential and log functions
exp(x) ex
log(x) loge x
log10(x) log10 x 5-18
math.h
Hyperbolic Functions:
sinh(x), cosh(x), tanh(x)
5-19
Example: sqrt(), pow()
double x = 2.718;
y = pow(x,6.0); y = 2.7186.0
z = (loge(2.718))2.5
z = pow(log(x),2.5);
Return data type of sqrt and pow is
double. 5-20
Example: ceil() & floor()
double x;
ceil(x) returns the integer (as a value of
type double!) that is just larger than or equal
to x.
5-22
Example (isdigit)
Digit like
Non-zero
'1' isdigit
value (True)
Non-Digit like
'a' isdigit 0 (False)
5-23
Example (isdigit)
#include <ctype.h>
char ch='1';
if (isdigit(ch) != 0) //isdigit() is TRUE
printf("%c is a digit.", ch);
else
printf("%c is not a digit.", ch);
Note:
• isdigit returns TRUE (not 0) or FALSE (0).
• != means not equal
• We’ll see later that the if statement can be
simply written as
if (isdigit(ch))
5-24
stdlib.h
We shall only consider 4 functions:
abs(), system(), rand() and srand()
Example:
system("Pause");
causes program execution to pause until
5-25
we
press any key.
stdlib.h: rand()
5-26
Program 5.3 (rand())
/* Example on the use of rand() */
#include <stdio.h>
#include <stdlib.h> //Library for rand()
int main(void)
{
printf("Three random integers are:\n %d
%d %d\n", rand(), rand(), rand());
return 0;
}
5-27
Program 5.3 (rand())
Output:
Three random integers are:
6334 18467 41
numbers.
Program 5.4 (srand())
/* Example on the use of srand() */
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //Library for time()
int main(void)
{
srand((unsigned) time(NULL));
printf("Three random integers are:\n
%d %d %d\n", rand(),rand(),rand());
return 0;
}
5-29
Program 5.4 (srand())
Output (different each time the program is run):
4. stdlib.h
• See Slide 5-25 for list of miscellaneous
functions
5. time.h – time (NULL) function
5-31