Library Functions stdlib'h - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Library Functions stdlib'h

Description:

atoi and atof. These functions are used to convert a string to an integer and a double value ... illustrates the function atoi. Digital Electronics EEE3017W R. ... – PowerPoint PPT presentation

Number of Views:370
Avg rating:3.0/5.0
Slides: 20
Provided by: robynve
Category:

less

Transcript and Presenter's Notes

Title: Library Functions stdlib'h


1
Library Functions stdlib.h
  • This library contains many useful general purpose
    functions
  • These functions can be broken down into six
    logical groupings
  • String conversion
  • Random number
  • Memory allocation
  • Communication with the environment
  • Searching and sorting utilities
  • Integer arithmetic

2
String Conversion Functions
  • These functions are used to convert strings to
    different data types
  • This is useful when data is read into a string
    buffer and must be converted to a numeric value
    so that a calculation can be performed on it
  • double atof(const char s)
  • int atoi(const char s)
  • long int atol(const char s)

3
atoi and atof
  • These functions are used to convert a string to
    an integer and a double value
  • Leading blank characters are skipped
  • Trailing illegal characters are ignored
  • If conversion cannot be made zero is returned

4
Example
  • This example illustrates the function atoi
  • char str1 "100"
  • char str2 "55.444"
  • char str3 " 1234"
  • char str4 "123four"
  • char str5 "invalid123"
  • int i
  • float f
  • i atoi(str1) // i 100
  • f atof(str2) // f 55.44
  • i atoi(str3) // i 1234
  • i atoi(str4) // i 123
  • i atoi(str5) // i 0

5
Random Number Functions
  • Random numbers are often needed in programs to
    simulate random events
  • It is important to understand that these
    functions do not produce truly random numbers
  • They produce pseudo-random numbers which are
    generated by a formula and are repeatable
  • Some random number functions in this C library
    are
  • int rand(void)
  • void srand(unsigned int seed)

6
int rand(void)
  • This function returns a pseudo-random number in
    the range 0 to RAND_MAX which is at least 32767
    (215 1)

7
void srand(unsigned int seed)
  • It is possible to improve the randomness of a
    variable by changing the starting point of the
    random function
  • The starting point is called a seed
  • If the seed is set to something like a time
    varying variable such as time of day we can see
    how this could really improve the unrepeatability
    of the function
  • The function srand is used to set the initial
    seed for the rand function
  • If srand is not used the initial seed is 1

8
Example
  • This example illustrates the functions rand and
    srand
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include lttime.hgt
  • int main(void)
  • int random
  • srand((unsigned int)time(NULL))
  • random rand()
  • printf("i\n", random)
  • system("PAUSE")

9
Communication with the Environment
  • These functions are used to control how the
    program interacts with the computer environment
    and include
  • void abort(void)
  • void exit(int status)
  • int system(const char string)

10
exit and abort
  • These two functions cause the program to
    terminate
  • The exit function terminates the program
    gracefully in other words it terminates the
    program and completes all the necessary
    termination procedures
  • The abort function causes abnormal program
    termination

11
int system(const char string)
  • The system function passes its string argument to
    the operating systems command processor for
    execution

12
Searching and Sorting Utilities
  • These functions are used to preform a binary
    search function and a simple data sort
  • They include
  • void bsearch(const void key, const void base,
    size_t n, size_t size, int (cmp)()const void
    keyval, const void datum))
  • void qsort(void base, size_t n size_t size, int
    (cmp)(const void, const void))

13
qsort
  • This function is used to arrange objects in an
    array base in ascending order according to the
    comparison function cmp
  • base contains n objects each of size size
  • cmp returns a
  • Negative value if first argument is less than
    the second
  • Zero if they are equal
  • Positive value if it is greater

14
bsearch
  • This function searches an ordered array base (of
    n objects each of size size) for an item matching
    key according to a comparison function cmp
  • cmp returns a
  • Negative value if first argument is less than
    the second
  • Zero if they are equal
  • Positive value if it is greater
  • Items in base are assumed to be sorted in
    ascending order
  • It returns a pointer to an item matching key or
    NULL if none are found

15
Integer Arithmetic
  • This library also contains some basic arithmetic
    functions. For more mathematic operations use
    math.h
  • These include
  • int abs(int j)
  • div_t div(int numer, int denom)
  • long int labs(long int j)
  • ldiv_t ldiv(long int numer, long int denom)

16
abs and labs Functions
  • The abs and labs function return the absolute
    value of their number arguments.
  • The abs function works on integer values while
    the labs function works on long values

17
Example
  • This example illustrates the functions abs
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • int main(void)
  • signed int num, absnum
  • printf(Please enter an integer )
  • scanf(i, num)
  • absnum abs(num)
  • printf(The absolute value of the integer is
    i\n,absnum)
  • system(PAUSE)

18
div and ldiv Functions
  • These functions take two arguments
  • numer which is the numerator
  • demon which is the denominator
  • They return a structure which holds two values
  • The quotient quot
  • The reminder rem
  • div works on integer values while ldiv works on
    long values

19
Example
  • This example illustrates the functions div
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • int main(void)
  • int num, denon
  • div_t ans
  • num 80
  • denon 4
  • ans div(num,denon)
  • printf(The answer is Quotient i and Remainder
    i,ans.quot, ans.rem)
  • system(PAUSE)
Write a Comment
User Comments (0)
About PowerShow.com