[C] 06-standard library Overview

Source: Internet
Author: User
Tags mathematical functions

Any program will have some general functional requirements, and the implementation of these requirements constitute a library. It can improve the reusability, robustness and portability of the program, which is also the embodiment of modular design. The C Specification defines some common interface libraries. Here, we will only give an overview of them. Of course, you should refer to the specification for details.

To improve efficiency, many library functions in C have a macro-defined version at the same time. Therefore, when passing parameters, try to use expressions without any side effects to avoid errors. If you do not want to useMacro-version Functions, You can use three methods: (1) UNDEF macro first; (2) Name of the function is enclosed in parentheses; (3) the header file is not included.

// method 1#include <stdlib.h>#undef atoiatoi("123");// method 2#include <stdlib.h>(atoi)("123");// method 3extern int atoi(const char*);atoi("123");

Input/Output Library<Stdio. h>Including file operations, stream control, and input and output. All read and write objects of C are called streams. All its information is contained in the struct file, including buffer and location information. The system has three global pointer variables (stdin, stdout, and stderr) pointing to a predefined stream, which is the default stream of scanf, printf, and assert. Operations on files in the Library include deletion, renaming, opening, closing, and buffer settings. Stream Control includes refresh, location acquisition and setting, and error handling. Input and Output include direct input and output, input and output of characters and strings, and formatted input and output. The corresponding wide character input and output are defined in <wchar. h>. The standard library does not strictly check the buffer boundary. For more secure interfaces, see ISO/iec tr 24731. Taking printf as an example, formatted output provides controllable output of metric types. The format of scanf is simpler than that of printf. It is not described. You only need to note that the blank space only serves as the separator. The format is% [Flags] [width] [. Precision] [length] TypeSquare brackets are optional. The meanings are shown in the following table. During parameter transfer, integer lifting, float conversion to double, pointer conversion to void * are performed first, and length and type are jointly determined.Final output typeInstead of the original data type.

Flag - Left alignment; right alignment by default
+ Display symbols. By default, only negative signs are displayed.
Space Spaces are used to display positive signs without symbols.
# The prefix is displayed in hexadecimal notation 8/16, and the floating point always shows the decimal point.
0 Integer and floating point leading 0 fill, if left alignment or integer precision is not filled
Width Positive Integer ,* The minimum width displayed. * indicates that the minimum width is determined by the parameter.
Precision Positive Integer, null The minimum number of integers, the maximum valid bits of g/g, and the decimal places of other floating point numbers. If it is null, it indicates 0.
Length HH, H, l, ll, J, Z, T The integers are Char/short/long/intmax_t/size_t/ptrdiff_t, n is the corresponding pointer, LC is wint_t, LS is wchar_t
L The floating point is long double.
Type D, I, O, U, X, X Integer. d/I is a signed integer, and O/u/X is an unsigned integer in hexadecimal notation of 8/10/16.
F, F, E, E, G, G, A, Floating Point Number. f/F indicates a 10-digit floating point. E/E indicates a 10-digit scientific notation. G/G indicates that the first two are short. A/A indicates a 16-digit scientific notation.
C, S C is a character, and S is a string
P Pointer
N Number of printed characters written to the integer pointer Parameter
% %

Variable Parameter Library<Stdarg. h>The old name <varargs. h> provides some macros to obtain implicit parameters. The pressure stack sequence of function parameters is based on implementation. Therefore, the compiler is also responsible for Variable Parameter management. The address of the parameter sequence information is stored in va_list (pointer), which is obtained from the parameter through va_start or copied using va_copy. Va_arg gets a type of parameter from the list header and deletes it from the list after obtaining the parameter. Va_clean completely releases the list information.

// interfaces of <stdarg.h>void va_start(va_list ap, paraN);void va_copy(va_list dst, va_list src);type va_arg(va_list ap, type);void va_end(va_list ap);// example#include <stdarg.h>#include <stdio.h>void fun(int n, ...){    va_list va, copy;    int a;    va_start(va, n);    va_copy(copy, va);    a = va_arg(va, int);    va_end(va);    va_end(copy);}

Common function library<Stdlib. h>It provides many common interfaces, which were originally the base camp of the standard library. Including String Conversion, random number generation, dynamic memory management, system control and calling, sorting search algorithm, absolute value and division of integers, conversion of wide characters and multi-byte characters, etc. General macro definition<Stddef. h>Contains some common macro definitions, including ptrdiff_t, size_t, wchar_t, null, offsetof, max_align_t, and so on. Time Library<Time. h>Including time and date acquisition and formatting, localization Library<Local. h>You can configure C to conform to the writing style of each region. The default value is setlocal (lc_all, "C") during initialization ").

// some interfaces in <stdlib.h>int atoi(const char* nptr);double atof(const char* nptr);int rand(void);void srand(unsigned int seed);void* malloc(size_t size);void* realloc(void* ptr, size_t size);_Noreturn void abort(void);int system(const char* string);void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));int abs(int j);div_t div(int numer, int denom);int mbtowc(wchar_t* restrict pwc, const char* restrict s, size_t n);

Range Library<Limits. h>Defines the value range of all types of integer types, integer Type Library<Stdint. h>Defines integer types and their value ranges. Integer Library<Inttypes. h>Including <stdint. h>, and defining format macros for various integer types in formatting input and output, as well as the common libraries corresponding to these integer types (<stdlib. h> ). Floating Point Library<Float. h>Define the specific details of the platform floating point representation, including the scope of each domain. Floating Point Environment Library<Fenv. h>Including the acquisition and configuration of the floating point computing environment, as well as the status and exception information of the current floating point operation.

// some macros in <limits.h>#define CHAR_BIT     8#define SCHAR_MIN    -128#define INT_MAX      (-2147483647 - 1)// some macros in <stdint.h>#define int32_t      int#define intmax_t     long long#define INT32_MAX    INT_MAX// some macros in <inttypes.h>#include <stdint.h>#define PRId64 "lld"intmax_t imaxabs(intmax_t j);intmax_t strtoimax(const char* restrict nptr, char** restrict endptr, int base);

Mathematical function library<Math. h>It defines mathematical functions of various floating-point numbers and function libraries of plural numbers.<Complex. h>Defines macros related to the plural and mathematical functions of the plural,<Tgmath. h>Contains the first two and uses a unified macro for the same function (Same name as <math. h> ).

Character Type Library<Ctype. h>And wide Character Library<Wctype. h>It provides an interface to determine the character type and can also perform case conversion. String Library<String. h>Provides operations on strings (memory segments), including copying, connecting, and searching. Wide Character Library<Wchar. h>It contains the wide character versions of various functions, including formatting input and output, string operations, time display, conversion of wide characters and single characters, etc. Unicode library<Uchar. h>Including Unicode operations and conversion, and Symbol Library<Iso646.h>Provide a Word Representation for symbols not found in iso_646, for example, and representation &&.

// some interfaces in <ctype.h>int isdigit(int c);int islower(int c);int toupper(int c);// some interfaces in <string.h>void* memcpy(void* restrict s1, const void* restrict s2, size_t n);char* strcpy(char* restrict s1, const char* restrict s2);char* strcat(char* restrict s1, const char* restrict s2);int memcmp(const void* s1, const void* s2, size_t n);char* strstr(const char* s1, const char* s2);

Long Jump Library<Setjmp. h>Provides an interface for directly redirecting between functions to quickly return to the old execution point. Jmp_buf is an array type that stores the required information for redirection. Setjmp saves the current vertex, and longjmp jumps to a certain point in time. Setjmp returns 0, indicating that it is returned from setjmp; otherwise, it is redirected from longjmp. Signal Library<Signal. h>Provides an interface for registering a signal callback function and initiating a signal to the system to check the database.<Assert. h>Check the conditions during running or compiling and report an error. Error code library<Errno. h>The global variables defined in record the number of the last error.

#include <setjmp.h>jmp_buf buf;void f(void) {g();}void g(void) {longjmp(buf, 1);}int main (void){    if (0 == setjmp(buf)) f();    // from setjmp    else {}                       // from longjmp}

Library<Stdbool. h>,<Stdalign. h>And<Stdnoreturn. h>Defines all lowercase macros for newly added keywords. Library<Threads. h>And<Stdatomic. h>Provides macros and interfaces related to threads and atomic operations. The corresponding content is ignored here (not carefully read or understood ), please refer to the standard content (including the use of the keyword _ thread_local and _ atomic ).

 

[Completed]

 

[C] 06-standard library Overview

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.