匯總 C 標準庫、POSIX、BGI 圖形庫、DOS 擴展庫中以 C 開頭 的常用函數,含原型、功能、頭文件、示例與現代替代提示。
#include // 需定義 struct complex { double x, y; };
double cabs(struct complex z);
// 返回 sqrt(z.x^2 + z.y^2)。C99 內建 _Complex 類型,推薦用 cabs() 配合 complex.h。
#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);
}
#include
double ceil(double x);
// 返回 ≥ x 的最小整數值(double 類型)。配合 floor() 使用。
#include
char *cgets(char *buffer);
// buffer[0] = 最大字符數+1,返回指向 buffer[2] 的指針。非標準,現代用 fgets/getline。
#include // POSIX
#include // Windows (_chdir)
int chdir(const char *path);
// 成功返回 0,失敗 -1 置 errno。配合 getcwd 使用。
#include
int chmod(const char *path, mode_t mode);
// mode: S_IRUSR(0400), S_IWUSR(0200), S_IXUSR(0100) 等組合。
// Windows 僅支援 _chmod 讀寫屬性,無執行位概念。
#include // ftruncate
#include // _chsize (Windows)
int chsize(int fd, long size);
// 截斷或擴展文件至 size 字節。擴展部分填 0。
#include
void far circle(int x, int y, int radius);
// 需 initgraph 初始化。現代用 SDL_RenderDrawCircle 等。
#include
void far cleardevice(void);
// 清整個圖形視口,背景色填充,當前位置移至 (0,0)。
#include
void clearerr(FILE *stream);
// 重置 stream 的錯誤標誌與 EOF 標誌。配合 ferror/feof 使用。
#include
void far clearviewport(void);
// 僅清當前視口(setviewport 設置),非整屏。
#include
int close(int fd);
// 關閉文件描述符,刷新緩衝區。C 標準流用 fclose(FILE*)。
#include
clock_t clock(void);
// 返回進程消耗的 CPU 時間(clock ticks)。秒數 = (end-start)/CLOCKS_PER_SEC。
// 注意:多線程下返回所有線程 CPU 時間和,非墻鐘時間。
#include
void far closegraph(void);
// 釋放圖形驅動內存,恢復文本模式。
#include
void clreol(void);
// 從光標位置清至行尾,光標不動。非標準。
#include
void clrscr(void);
// 清文本屏幕,光標歸 (1,1)。非標準,現代用 system("cls")/ANSI 轉義序列。
#include // 或 malloc.h
unsigned coreleft(void);
// 返回堆頂與最高分配塊之間空間。現代系統虛擬記憶體意義有限。
#include
double cos(double x); // 弧度制
double cosh(double x); // (e^x + e^-x)/2
#include
struct COUNTRY *country(int code, struct COUNTRY *cp);
// 返回貨幣符號、日期格式等。現代用 locale.h / ICU 庫。
#include
int cprintf(const char *fmt, ...);
void cputs(const char *str);
// 輸出到當前文本視口(window)。非標準。
#include
#include
int creat(const char *path, mode_t mode);
// 等價於 open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)。現代用 open/fopen。
#include
int creatnew(const char *path, int attrib);
// 文件存在時失敗。現代用 open(path, O_CREAT|O_EXCL, ...)。
#include
int creattemp(char *path, int attrib);
// 在 path 目錄創建唯一文件名。現代用 mkstemp/tmpfile。
#include
int cscanf(const char *fmt, ...);
// 從控制台讀取,回顯。非標準。
#include
char *ctime(const time_t *timer);
// 等價於 asctime(localtime(timer))。返回靜態緩衝區,非線程安全。
// 現代用 strftime/ctime_r。
#include
void ctrlbrk(int (*handler)(void));
// 設置 INT 1BH 處理器。現代用 signal(SIGINT)/SetConsoleCtrlHandler。
| 舊函數 | 現代替代 | 頭文件/庫 |
|---|---|---|
| calloc | calloc (標準保留) | stdlib.h |
| chdir/chmod/close | 同名 POSIX 函數 | unistd.h |
| chsize | ftruncate | unistd.h |
| circle/cleardevice/closegraph | SDL2/OpenGL/Vulkan | SDL2/GL |
| clearerr | clearerr (標準保留) | stdio.h |
| clock | clock_gettime(CLOCK_PROCESS_CPUTIME_ID) | time.h |
| clrscr/clreol/cprintf | ANSI 轉義序列 / ncurses | stdio.h / ncurses.h |
| cos/cosh | 同名 (標準保留) | math.h |
| creat | open(O_CREAT|O_WRONLY|O_TRUNC) | fcntl.h |
| creatnew | open(O_CREAT|O_EXCL) | fcntl.h |
| creattemp | mkstemp / tmpfile | unistd.h / stdio.h |
| ctime | ctime_r / strftime | time.h |
| ctrlbrk | signal(SIGINT) / SetConsoleCtrlHandler | signal.h / windows.h |