C langugae : Function at Glance

The ANSI C library classifies functions into several groups, with each group having an associated header file. This appendix gives you an overview of the library, listing the header files and briefly describing their associated functions. Some of these functions (for example, several I/O functions) are discussed in much greater detail in the text. More generally, for complete descriptions, consult the documentation for your implementation, or a reference manual
Input and Output
Input and Output contains sections on I/O streams, sequential and random file access , formatted I/O, and error handling.

                   General file access functions

Purpose

Functions

Rename a file, delete a file

rename( ), remove( )

Create and/or open a file

fopen( ), freopen( ), tmpfile( )

Close a file

fclose( )

Generate a unique filename

tmpnam( )

Query or clear file access flags

feof( ), ferror( ), clearerr( )

Query the current file access position

ftell( ), fgetpos( )

Change the current file access position

rewind( ), fseek( ), fsetpos( )

Write buffer contents to file

fflush( )

Control file buffering

setbuf( ), setvbuf( )


There are two complete sets of functions for input and output of characters and strings: the byte-character and the wide-character I/O functions
File I/O functions

Purpose

Functions in stdio.h

Functions in wchar.h

Get/set stream orientation

 

fwide( )

Write characters

fputc( ), putc( ), putchar( )

fputwc( ), putwc( ), putwchar( )

Read characters

fgetc( ), getc( ), getchar( )

fgetwc( ), getwc( ), getwchar( )

Put back characters read

ungetc( )

ungetwc( )

Write lines

fputs( ), puts( )

fputws( )

Read lines

fgets( ), gets( )

fgetws( )

Write blocks

fwrite( )

 

Read blocks

fread( )

 

Write formatted strings

printf( ), vprintf( )

fprintf( ), vfprintf( )

sprintf( ), vsprintf( )

snprintf( ), vsnprintf()

wprintf( ), vwprintf( )

fwprintf( ), vfwprintf( )

swprintf( ), vswprintf( )

Read formatted strings

scanf( ), vscanf( )

fscanf( ), vfscanf( )

sscanf( ), vsscanf( )

wscanf( ), vwscanf( )

fwscanf( ), vfwscanf( )

swscanf( ), vswscanf( )

Mathematical Functions
The standard library provides many mathematical functions. Most of them operate on real or complex floating-point numbers. However, there are also several functions with integer types, such as the functions to generate random numbers. The functions to convert numeral strings into arithmetic types.
Integer arithmetic functions

Purpose

Functions declared in stdlib.h

Functions declared in stdint.h

Absolute value

abs( ), labs( ), llabs( )

imaxabs( )

Division

div( ), ldiv( ), lldiv( )

imaxdiv( )

Random numbers

rand( ), srand( )

 
Floating-Point Functions
The functions for real floating-point types are declared in the header math.h, and those for complex floating-point types in complex.h. These functions that are available for both real and complex floating-point types. The complex versions of these functions have names that start with the prefix c. The functions that are only defined for the real types; and The functions that are specific to complex types.
 Functions for real and complex floating-point types

Mathematical function

C functions in math.h

C functions in complex.h

Trigonometry

sin( ), cos( ), tan( )

asin( ), acos( ), atan( )

csin( ), ccos( ), ctan( )

casin( ), cacos( ), catan( )

Hyperbolic trigonometry

sinh( ), cosh( ), tanh( )

asinh( ), acosh( ), atanh( )

csinh( ), ccosh( ), ctanh( )

casinh( ), cacosh( ), catanh( )

Exponential function

exp( )

cexp( )

Natural logarithm

log( )

clog( )

Powers, square root

pow( ), sqrt( )

cpow( ), csqrt( )

Absolute value

fabs( )

cabs( )



Functions for real floating-point types

Mathematical function

C function

Arctangent of a quotient

atan2( )

Exponential functions

exp2( ), expm1( ), frexp( ), ldexp( )

scalbn( ), scalbln( )

Logarithmic functions

log10( ), log2( ), log1p( ),

logb( ), ilogb( )

Roots

cbrt( ), hypot( )

Error functions for normal distributions

erf( ), erfc( )

Gamma function

tgamma( ), lgamma( )

Remainder

fmod( ), remainder( ), remquo( )

Separate integer and fractional parts

modf( )

Next integer

ceil( ), floor( )

Next representable number

nextafter( ), nexttoward( )

Rounding functions

trunc( ), round( ), lround( ), llround( ), nearbyint( ), rint( ), lrint( ), llrint( )

Positive difference

fdim( )

Multiply and add

fma( )

Minimum and maximum

fmin( ), fmax( )

Assign one number's sign to another

copysign( )

Generate a NaN

nan( )



 Functions for complex floating-point types

Mathematical function

C function

Isolate real and imaginary parts

creal( ), cimag( )

Argument (the angle in polar coordinates)

carg( )

Conjugate

conj( )

Project onto the Riemann sphere

cproj( )


Character Classification and Conversion
16.3. Character Classification and Conversion The standard library provides a number of functions to classify characters and to perform conversions on them. The header ctype.h declares such functions for byte characters, with character codes from 0 to 255. The header wctype.h declares similar functions for wide characters, which have the type wchar_t. These functions are commonly implemented as macros.
The results of these functions, except for isdigit( ) and isxdigit( ), depends on the current locale setting for the locale category LC_CTYPE. You can query or change the locale using the setlocale( ) function.
Character classification functions

Category

Functions in ctype.h

Functions in wctype.h

Letters

isalpha( )

iswalpha( )

Lowercase letters

islower( )

iswlower( )

Uppercase letters

isupper( )

iswupper( )

Decimal digits

isdigit( )

iswdigit( )

Hexadecimal digits

isxdigit( )

iswxdigit( )

Letters and decimal digits

isalnum( )

iswalnum( )

Printable characters (including whitespace)

isprint( )

iswprint( )

Printable, non-whitespace characters

isgraph( )

iswgraph( )

Whitespace characters

isspace( )

iswspace( )

Whitespace characters that separate words in a line of text

isblank( )

iswblank( )

Punctuation marks

ispunct( )

iswpunct( )

Control characters

iscntrl( )

iswcntrl( )


Free Web Hosting
Case Mapping
Character conversion functions

Conversion

Functions in ctype.h

Functions in wctype.h

Upper- to lowercase

tolower( )

towlower( )

Lower- to uppercase

toupper( )

towupper( )

String Processing
A string is a continuous sequence of characters terminated by '\0', the string terminator character. The length of a string is considered to be the number of characters before the string terminator. Strings are stored in arrays whose elements have the type char or wchar_t. Strings of wide charactersthat is, characters of the type wchar_tare also called wide strings.
String-processing functions

Purpose

Functions in string.h

Functions in wchar.h

Find the length of a string.

strlen( )

wcslen( )

Copy a string.

strcpy( ), strncpy( )

wcscpy( ), wcsncpy( )

Concatenate strings.

strcat( ), strncat( )

wcscat( ), wcsncat( )

Compare strings.

strcmp( ), strncmp( ), strcoll( )

wcscmp( ), wcsncmp( ), wcscoll( )

Transform a string so that a comparison of two transformed strings using strcmp( ) yields the same result as a comparison of the original strings using the locale-sensitive function strcoll( ).

strxfrm( )

wcsxfrm( )

In a string, find:

   
  • The first or last occurrence of a given character

strchr( ), strrchr( )

wcschr( ), wcsrchr( )

  • The first occurrence of another string

strstr( )

wcsstr( )

  • The first occurrence of any of a given set of characters

strcspn( ), strpbrk( )

wcscspn( ), wcspbrk( )

  • The first character that is not a member of a given set

strspn( )

wcsspn( )

Parse a string into tokens

strtok( )

wcstok( )

Multibyte Characters
16.5. Multibyte Characters In multibyte character sets, each character is coded as a sequence of one or more bytes Unlike wide characters, each of which is represented by a single object of the type wchar_t, individual multibyte characters may be represented by different numbers of bytes. However, the number of bytes that represent a multibyte character , including any necessary state-shift sequences, is never more than the value of the macro MB_CUR_MAX, which is defined in the header stdlib.h.
C provides standard functions to obtain the wide-character code, or wchar_t value, that corresponds to any given multibyte character, and to convert any wide character to its multibyte representation. Some multibyte encoding schemes are stateful; the interpretation of a given multibyte sequence may depend on its position with respect to control characters, called shift sequences, that are used in the multibyte stream or string. In such cases, the conversion of a multibyte character to a wide character, or the conversion of a multibyte string into a wide string, depends on the current shift state at the point where the first multibyte character is read. For the same reason, converting a wide character to a multibyte character, or a wide string to a multibyte string, may entail inserting appropriate shift sequences in the output.
Multibyte character functions

Purpose

Functions in stdlib.h

Functions in wchar.h

Find the length of a multibyte character

mblen( )

mbrlen( )

Find the wide character corresponding to a given multibyte character

mbtowc( )

mbrtowc( )

Find the multibyte character corresponding to a given wide character

wctomb( )

wcrtomb( )

Convert a multibyte string into a wide string

mbstowcs( )

mbsrtowcs( )

Convert a wide string into a multibyte string

wcstombs( )

wcsrtombs( )

Convert between byte characters and wide characters

 

btowc( ), wctob( )

Test for the initial shift state

 

mbsinit( )

Converting Between Numbers and Strings
The standard library provides a variety of functions to interpret a numeral string and return a numeric value. The numeral conversion functions differ both in their target types and in the string types they interpret. The functions for char strings are declared in the header stdlib.h, and those for wide strings in wchar.h.
Conversion of numeral strings

Conversion

Functions in stdlib.h

Functions in wchar.h

String to int

atoi( )

 

String to long

atol( ), strtol( )

wcstol( )

String to unsigned long

strtoul( )

wcstoul( )

String to long long

atoll( ), strtoll( )

wcstoll( )

String to unsigned long long

strtoull( )

wcstoull( )

String to float

strtof( )

wcstof( )

String to double

atof( ), strtod( )

wcstod( )

String to long double

strtold( )

wcstold( )

Memory Block Handling
The functions initialize, copy, search, and compare blocks of memory. The functions declared in the header string.h access a memory block byte by byte, while those declared in wchar.h read and write units of the type wchar_t. Accordingly, the size parameter of each function indicates the size of a memory block as a number of bytes, or as a number of wide characters.
Functions to manipulate blocks of memory

Purpose

Functions in string.h

Functions in wchar.h

Copy a memory block, where source and destination do not overlap

memcpy( )

wmemcpy( )

Copy a memory block, where source and destination may overlap

memmove( )

wmemmove( )

Compare two memory blocks

memcmp( )

wmemcmp( )

Find the first occurrence of a given character

memchr( )

wmemchr( )

Fill the memory block with a given character value

memset( )

wmemset( )

Dynamic Memory Management
Many programs, including those that work with dynamic data structures for example, depend on the ability to allocate and release blocks of memory at runtime. C programs can do that by means of the four dynamic memory management functions declared in the header stdlib.h
Dynamic memory management functions

Purpose

Function

Allocate a block of memory

malloc( )

Allocate a memory block and fill it with null bytes

calloc( )

Resize an allocated memory block

realloc( )

Release a memory block

free( )

Date and Time
The header time.h declares the standard library functions to obtain the current date and time , to perform certain conversions on date and time information, and to format it for output. A key function is time( ), which yields the current calendar time in the form of an arithmetic value of the type time_t. This is usually encoded as the number of seconds elapsed since a specified moment in the past, called the epoch. The Unix epoch is 00:00:00 o'clock on January 1, 1970, UTC (Coordinated Universal Time, formerly called Greenwich Mean Time or GMT).
There are also standard functions to convert a calendar time value with the type time_t into a string or a structure of type struct tm. The structure type has members of type int for the second, minute, hour, day, month, year, day of the week, day of the year, and a Daylight Saving Time flag.
Date and time functions

Purpose

Function

Get the amount of CPU time used

clock( )

Get the current calendar time

time( )

Convert calendar time to struct tm

gmtime( )

Convert calendar time to struct tm with local time values

localtime( )

Normalize the values of a struct tm object and return the calendar time with type time_t

mktime( )

Convert calendar time to a string

ctime( ), strftime( ), wcsftime( )