This section gives an alphabetic listing of common ANSI C system routines.
abs int abs(int n); stdlib.h absolute value of n See also: labs(), fabs()
acos
double acos(double x);
math.h
arc cosine of x in the range to
.
x must be in the range -1
to 1. If not, returns 0 and sets errno to EDOM, i.e. domain error.
asin
double asin(double x);
math.h>
arc sine of x in the range to
x must be in the range -1
to 1. If not, returns 0 and sets errno to EDOM, i.e. domain error.
assert void assert(int exp); assert.h void if exp is zero, it prints a message:
It then aborts the program.
atan
double atan(double x);
math.h
arc tangent of x; 0, if error.
The returned value is within the range to
.
atan2
double atan(double y, double x);
math.h
arc tangent of y/x; 0, if error.
The returned value is within the range to
.
atof double atof(const char *str); stdlib.h floating point equivalent of string, str. This function converts a string to a floating point value. The function converts the characters in the string, str, from left to right until it hits a character other than those allowed in a floating point number.
atoi int atoi(const char *str); stdlib.h integer equivalent of string str. This function converts a string to an integer value. The function converts each digit in str from left to right until it hits a non-digit character.
bsearch
stdlib.h address of first match or 0. Searches the table of nelem elements , starting at base and returns the address of the first entry, of size, width, that matches the key. If no match is found, it returns a 0. The comparison function is *fcmp given the key pointer as its first argument and the element pointer as its second. It should return 0 if the two entries are equal, a positive value if the first is greater, and a negative value if the first is less.
cabs double cabs(struct complex z); math.h absolute value, i.e. magnitude of a complex number, z. complex is a structure defined in < math.h> with a real part, x and an imaginary part, y:
struct complex { double x, y; };
calloc void *calloc(size_t number, size_t size); stdlib.h pointer to the allocated block of memory, if successful; NULL if unsuccessful. The calloc() function returns a pointer to the allocated block of memory. The block size is the number of items times size bytes. The return value is NULL if there is insufficient memory available. All bytes of allocated memory are initialized to zero.
Note: calloc() and malloc() are similar functions, except that malloc() does not initialize memory.
ceil double ceil(double x); math.h smallest whole number not less than x. The ceil() function returns a double value representing the smallest integer that is greater than or equal to x.
clearerr void clearerr(FILE *stream); stdio.h void The clearerr() function resets the stream's error and end of file indicators to 0.
cos double cos(double x); math.h cosine of x Ranges as for sin().
div div_t div(int numerator, int denominator); stdlib.h quotient and remainder of numerator divided by denominator The type div_t is defined in < stdlib.h> as a structure of integers:
typedef struct { int quot; int rem; } div_t;
ldiv
stdlib.h quotient and remainder of numerator divided by denominator The type ldiv_t is defined in < stdlib.h> as a structure of integers:
typedef struct { long quot; long rem; } ldiv_t;
exit void exit(int status); stdlib.h void The function exit() terminates the calling process; buffers are written out and all files are closed. The value of status is returned as the exit status of the process; 0 usually implies no error and non-zero indicates some error.
atexit int atexit(void (*fcn)(void)); stdlib.h non-zero of the registration cannot be made. The function atexit() registers the function, fcn, to be called when a program exits normally.
closes stream fp.
Note: clearerr(), fclose() or rewind() reset the indicator.
Note: clearerr() or rewind() can reset the indicator.
fflush int fflush(FILE *fp); stdio.h 0 if successful; EOF if not successful. This function forces any data in the buffer associated with the output file, fp, to be written out.
fgets char *fgets(char *str,int n,FILE *fp); stdio.h str is returned if successful, NULL if unsuccessful. This fuction reads either until a newline is read or until n characters are read from a file pointed to by fp into the string str. A NULL character is appended to mark the end of the string, but the newline is not stripped.
fopen FILE *fopen(char *filename, char *mode); stdio.h a file pointer if successful; NULL if unsuccessful. This function is used for opening a file. The string, filename, specifies name of the file and mode specifies how the file is to be accessed as one of the following:
If a file is a binary file, append a b to the mode string; e.g. "rb" for opening a binary input file. In Unix systems, do not append a b; there is no difference between text and binary streams. If a file is opened for update, fflush() must be used between reading and writing.
fprintf
stdio.h number of characters sent to the file if successful; EOF if unsuccessful. This fuction prints arg1 and those following to the file pointed to by fp. It behaves like printf() except that it writes to a file.
fputc int fputc(int ch, FILE *fp); stdio.h ch if successful, EOF if unsuccessful. Outputs a character, ch, to the file pointed to by fp.
fputs int fputs(char *str, FILE *fp); stdio.h> EOF if unsuccessful; non-negative if successful. Function writes a string of characters, str, to a file stream, fp. Characters are written to the file until the NULL character is reached. The NULL character is not written to the file. If a newline character is in the string, it is not stripped.
free void free(void *ptr); stdlib.h void This fuction releases the block of memory pointed to by ptr, previously allocated by calloc() or malloc(). Releaseing an allocated block of memory places the block back into the free memory pool of the heap.
fread
stdio.h number of items read; NULL if error or end of file. This function reads no_items, each of size, size, bytes from stream, fp, into buffer.
freopen
stdio.h stream if successful; NULL if unsuccessful. The function opens filename with specified mode and associates stream to the file. This function is typically used to change the files associated with stdin, stdout, and stderr to specified files.
frexp
double frexp(double x, int *exp);
math.h
see description
Splits x into a mantissa, m, and an exponent, n, of base 2,
such that x is
. The mantissa, i.e. fractional part, is less than 1 and not less than
0.5. It returns m and stores n into *exp.
fscanf
stdio.h number of items read if successful; EOF if end of file. This function reads values from stream and stores them where the arguments point. The arguments arg1 and those following must be pointers. It behaves like scanf() except that it reads from a file.
fseek
stdio.h 0 if successful; nonzero on failure. The function, fseek, sets the file pointer for stream to a new position that is offset bytes from the location specified by whence; whence must be 0 for the beginning, 1 for the current poistion, or 2 for the end of file.
fsetpos int fsetpos(FILE *stream, const fpos_t *pos); stdio.h 0 on success; a nonzero on failure. Sets the file pointer for stream to a new position whose value was obtained by fgetpos() for that stream. It also clears end of file indicator for the stream.
fwrite
stdio.h the number of objects written if successful; less than no_objs on error or end of file. This function writes no_objs objects of size, size, from buffer to stream.
gets char *gets(char *str); stdio.h str if successful, EOF if unsuccessful. This function reads a line from the standard input. Characters are read into str until the newline character is encountered. A NULL character is appended to mark the end of the string and the newline character is discarded.
isalnum int isalnum(int ch); ctype.h non-zero if ch is alphanumeric, zero if ch is not alphanumeric. This function checks to see if the character, ch, is alphabetic ( 'A'..'Z' or 'a'..'z') or numeric ( '0'..'9').
isalpha int isalpha(int ch); ctype.h non-zero if ch is alphabetic, zero if ch is not alphabetic. This function checks to see if the character, ch, is alphabetic ( 'A'...'Z' or 'a'...'z').
isascii int isascii(int c); ctype.h non-zero if c is ASCII; zero if c is not ASCII. This function checks to see if the low order byte of c is in the range 0-127.
iscntrl int iscntrl(int c); ctype.h non-zero if c is a control character/delete character; 0 otherwise. This function checks to see if c is a control character, 0-31, or a delete character 127.
isdigit int isdigit(int ch); ctype.h non-zero if ch is a digit, zero if ch is not a digit. This function checks if ch is a digit character ( '0'...'9').
Note: x > 0.
Note: x > 0.
modf double modf(double x, double *intptr); math.h the signed fractional portion of x. The function breaks x into fractional and integral parts each with the same sign as x. The integral part is stored in *intptr and the fractional part is returned.
malloc void *malloc(unsigned size); stdlib.h pointer to a block of memory allocated if successful; NULL if memory not available. This function allows a program to obtain a block of memory from the heap. The size of the block is size bytes. Use sizeof operator to determine the size of a type.
perror void perror(const char *s); stdio.h void Prints first s and then an error message to stderr stream corresponding to the latest error.
Note: Domain error if:
printf int printf(char *format_string, ...); stdio.h Number of bytes output; EOF if unsuccessful. Outputs values of a variable number of arguments that follow format_string which describes how the output will be formatted for each following argument. The format string consists of ordinary characters and conversion specifications. Ordinary characters are simple copied to stdout. Comversion specifications provide the format information for outputting values. A conversion specification starts with a % and ends with a conversion character. In between the two can be a flag, width, precision, and a size modifier.
Default justification is right justification with blanks to pad on the left.
Note: These conventions apply to all formatted output functions fprintf(), and sprintf(). The function, sprint(), generates it's output to a string passed as the first argument (a pointer to the string) and the output to a string should be one call to sprintf().
putc int putc(int ch, FILE *stream); stdio.h ch if successful; EOF if unsuccessful. This is a macro defined in < stdio.h> which outputs character, ch to stream.
putchar int putchar(int ch); stdio.h ch if successful; EOF if unsuccessful. It is a macro that writes ch to standard output.
puts int puts(char *str); stdio.h non-negative value if successful; EOF if unsuccessful. Writes a NULL terminated str to stdout. The NULL is not written, and a newline is appended.
qsort
stdlib.h
void
Uses quicksort to sort an array. A pointer to the base, i.e.
element, is given by base.
Argument, nelem, is the number of elements in the table.
Argument, width, is the size of each element in bytes.
Argument, *fcmp, is the comparison function that
returns a positive, zero, or a negative value if the first dereferenced
argument is greater than, equal to, or less than the second dereferenced
argument.
realloc char *realloc(char *oldptr,int size); stdlib.h pointer to new block of memory if successful; NULL if insufficient memory. The realloc() function changes the size of a previously allocated memory block, *oldptr, to size. The returned pointer points to the begining of the new block of memory. The contents of the block are unchanged up to the shorter of the new and old sizes.
remove int remove(char *filename); stdio.h 0 if successful; non-zero if it fails. Removes the file, filename.
rename int rename(char *old, char *new); stdio.h non-zero if it fails, 0 if successful. Changes the name of a file from old to new.
rewind int rewind(FILE *stream); stdio.h 0 if successful; nonzero otherwise. Positions the file pointer in stream to the beginning of the file and clears error and end of file indicators.
scanf int scanf(char *format_string, ...); stdio.h number of items read if successful; EOF if end of file. This fuction reads values from standard input and stores them where the arguments point. The argument, format_string, specifies how the input fields are to be interpreted. It consists of white space, non-white space, and conversion specifications. Except for character conversion, it skips over all leading white space characters until a non-white space is encountered. It matches and discards all non-white space characters. Conversion specifications are used to convert the values in the input and store them where the arguments point.
Conversion specifications start with a % and end with a conversion character. They may optionally include:
Conversion characters are d, u, o, x, f, lf (for double), c, s. Refer to Chapter 8 for details and examples.
Note: The above discussion applies to fscanf() and sscanf(). Read from a string using a single call to sscanf().
setbuf void setbuf(FILE *stream, char *buffer); stdio.h void This function allows the user to control buffering for stream. The function must be called after the file is opened but before any I/O operation is performed. If the buffer argument is NULL, I/O to the file associated with stream is unbuffered. If not, buffer must point to a character array of length BUFSIZ, the buffer size as defined in < stdio.h>.
Note: x >= 0.
srand void srand(unsigned seed); stdlib.h void Seeds the pseudo-random generator.
strcat char *strcat(char *dest, char *src); string.h pointer to the concatenated string, dest
This function concatenates two strings. The NULL character at the end of dest is removed and src is appended to dest; the result is stored in dest.
strcmp int strcmp(char *str1, char *str2); string.h negative if str1 < str2; 0 if str1 == str2; positive if str1 > str2
The two strings, str1 and str2 are scanned and corresponding characters are compared. The scan stops when there is a mismatch or a NULL character is reached. When the scan stops, the difference between the corresponding characters of str1 and str2 is returned.
strcpy char *strcpy(char *dest, const char *src); string.h dest This function copies characters from src to dest until a NULL character is encountered in src. NULL is appended to dest.
in: <string.h> Returns: dest
in: <string.h> Returns: first n characters of s1 and s2 are compared, and either zero or the difference of the first unequal characters is returned, The value returned is:
strncpy
string.h dest The above three functions perform the same operations as strcat(), strcmp(), and strcpy(), except that n specifies the number of characters that are concatenated, compared, or copied. When copying or concatenating, a NULL is not appended unless the length of src is less than n.
in: <string.h> Returns: A pointer to the first occurrence in s1 of any of the characters of s2. It returns NULL if s1 does not contain any of the characters in s2.
in: <string.h> Returns: the length of the initial part of s1 that consists entirely of characters from s2.
in: <string.h> Returns: the length of the initial part of s1 that consists entirely of characters not in s2.
strtod double strtod(const char *s, char **endptr); stdlib.h value of s as a double. Scans s and converts characters to double; s may be in decimal or exponential format. String scan stops when a character that does notbelong to a double is encountered. If endptr is not NULL, it sets *endptr to point to the first character that stopped the scan.
strtok char *strtok(char *s, const char *delimit); string.h a pointer to the token or NULL if there is no token. Finds tokens in s delimited by one or more characters contained in delimit. After the first call, it puts a NULL character in s right after the first token and returns a pointer to the first character of the first token. Subsequent calls with NULL for the first argument will work through the string, s, in the above manner until no token remains at which point a NULL pointer is returned.
strtol
stdlib.h The long equivalent of the first part of s.
Note: The first part is the leading part of a string that satisfies a property. The reamining part is the part after the first part. Converts the first part of string, s to long and stores the remaining part of s into *endp. If base is zero, then a basae of 8, 10, or 16 is assumed with a leading 0 in s specifying octal, a leading 0X specifying a hexadecimal. Otherwise, base can be between 2 and 36.
in: <stdlib.h> Returns: same as strtol() except that an unsigned long is returned.
tmpfile FILE *tmpfile(void); stdio.h a file pointer if successful; returns NULL otherwise. Creates a temporary file of mode wb+ which will be removed when closed or when the program terminates normally.
ungetc int ungetc(int c, FILE *stream); stdio.h character c if successful; EOF if unsuccessful. This function pushes character, c, back onto stream. The next character read from stream will be c. A maximum of one character per stream may be allowed to be pushed back.