C標準庫常用標頭檔案及函式總結

2021-10-07 04:25:35 字數 3896 閱讀 8982

本文我們總結一下c標準庫常用的介面標頭檔案及函式,包含c99和c11標準。

1. 斷言 assert.h  (程式錯誤檢測)

void assert(int exprs)  如果exprs表示式為真,則什麼也不做,如果exprs表示式為假,則編譯器顯示表示式所在的檔名和行號,然後打斷程式執行。

2. ctype.h(字元型別判斷)

int  isalnum(int c) 如果c 是字母或數字,返回真。

int isalpha(int c)如果c是字母,返回真。

int isdigit(int c)如果c是數字,返回真。

int isspace(int c) 如果c是空白字元,返回真。

int tolower(int c)如果c是大寫,則返回小寫。

int toupper(int c) 如果c是小寫,返回大寫。

3.math.h(數學庫)

double pow(double x,double y) 返回x的y次冪。

double sqrt(double x) 返回x的平方根

double fabs(double x)返回x的絕對值

double sin(double x) 返回x的正弦值。

double cos(double x)返回x的余弦值

double tan(double x)返回x的正切值

double log(double x) 返回x的自然對數

4. stdbool.h(布林型別)

bool   、true(展開為int 1)、false(展開為int 0)

5. stdint.h(精準資料型別)

int8_t 、int16_t、int32_t、int64_t 、uint8_t、uint16_t 、uint32_t 、uint64_t

6. stdio.h(標準io庫)

標準輸入輸出:

int printf(const char *restrict,.....) 格式化輸出

int scanf(const char *restrict,.......)格式化輸入

字串標準輸入輸出:

int sprintf(char *restrict,const char *restrict,.....)格式化輸出到字串中

int snprintf(char *restrict,size_t n,char *restrict,.....)格式化輸出前n個字元到字串

檔案操作:

fopen(const char *restrict,const char *restrict)開啟指定檔案

fclose(file *)關閉指定檔案

size_t fread(void *restrict ,size_t size,file *restrict)讀取檔案流中的資料

size_t fwrite(const void *restrict , size_t size, file *restrict)向檔案流中寫入資料

int fseek(file *restrict,int pos)設定當前檔案指標位置

int ftell(file *restrict)獲取當前檔案指標位置

7. stdlib.h(通用檔案 malloc 、free等)

null (空指標定義)

int abs(int n)返回絕對值

void *malloc(size_t size) 分配記憶體空間,失敗返回null

void free(void *ptr)釋放記憶體空間

void *calloc(size_t nmem, size_t size)為包含nmem個成員的陣列分配空間,每個元素佔size個位元組大小,並初始化為0。

void srand(unsigned int seed)設定隨機數生成器種子

void rand(void)返回0~rand_max的乙個偽隨機數

double atof(const char *nptr) 字串轉浮點型別

int atoi(const char *nptr)字串轉整形

int atol(const char *nptr)字串裝長整形

8. string.h(字串處理)

記憶體處理:

int memcpy(void *s1,const void *s2,size_t n) 記憶體拷貝函式

void *memset(void *s1,int v,size_t size)記憶體初始化函式

int memcpy(const void *s1, const void *s2, size_t n)記憶體比較函式

int *memchr(const void *s,int c ,size_t n)查詢字元c 首次出現的位置,返回位址。

字串處理:

char *strcat(char *s1,const char *s2)字串拼接函式

char *strncat(char *s1,const char *s2,size_t n)從s2值拷貝n個字元到s1末尾,返回s1.

char *strcpy(char *s1,const char s2) 字串拷貝,返回s1

char *strncpy(char *s1,const char s2,size_t n)從s2拷貝n個字元到s1,返回s1.

int strcmp(const char *s1,const char *s2)字串比較函式,相同返回0.

int strncmp(const char *s1, const char *2,size_t n)比較前n個字元,相同返回0.

char * strchr(const char *s1,int c) 查詢字串s1中,字元c首次出現的位置,並返回指標,查詢不到返回null。

char *strstr(const char *s1,const char *s2) 查詢字串s1中字串s2首次出現的位置,並返回指標,查詢不到,返回null。

int strlen(const char *s)返回字串長度,不包含'\0'.

9. time.h

定義型別: time_t 、struct tm、struct timespc、clock_t.

標準 C 常用標頭檔案

include 通用演算法 include 位集容器 include 字元處理 include 數學公式 include 複數類 include 標準c的輸入輸出 include 定義雜項函式及記憶體分配函式 include 字串 include 時間函式 include 雙端佇列容器 includ...

C 標準庫標頭檔案

包含c 標準輸入和輸出函式的函式原型,並已取代了標頭檔案 格式化資料流的的流操縱元的函式原型,並以取代了標頭檔案 顧名思義,它包含各個數學函式原型,並已取代 它包含數轉換為文字,文字轉換為數,記憶體分配 隨機數 以及其他各種攻取函式的的函式原型並取代了標頭檔案 該標頭檔案包含維護時間和日期函式原型和...

C常用標準庫及函式

常用標頭檔案 stdio.h stdlib.h ctype.h string.h math.h system.h 1 stdio庫 主要功能是輸入輸出操作,包括檔案輸入輸出,標準輸入輸出。輸出函式 int puts const char str 把乙個字串寫出到標準輸出 int printf cha...