8
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.

Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

Embed Size (px)

Citation preview

Page 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

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.

Page 2: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

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.

Page 3: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

math.h

The math library contains what you would expect: functions for performingcommon mathematical operations. These include

Function Descriptionabs returns the absolute value of an integer xcos returns the cosine of x, where x is in radiansexp returns ex

fabs returns the absolute value of a float xlog returns the logarithm to base elog10 returns the logarithm to base 10pow returns xy

sin returns the sine of x, where x is in radianssqrt returns the square root of xtan returns the tangent of x, where x is in radians

Page 4: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

math.h

The math library also contains functions you may not have heard of:

Function Descriptionceil 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 downto 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.

Page 5: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

strings.hThere are many functions for manipulating strings. Some of the more useful are

Function Descriptionstrcat concatenates (i.e., adds) two stringsstrcmp compares two stringsstrcpy copies a stringstrlen calculates the length of a string (not including the null)strstr finds a string within another stringstrtok 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 theirOperations.

Page 6: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

stdio.h

We’ve already seen many of the functions in stdio.h.

Function Descriptionprintf formatted printing to stdoutscanf formatted input from stdinfprintf formatted printing to a filefscanf formatted input from a filegetc 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 streamfputs write a string to a streamfopen open a filefclose close a file

Page 7: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

stdlib.h

Function Descriptionatof convert a string to a double (not a float)atoi convert a string to an intexit terminate a program, return an integer valuefree release allocated memorymalloc allocate memoryrand generate a pseudo-random numbersrand seed the pseudo-random number generatorsystem execute an external command

See example-library-stdlib.c on the course webpage.

Page 8: Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions

time.h

This library contains several functions for getting the current date and time.

Function Descriptiontime get the system timectime convert the result from time() to something meaningful

See example-library-time.c on the course webpage.