Oct 23, 2023
C Library Functions and Their Usage

C Library Functions

A library function in a C program must have a unique name and be defined as extern. This hides the function from other programs when it is published in a static or dynamic library file.

Unlike other languages, ANSI C has a relatively small standard library. It provides a basic set of functions for math, string manipulation and type conversions.

Inbuilt Functions

C library functions are pre-defined and grouped together into a single location called a library. They are used for performing various operations in a program like input/output, string manipulation and error handling etc. These are available in c compilers and can be accessed by using the appropriate header files.

In a program, the built-in functions can be called by passing the function pointer to them. Then the functions will get executed and return their results. Inbuilt functions can be used to save time in writing code for repetitive tasks. These functions are available for all the platforms as they are compiled in the same manner.

Several functions are known to be unsafe to use in practice, such as gets(). This is due to a combination of factors, including lack of context and memory safety. Despite this, they are still used in some languages and can be very useful. This is the reason why many resources recommend that they should be avoided.

General Functions

Every program must contain a main function. It can either be a user-defined or library function, but the latter must be called using the #include preprocessor directive.

All c library functions must be declared in a header file that is included in the source code of a program. The header files also contain data type definitions and macros.

Some c library functions require the use of pointers as argument parameters. Examples include math functions (abs, sqrt, floor, ceil), string handling (strint, sscanf, putc) and character conversion.

Several of these library functions are prone to pitfalls such as memory corruption or incorrect behaviour. For example, the string input function gets() is notorious for causing buffer overflows and many programming guides recommend avoiding its usage. These pitfalls are often related to the way pointers are handled in the C language. The stdio library functions can be used to reduce this risk. This library contains functions for converting between various file formats and providing access to the system’s hardware resources.

Portable Functions

C library functions are pre-defined functions that provide a range of functionality like input/output operations, string manipulation and mathematical calculations. These functions are grouped into different header files that must be included in the program in order to access them. These header files also contain the function prototypes and data definitions that are used in the program.

Some of these functions are ‘portable’ and others are not. Generally, those functions that are portable operate in user space (also known as ‘user mode’) and do not interact directly with the hardware. This means that they can work on all platforms and can be reused.

For example, the chr() function is a portable character conversion function. This functions classifies a given character ch as either alphabetic or punctuation and then returns the upper or lower case version of chr. The other functions, _stdio(), fgetc() and fputc() require re-implementation of the type layout in the target C implementation.

Error Handling Functions

Error handling is a big part of writing software and most modern languages have good error handling mechanisms built in that make it easier to maintain code. C does not have such facilities and as a system programming language it is left to the programmer to test return values from functions for error information. Most legacy function calls return -1 or NULL in case of an error. Some of them also set a global error code called errno (defined in the header file error.h>).

A programmer can then use the errno variable to display any errors or exit the program. Another way of handling errors is to create a function that checks for possible error conditions. The function can then log the error, exit, or try to recover from the error. This approach tends to be harder to read since the function mixes error handling, initialization, and cleanup. Instead, it is recommended to write code that splits up these responsibilities into separate functions.

Return to the home screen

More Details

Leave a Reply

Your email address will not be published. Required fields are marked *