Library

This is a summary of the library defined by the ANSI standard. The standard library is not part of the C language proper, but an environment that supports standard C will provide the function declarations and type and macro definitions of this library.
The functions, types and macros of the standard library are declared in standard headers:



A header can be accessed by
#include

Headers may be included in any order and any number of times. A header must be included outside of any external declaration or definition and before any use of anything it declares. A header need not be a source file. External identifiers that begin with an underscore are reserved for use by the library, as are all other identifiers that begin with an underscore and an uppercase letter or another underscore.
Input and Output:
The input and output functions, types, and macros defined in represent nearly one third of the library. . When a program begins execution, the three streams stdin, stdout, and stderr are already open.

File Operations
The following functions deal with operations on files.
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r" open text file for reading
"w" create text file for writing; discard previous contents if any
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e., reading and writing)
"w+" create text file for update, discard previous contents if any
"a+" append; open or create text file for update, writing at end
freopen opens the file with the specified mode and associates the stream with it. It returns stream, or NULL if an error occurs. freopen is normally used to change the files associated with stdin, stdout, or stderr.
int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream, the effect is undefined.
int fclose(FILE *stream)
fclose flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream. It returns EOF if any errors occurred, and zero otherwise.
int remove(const char *filename)
remove removes the named file, so that a subsequent attempt to open it will fail. It returns non-zero if the attempt fails.
int rename(const char *oldname, const char *newname)
rename changes the name of a file; it returns non-zero if the attempt fails.
Formatted Output
The printf functions provide formatted output conversion.
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes output to stream under the control of format.

Formatted Input
The scanf function deals with formatted input conversion.
int fscanf(FILE *stream, const char *format, ...)
fscanf reads from stream under control of format, and assigns converted values through subsequent arguments, each of which must be a pointer.

Character Input and Output Functions
fgetc returns the next character of stream as an unsigned char (converted to an int), or EOF if end of file or error occurs.
fgets reads at most the next n-1 characters into the array s, stopping if a newline is encountered; the newline is included in the array, which is terminated by '\0'
fputc writes the character c (converted to an unsigend char) on stream. It returns the character written, or EOF for error.
fputs writes the string s (which need not contain \n) on stream; it returns nonnegative, or EOF for an error.
getc is equivalent to fgetc except that if it is a macro, it may evaluate stream more than once.

Direct Input and Output Functions
fread reads from stream into the array ptr at most nobj objects of size size. fread returns the number of objects read; this may be less than the number requested.
feof and ferror must be used to determine status.
fwrite writes, from the array ptr, nobj objects of size size on stream. It returns the number of objects written, which is less than nobj on error.

File Positioning Functions
fseek sets the file position for stream; a subsequent read or write will access data beginning at the new position.
ftell returns the current file position for stream, or -1 on error.
void rewind(FILE *stream)
rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); clearerr(fp).
fgetpos records the current position in stream in *ptr, for subsequent use by fsetpos.

Character Class Tests:
The header declares functions for testing characters. For each function, the argument list is an int, whose value must be EOF or representable as an unsigned char, and the return value is an int.

isalnum(c) isalpha(c) or isdigit(c) is true
isalpha(c) isupper(c) or islower(c) is true
iscntrl(c) control character
isdigit(c) decimal digit
isgraph(c) printing character except space
islower(c) lower-case letter
isprint(c) printing character including space
ispunct(c) printing character except space or letter or digit
isspace(c) space, formfeed, newline, carriage return, tab, vertical tab
isupper(c) upper-case letter
isxdigit(c) hexadecimal digit

In addition, there are two functions that convert the case of letters:

int tolower(c) convert c to lower case
int toupper(c) convert c to upper case
If c is an upper-case letter, tolower(c) returns the corresponding lower-case letter,
toupper(c) returns the corresponding upper-case letter; otherwise it returns c.

String Functions:
These are the groups of string functions defined in the header .
char *strcpy(s,ct) copy string ct to string s, including '\0'; return s.
char *strcat(s,ct) concatenate string ct to end of string s; return s.
int strcmp(cs,ct) compare string cs to string ct, return <0 cs="="ct,">0
char *strchr(cs,c) return pointer to first occurrence of c in cs or NULL if not present.
strspn(cs,ct) return length of prefix of cs consisting of characters in ct.
size_t
char *strstr(cs,ct) return pointer to first occurrence of string ct in cs, or NULL if not present.
strlen(cs) return length of cs.

Mathematical Functions:
The header declares mathematical functions and macros.
In the following table, x and y are of type double, n is an int, and all functions return double. Angles for trigonometric functions are expressed in radians.
sin(x) sine of x
cos(x) cosine of x
tan(x) tangent of x

asin(x) sin-1(x) in range [-pi/2,pi/2], x in [-1,1].
acos(x) cos-1(x) in range [0,pi], x in [-1,1].
atan(x) tan-1(x) in range [-pi/2,pi/2].
atan2(y,x) tan-1(y/x) in range [-pi,pi].
sinh(x) hyperbolic sine of x
cosh(x) hyperbolic cosine of x
tanh(x) hyperbolic tangent of x
exp(x) exponential function ex
log(x) natural logarithm ln(x), x>0.
log10(x) base 10 logarithm log10(x), x>0.
pow(x,y) xy. A domain error occurs if x=0 and y<=0, or if x<0 and y is not an integer.
sqrt(x) sqare root of x, x>=0.
ceil(x) smallest integer not less than x, as a double.
floor(x) largest integer not greater than x, as a double.
fabs(x) absolute value x
ldexp(x,n) x*2n
frexp(x, int *ip)
splits x into a normalized fraction in the interval [1/2,1) which is returned, and a power of 2, which is stored in *exp. If x is zero, both parts of the result are zero.
modf(x, double *ip)
splits x into integral and fractional parts, each with the same sign as x. It stores the integral part in *ip, and returns the fractional part.
Utility Functions:
The header declares functions for number conversion, storage allocation, and similar tasks.
void *calloc(size_t nobj, size_t size)
calloc returns a pointer to space for an array of nobj objects, each of size size, or NULL if the request cannot be satisfied. The space is initialized to zero bytes.
void *malloc(size_t size)
malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized.
Void *realloc(void *p, size_t size)
realloc changes the size of the object pointed to by p to size. The contents will be unchanged up to the minimum of the old and new sizes.
void free(void *p)
free deallocates the space pointed to by p; it does nothing if p is NULL. p must be a pointer to space previously allocated by calloc, malloc, or realloc.
void abort(void)
abort causes the program to terminate abnormally, as if by raise(SIGABRT).

0 comments:

Newer Post Older Post Home