0% found this document useful (0 votes)
23 views8 pages

Fe1008 05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Fe1008 05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The C Standard Library

Functions are the building blocks of C


FE1008 Computing programs.

e.g. printf(), scanf(), sqrt()


Chapter 5
Visual C++ (other C systems as well) provides
Standard Library a rich collection of powerful functions known as
the C Standard Library for our use.
Functions

5-2

Header Files Table 5.1 Selected Header Files


The library has a set of associated header files
Header File Description
such as stdio.h. These contain
• prototypes of functions in the library ctype.h Character handling
(Prototypes will be covered in a later chapter). .
math.h Math functions
• macro definitions
• other programming elements. stdio.h Input/Output
To use a function (e.g. printf( )), we need to stdlib.h Misc functions
include the corresponding header file.
string.h String functions
time.h Time functions
5-3 5-4
How to use standard functions stdio.h
Some I/O functions/macros:
We need to know: • printf()
• The name of the function
• putchar() Displays one char on screen
• What the function does
Example: putchar('A');
• Arguments (if any) of the function
• scanf()
• Data type of result returned (if any)
• getchar() Gets one char from input stream
• The associated header file name
• fflush() Flushes I/O buffers

5-5 5-6

Input Stream & Input Buffer fflush(stdin)

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

getchar() Example: getchar()

It will only read a character from the input buffer


getchar() can be used to stop program
after the user presses the ENTER key. execution temporarily until the user presses the
ENTER key.
Suppose you type { . . .
abcd ↵ fflush(stdin); /* Needed if there
is an input statement before this */
when getchar() is expecting input. getchar() getchar();
will only read the first character 'a', leaving
return 0;
behind the others.
}

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

Example: sqrt(), pow() Example: abs() and fabs()

double sqrt(double x);


It normally takes an argument of type double
abs() is for absolute value of an integer
but float x (or even int) is also acceptable. fabs() is for double (or float) data type.
Be careful about this difference.
double pow(double x, double y);

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

Program 5.3 (rand()) Program 5.3 (rand())


/* Example on the use of rand() */ Output:
Three random integers are:
#include <stdio.h>
6334 18467 41
#include <stdlib.h>
The program always produces the same output
int main(void) when the program is run repeatedly. This is
{ normally not satisfactory. To make the result
printf("Three random integers are:\n %d unpredictable, we need to provide a “seed” to
%d %d\n", rand(), rand(), rand()); the random number generator in rand().
return 0; Programmers frequently use the time, in
} seconds, provided by the computer clock as this
seed number.
5-27 5-28
Program 5.4 (srand()) Program 5.4 (srand())
/* Example on the use of srand() */
#include <stdio.h> Output (different each time the program is run):
#include <stdlib.h>
#include <time.h> Three random integers are:
12066 20374 22199
int main(void)
{
srand((unsigned) time(NULL)); srand() takes an unsigned integer argument
printf("Three random integers are:\n (the computer time in seconds, usually since
%d %d %d\n", rand(),rand(),rand()); midnight January 1, 1970 GMT).
return 0;
} time(NULL) means the time value used is not
stored in RAM.
5-29 5-30

You might also like