首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C標準庫函數速查手冊(C 開頭):cabs/calloc/ceil/cgets/chdir/chmod/chsize/circle/cleardevice/clearerr/clearviewport/close/clock/closegraph/clreol/clrscr/coreleft/cos/cosh/country/cprintf/cputs/creat/creatnew/creattemp/cscanf/ctime/ctrlbrk

     編輯:關於C語言

C 開頭標準庫函數速查

匯總 C 標準庫、POSIX、BGI 圖形庫、DOS 擴展庫中以 C 開頭 的常用函數,含原型、功能、頭文件、示例與現代替代提示。

函數列表

cabs - 複數絕對值


#include   // 需定義 struct complex { double x, y; };
double cabs(struct complex z);
// 返回 sqrt(z.x^2 + z.y^2)。C99 內建 _Complex 類型,推薦用 cabs() 配合 complex.h。

calloc - 零初始化動態分配


#include 
void *calloc(size_t nmemb, size_t size);
// 分配 nmemb * size 字節並清零。等價於 malloc + memset(0)。
// 整數溢出檢查:nmemb > SIZE_MAX/size 時行為未定義。
int main() {
    int *p = calloc(100, sizeof(int));  // 100 個 0
    free(p);
}

ceil - 向上取整


#include 
double ceil(double x);
// 返回 ≥ x 的最小整數值(double 類型)。配合 floor() 使用。

cgets - 控制台讀字符串 (conio.h, DOS/Windows)


#include 
char *cgets(char *buffer);
// buffer[0] = 最大字符數+1,返回指向 buffer[2] 的指針。非標準,現代用 fgets/getline。

chdir - 改變工作目錄


#include   // POSIX
#include   // Windows (_chdir)
int chdir(const char *path);
// 成功返回 0,失敗 -1 置 errno。配合 getcwd 使用。

chmod - 改變文件權限


#include 
int chmod(const char *path, mode_t mode);
// mode: S_IRUSR(0400), S_IWUSR(0200), S_IXUSR(0100) 等組合。
// Windows 僅支援 _chmod 讀寫屬性,無執行位概念。

chsize - 改變文件大小 (POSIX: ftruncate)


#include   // ftruncate
#include       // _chsize (Windows)
int chsize(int fd, long size);
// 截斷或擴展文件至 size 字節。擴展部分填 0。

circle - 繪圓 (BGI)


#include 
void far circle(int x, int y, int radius);
// 需 initgraph 初始化。現代用 SDL_RenderDrawCircle 等。

cleardevice - 清除圖形屏幕 (BGI)


#include 
void far cleardevice(void);
// 清整個圖形視口,背景色填充,當前位置移至 (0,0)。

clearerr - 清除流錯誤/EOF 標誌


#include 
void clearerr(FILE *stream);
// 重置 stream 的錯誤標誌與 EOF 標誌。配合 ferror/feof 使用。

clearviewport - 清除圖形視口 (BGI)


#include 
void far clearviewport(void);
// 僅清當前視口(setviewport 設置),非整屏。

close - 關閉文件描述符 (POSIX)


#include 
int close(int fd);
// 關閉文件描述符,刷新緩衝區。C 標準流用 fclose(FILE*)。

clock - CPU 時間


#include 
clock_t clock(void);
// 返回進程消耗的 CPU 時間(clock ticks)。秒數 = (end-start)/CLOCKS_PER_SEC。
// 注意:多線程下返回所有線程 CPU 時間和,非墻鐘時間。

closegraph - 關閉圖形系統 (BGI)


#include 
void far closegraph(void);
// 釋放圖形驅動內存,恢復文本模式。

clreol - 清除行尾 (conio.h)


#include 
void clreol(void);
// 從光標位置清至行尾,光標不動。非標準。

clrscr - 清屏 (conio.h)


#include 
void clrscr(void);
// 清文本屏幕,光標歸 (1,1)。非標準,現代用 system("cls")/ANSI 轉義序列。

coreleft - 獲取堆剩餘空間 (舊式堆管理)


#include   // 或 malloc.h
unsigned coreleft(void);
// 返回堆頂與最高分配塊之間空間。現代系統虛擬記憶體意義有限。

cos / cosh - 余弦 / 雙曲余弦


#include 
double cos(double x);   // 弧度制
double cosh(double x);  // (e^x + e^-x)/2

country - 獲取國家信息 (DOS)


#include 
struct COUNTRY *country(int code, struct COUNTRY *cp);
// 返回貨幣符號、日期格式等。現代用 locale.h / ICU 庫。

cprintf / cputs - 控制台格式化/字符串輸出 (conio.h)


#include 
int cprintf(const char *fmt, ...);
void cputs(const char *str);
// 輸出到當前文本視口(window)。非標準。

creat / _creat - 創建/截斷文件 (舊式)


#include 
#include 
int creat(const char *path, mode_t mode);
// 等價於 open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)。現代用 open/fopen。

creatnew - 唯一創建文件 (DOS)


#include 
int creatnew(const char *path, int attrib);
// 文件存在時失敗。現代用 open(path, O_CREAT|O_EXCL, ...)。

creattemp - 創建臨時文件 (DOS)


#include 
int creattemp(char *path, int attrib);
// 在 path 目錄創建唯一文件名。現代用 mkstemp/tmpfile。

cscanf - 控制台格式化輸入 (conio.h)


#include 
int cscanf(const char *fmt, ...);
// 從控制台讀取,回顯。非標準。

ctime - time_t 轉字符串


#include 
char *ctime(const time_t *timer);
// 等價於 asctime(localtime(timer))。返回靜態緩衝區,非線程安全。
// 現代用 strftime/ctime_r。

ctrlbrk - 設置 Ctrl-Break 處理器 (DOS)


#include 
void ctrlbrk(int (*handler)(void));
// 設置 INT 1BH 處理器。現代用 signal(SIGINT)/SetConsoleCtrlHandler。

現代替代指南

舊函數現代替代頭文件/庫
calloccalloc (標準保留)stdlib.h
chdir/chmod/close同名 POSIX 函數unistd.h
chsizeftruncateunistd.h
circle/cleardevice/closegraphSDL2/OpenGL/VulkanSDL2/GL
clearerrclearerr (標準保留)stdio.h
clockclock_gettime(CLOCK_PROCESS_CPUTIME_ID)time.h
clrscr/clreol/cprintfANSI 轉義序列 / ncursesstdio.h / ncurses.h
cos/cosh同名 (標準保留)math.h
creatopen(O_CREAT|O_WRONLY|O_TRUNC)fcntl.h
creatnewopen(O_CREAT|O_EXCL)fcntl.h
creattempmkstemp / tmpfileunistd.h / stdio.h
ctimectime_r / strftimetime.h
ctrlbrksignal(SIGINT) / SetConsoleCtrlHandlersignal.h / windows.h
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved