Download presentation
Presentation is loading. Please wait.
Published bySimon Mason Modified over 9 years ago
1
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions for performing common tasks.
2
Standard C Library Many of the general purpose functions available in the the Standard C library are declared in the following: math.h strings.h stdio.h stdlib.h time.h We’ll discuss the most commonly used functions declared in these.
3
math.h The math library contains what you would expect: functions for performing common mathematical operations. These include Function Description abs returns the absolute value of an integer x cos returns the cosine of x, where x is in radians exp returns e x fabs returns the absolute value of a float x log returns the logarithm to base e log10 returns the logarithm to base 10 pow returns x y sin returns the sine of x, where x is in radians sqrt returns the square root of x tan returns the tangent of x, where x is in radians
4
math.h The math library also contains functions you may not have heard of: Function Description ceil the ceiling function rounds a number with a decimal part up to the next highest integer (written mathematically as ⌈ x ⌉ ) floor the floor function rounds a number with a decimal part down to the next lowest integer (written mathematically as ⌊ x ⌋ ) See example-library-math.c on the course webpage. Note: Most of the functions in the math library expect the function argument(s) to be of type double and return a value of type double.
5
strings.h There are many functions for manipulating strings. Some of the more useful are Function Description strcat concatenates (i.e., adds) two strings strcmp compares two strings strcpy copies a string strlen calculates the length of a string (not including the null) strstr finds a string within another string strtok divides a string into tokens (i.e., parts) See example-library-strings.c on the course webpage. See example-library-strings2.c on the course webpage. Note: strcpy() and strcat() do not allocate the space required for their Operations.
6
stdio.h We’ve already seen many of the functions in stdio.h. Function Description printf formatted printing to stdout scanf formatted input from stdin fprintf formatted printing to a file fscanf formatted input from a file getc get a character from a stream (e.g, stdin or a file) putc write a character to a stream (e.g, stdout or a file) fgets get a string from a stream fputs write a string to a stream fopen open a file fclose close a file
7
stdlib.h Function Description atof convert a string to a double (not a float) atoi convert a string to an int exit terminate a program, return an integer value free release allocated memory malloc allocate memory rand generate a pseudo-random number srand seed the pseudo-random number generator system execute an external command See example-library-stdlib.c on the course webpage.
8
time.h This library contains several functions for getting the current date and time. Function Description time get the system time ctime convert the result from time() to something meaningful See example-library-time.c on the course webpage.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.