Total Pageviews

Saturday, November 13, 2010

Lesson 36 -C-UNIX Library Functions

The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others are very specialised in their application.

Wise programmers will check whether a library function is available to perform a task before writing their own version. This will reduce program development time. The library functions have been tested, so they are more likely to be correct than any function which the programmer might write. This will save time when debugging the program.

Finding Information about Library Functions

The UNIX manual has an entry for all available functions. Function documentation is stored in section 3 of the manual, and there are many other useful system calls in section 2. If you already know the name of the function you want, you can read the page by typing (to find about strcat).


man 3 strcat
If you don't know the name of the function, a full list is included in the introductory page for section 3 of the manual. To read this, type

man 3 intro
There are approximately 700 functions described here. This number tends to increase with each upgrade of the system.
On any manual page, the SYNOPSIS section will include information on the use of the function. For example


#include

char *ctime(time_t *clock)
This means that you must have

#include
in your file before you call ctime. And that function ctime takes a pointer to type time_t as an argument, and returns a string (char *). time_t will probably be defined in the same manual page.
The DESCRIPTION section will then give a short description of what the function does. For example


ctime() converts a long integer, pointed to by clock, to a
26-character string of the form produced by asctime().
Further related reading is suggested in the SEE ALSO section.

Use of Library Functions

To use a function, ensure that you have made the required #includes in your C file. Then the function can be called as though you had defined it yourself.

It is important to ensure that your arguments have the expected types, otherwise the function will probably produce strange results. lint is quite good at checking such things.

Some libraries require extra options before the compiler can support their use. For example, to compile a program including functions from the math.h library the command might be


cc mathprog.c -o mathprog -lm
The final -lm is an instruction to link the maths library with the program. The manual page for each function will usually inform you if any special compiler flags are required.

Some Useful Library Functions

The following functions may be useful to you. Each manual page typically describes several functions, so if you see something similar to what you want, try looking in that manual page.

Function Description

abs integer absolute value
ctime convert date and time
fopen open a stream
printf formatted output
fputc put character or word on a stream
getwd get current working directory path name
strcat string concatenation
ctype character classification and conversion macros and functions
mktemp make a unique file name
puts put a string on a stream
sleep suspend execution for interval
stdio standard buffered input/output package

To get a full summary type


man 3 intro
at your terminal.

No comments:

Post a Comment