Standard library functions most commonly used in C language

Source: Internet
Author: User
Tags alphabetic character function prototype mathematical functions natural logarithm square root strcmp uppercase letter

Standard header files include:

<asset.h> <ctype.h> <errno.h> <float.h>

<limits.h> <locale.h> <math.h> <setjmp.h>

<signal.h> <stdarg.h> <stddef.h> <stdlib.h>

<stdio.h> <string.h> <time.h>

I. Standard definition (<stddef.h>)

The file <stddef.h> contains some common definitions of the standard library, which are included automatically, regardless of which standard header file we include in the,<stddef.h>.

This file defines:

L type size_t (the result type of the sizeof operator, which is an unsigned integral type);

L Type ptrdiff_t (the result type of the two pointer subtraction operation, is a signed integer type);

L type wchar_t (wide character type, is an integral type that is sufficient to store all encoded values of the character set in all local environments supported by this system. This also guarantees that the encoded value of the null character is 0);

L sign constant null (null pointer value);

L Macro Offsetor (this is a macro with parameters, the first parameter should be a struct type, the second argument should be the struct member name.)

Offsetor (s,m) to find the offset of the member m in the variable of the struct type T).

Note: Some of these definitions also appear in other header files (such as null).

Second, error message (<errno.h>)

<errno.h> defines an int type expression errno, which can be thought of as a variable with an initial value of 0, and some standard library functions perform in error when it is set to a value other than 0, but any standard library function sets it to 0.

<errno.h> also defines two macros Edom and Erange, all of which are non-0 integer values. If a parameter error is encountered in the execution of the mathematical function, the errno will be set to Edom, and if a range error occurs, the errno will be set to Erange.

Input and Output function (<stdio.h>)

File Open and Close:

FILE *fopen (const char *filename, const char *mode);

int fclose (FILE * stream);

Character input and output:

int fgetc (FILE *fp);

int FPUTC (int c, FILE *FP);

Getc and PUTC are similar to these two functions, but are implemented by macro definitions. There are usually the following definitions:

#define GETCHAR () getc (stdin)

#define PUTCHAR (c) PUTC (c, stdout)

int ungetc (int c, file* stream);//return character C to stream

Format input and output:

int scanf (const char *format, ...);

int printf (const char *format, ...);

int fscanf (FILE *stream, const char *format, ...);

int fprintf (FILE *stream, const char *format, ...);

int sscanf (char *s, const char *format, ...);

int sprintf (char *s, const char *format, ...);

Line-in input and output:

Char *fgets (char *buffer, int n, FILE *stream);

int fputs (const char *buffer, FILE *stream);

Char *gets (char *s);

int puts (const char *s);

Direct input and output:

size_t fread (void *pointer, size_t size, size_t num, FILE *stream);

size_t fwrite (const void *pointer, size_t size, size_t num, FILE *stream);

Iv. Mathematical Functions (<math.h>)

Trigonometric function:

Trigonometric function

Sin

Cos

Tan

Inverse trigonometric function

Asin

ACOs

Atan

Hyperbolic functions

Sinh

Cosh

Tanh

Exponential and logarithmic functions:

The exponential function with E as the base

Exp

Natural logarithm function

Log

A logarithmic function with a base of 10

Log10

Other functions:

Square root

sqrt

Absolute

Fabs

The first argument as the bottom, the second is the exponent

Double pow (double, double)

The remainder of the real number, two parameters are divided by divisor and divisor

Double Fmod (double, double)

Note: All functions that do not give a type feature above take a parameter whose parameters and return values are double types.

The following function returns the double value (including function ceil and floor). In the following table, in addition to the parameters that are specifically described, all other parameters of the function are of type double.

Function prototypes

Meaning explanation

Ceil (x)

Find the smallest integer not less than x (returns the double value corresponding to the integer)

Floor (x)

Find the largest integer not greater than x (returns the double value corresponding to the integer)

Atan2 (y, x)

Tan-1 (y/x), the range of values is [-pai,pai]

LDEXP (x, int n)

Find out x*2n

FREXP (x, int *exp)

The x is decomposed into y*2n, which is a decimal in the interval [1/2,1], returned as a function result, and the integer n is returned by the pointer *exp (an int variable address should be provided). When x is 0 o'clock, the values for both results are 0.

MODF (x, double

*IP)

The x is decomposed into fractional and integral parts, the fractional part as the function return value, and the integer part returned by the pointer *ip.

Five, character processing function (<ctype.h>)

See table below:

int Isalpha (c)

C is an alphabetic character

int IsDigit (c)

C is a numeric character

int Isalnum (c)

C is a letter or numeric character

int Isspace (c)

C is a space, tab, line break

int Isupper (c)

C is an uppercase letter

int Islower (c)

c is a lowercase letter

int Iscntrl (c)

C is the control character

int Isprint (c)

c is a printable character, including spaces

int Isgraph (c)

c is a printable character, not including spaces

int Isxdigit (c)

C is a hexadecimal numeric character

int ispunct (c)

C is punctuation

int tolower (int c)

Returns the corresponding lowercase letter when C is an uppercase letter, otherwise returns C itself

int toupper (int c)

Returns the corresponding uppercase letter when C is lowercase, otherwise returns C itself

Note: These functions return a value other than 0 when the condition is established. The last two conversion functions return the original character for a non-alphabetic argument.

Six, String function (<string.h>) string function

All string functions are listed in the table below, and the function description takes the following conventions: S, T (char *) type parameters, CS, CT (const char*) type parameters (they all should represent strings). n represents a parameter of type size_t (size_t is an unsigned integer type), and C is an integer parameter (converted to char in a function):

Function prototypes

Meaning explanation

size_t strlen (CS)

Find out the length of CS

Char *strcpy (S,CT)

Copy the CT to S. Requires S to specify a large enough character array

Char *strncpy (s,ct,n)

Copy up to n characters from the CT to S. Requires S to specify a character array that is large enough. If the characters in the CT are not enough n, fill the empty characters in S.

Char *strcat (S,CT)

Copy the characters in the CT to the existing string in S. s should specify a character array that holds the string and is large enough.

Char *strncat (s,ct,n)

Copy up to n characters in CT to the existing string in S. s should specify a character array that holds the string and is large enough.

int strcmp (CS,CT)

Compare the size of the string CS and CT, return positive, 0, negative values when CS is greater than, equal to, and less than Ct.

int strncmp (CS,CT,N)

Compares the size of the string CS and CT to a maximum of n characters. When CS is greater than, equal to, less than CT, return positive, 0, negative value respectively.

Char *STRCHR (CS,C)

Searches for C in CS and returns the position of the first occurrence of C, expressed as a pointer to the position. return value NULL if C is not in CS

Char *STRRCHR (CS,C)

Searches for C in CS and returns the position of the last occurrence of C, and returns null if no

size_t strspn (CS,CT)

A sequence of all the characters in the CT that is determined by CS to return its length

size_t strcspn (CS,CT)

A sequence of all non-CT characters determined by CS to return its length

Char *strpbrk (CS,CT)

Searches for the characters in the CT in CS, returns the position of the first character that satisfies the condition, and returns null if none is present

Char *strstr (CS,CT)

Search for string CT (query substring) in CS, return the first occurrence of CT as a substring of CS, return NULL if CT does not appear in CS

Char *strerror (n)

Returns the error message string associated with error number n (pointer to the error message string)

Char *strtok (S,CT)

Search for words in s that are formed by characters in CT as separators

Store Operations

<string.h> also has a set of character array manipulation functions (store manipulation functions) whose names start with Mem and are implemented in an efficient manner. In the following prototype, the type of the parameter S and T is (void *), the type of CS and CT is (const void *), the type of n is the size_t,c type is int (converted to unsigned char).

function prototype

< P>void *memcpy (s,ct,n)

Copy n characters from CT to S, return s

void *memmove (s,ct,n)

Copy n characters from CT to S, return s, two segments here allow overlap

int memcmp (cs,ct,n)

< p> compares n characters starting with CS and CT, the return value is defined with strcmp

void *memchr (cs,c,n)

Searches for the first occurrence of C in CS in the range of n characters, returns a pointer value for that position if found, otherwise returns null

void *memset (s,c,n)

Set the first n characters of S to C, return s

Vii. function function (<stdlib.h>)

Random number function:

Function prototypes

Meaning explanation

int rand (void)

Generate a random integer from 0 to Rand_max

void Srand (unsigned seed)

Set seed value for subsequent random number generation with seed

Dynamic Storage allocation functions:

Function prototypes

Meaning explanation

void *calloc (size_t n, size_t size)

Allocates a piece of storage that is large enough to hold n sizes of objects and fills all the bytes with 0 characters. Returns the address of the storage block. Return NULL if not satisfied

void *malloc (size_t size)

Allocates a chunk of storage that is large enough to store, returns the address of the storage block, returns NULL if it cannot be satisfied

void *realloc (void *p, size_t size)

Adjusts the storage block of p to size, and returns the address of the new block. If the requirements are met, the content of the new block is consistent with the original block, and Null is returned when the requirement is not met, and the original block is unchanged.

void free (void *p)

Releasing a previously allocated dynamic storage block

Several integer functions

A few simple integer functions are shown in the following table, div_t and ldiv_t are two predefined struct types that are used to store the quotient and remainder of the divide. The components of the div_t type are of type int quot and rem,ldiv_t are of type long quot and rem.

Function prototypes

Meaning explanation

int abs (int n)

To find the absolute value of an integer

Long Labs (long N)

To find the absolute value of a long integer

div_t Div (int n, int m)

n/m, quotient and remainder are stored in the corresponding members of the result structure respectively.

ldiv_t Ldiv (long n, long m)

Ibid., parameter is a long integer

numeric conversions

Function prototypes

Meaning explanation

Double atof (const char *s)

Construct a double value from string s

int atoi (const char *s)

Constructs an integer value from string s

Long Atol (const char *s)

Construct a long integer value from string s

Execution control

1) Abnormal termination function abort.

The prototype is: void abort (void);

2) normal termination function exit.

The prototype is: void exit (int status);

Causes the program to terminate immediately as normal. Status as the export value to the execution environment, 0 indicates a successful end, and two available constants are exit_success,exit_failure.

3) Normal termination of the registration function atexit.

The prototype is: int atexit (void (*FCN) (void))

You can use this function to register some functions as end actions. The registered function should be a function with no parameter and no return value. Atexit returns a value of 0 when the registration is normal, otherwise a value other than 0 is returned.

Interacting with the execution environment

1) The function system that transmits commands to the execution environment.

The prototype is: int system (const char *s);

The execution environment required to pass the string s to the program executes as a system command. If called with NULL as a parameter, the function returns a non-0 indicating that there is a command interpreter in the environment. If s is not NULL, the return value is determined by the implementation.

2) access to the function getenv of the execution environment.

The prototype is: Char *getenv (const char *s);

Retrieves the environment string associated with the string s from the execution environment. Returns null if it is not found. The specific results of this function are determined by the implementation. In many execution environments, you can use this function to view the value of the environment variable.

Common functions bsearch and qsort

1) Two-way lookup function bsearch:

void *bsearch (const void *key, const void *base, size_t n, size_t size, int (*cmp) (const void *keyval, const void *datum)) ;

function pointer parameter the actual parameter of the CMP should be a function similar to the string comparison function strcmp, which determines the order of sorts, when the first parameter keyval returns a positive, a 0, or a negative value, respectively, than the second argument datum large, equal, or an hour.

2) Quick Sort function Qsort:

void Qsort (void *base, size_t n, size_t size, int (*cmp) (const void *, const void *));

Qsort The requirements for the comparison function CMP are the same as for bsearch. has an array of base[0],..., base[n-1], the size of the element. With Qsort, the elements of this array can be rearranged in ascending order determined by CMP.

Standard library functions most commonly used in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.