匯總 C 標準庫、POSIX、BGI 圖形庫、DOS 擴展庫中以 D 開頭 的常用函數,含原型、功能、頭文件、示例與現代替代提示。
#include // 或
void delay(unsigned milliseconds);
// 忙等待或釋放 CPU(視實現)。現代跨平台用 sleep/usleep/nanosleep/Sleep。
// 精確延遲請用高精度計時器 + nanosleep/Select/WaitableTimer。
#include
void delline(void);
// 刪除光標所在行,下方內容上移。僅限文本模式視口。非標準。
#include
void far detectgraph(int far *graphdriver, int far *graphmode);
// 檢測顯卡類型並返回建議驅動/模式。供 initgraph 使用。現代無需手動檢測。
#include
double difftime(time_t time2, time_t time1);
// 返回 time2 - time1 的秒數 (double)。time_t 通常為整數秒,差值精確到秒。
// 高精度差值請用 clock_gettime/timespec 減法。
int main() {
time_t t1 = time(NULL);
sleep(2);
time_t t2 = time(NULL);
printf("差值: %.f 秒\n", difftime(t2, t1)); // 2.0
}
#include
void disable(void);
// 清除 CPU 中斷標誌位 (CLI)。僅限實模式/內核態。用戶態程序不可用。
// 配合 enable() (STI) 成對使用,保護臨界區。
#include
div_t div(int numer, int denom);
// 返回結構 div_t { int quot; int rem; }。
// C99 新增 ldiv(long), lldiv(long long), imaxdiv(intmax_t)。
int main() {
div_t r = div(10, 3);
printf("商=%d 餘=%d\n", r.quot, r.rem); // 3, 1
}
#include
int dosexterr(struct DOSERROR *info);
// 填充結構:exterror(擴展錯誤碼), class(錯誤類), action(建議動作), locus(錯誤位置)。
// 僅限 DOS 環境。現代用 errno / GetLastError / perror。
#include
long dostounix(struct date *dp, struct time *tp);
// 將 DOS 日期時間結構轉換為 Unix time_t (自 1970 秒數)。
// 現代直接用 time()/clock_gettime() 獲取時間戳。
#include
void far drawpoly(int numpoints, int far *polypoints);
// polypoints: x1,y1, x2,y2, ..., xn,yn (numpoints 對座標)。
// 不自動閉合,需手動重複首點。現代用 SDL_RenderDrawLines 等。
#include
int dup(int oldfd);
// 返回最小可用新 fd,指向同一打開文件描述(共享偏移量、狀態標誌)。
// 常用於重定向:dup2(oldfd, STDOUT_FILENO)。
int main() {
int fd = open("log.txt", O_WRONLY|O_CREAT, 0644);
dup2(fd, STDOUT_FILENO); // 標準輸出重定向到文件
close(fd);
printf("寫入文件\n");
}
#include
int dup2(int oldfd, int newfd);
// 若 newfd 已打開先關閉。原子操作,信號安全。
// 經典用法:重定向標準輸入/輸出/錯誤。
// 0=stdin, 1=stdout, 2=stderr
| 舊函數 | 現代替代 | 頭文件/庫 | 備註 |
|---|---|---|---|
| delay | nanosleep / Sleep / usleep | time.h / windows.h / unistd.h | 釋放 CPU,精度更高 |
| delline | ANSI \033[2K / ncurses | stdio.h / ncurses.h | 跨平台終端控制 |
| detectgraph | 無需 (SDL/OpenGL 自動) | - | - |
| difftime | difftime (標準保留) / timespec 減法 | time.h | 高精度用 timespec |
| disable/enable | pthread_mutex / atomic / 關中斷僅內核 | pthread.h / stdatomic.h | 用戶態用鎖/原子操作 |
| div/ldiv/lldiv | 同名 (標準保留) | stdlib.h | C99 新增 lldiv/imaxdiv |
| dosexterr | errno / perror / GetLastError | errno.h / windows.h | 標準錯誤處理 |
| dostounix | time() / clock_gettime() | time.h | 直接獲取 Unix 時間戳 |
| drawpoly | SDL_RenderDrawLines / OpenGL | SDL2 / GL | 現代圖形 API |
| dup/dup2 | dup/dup2 (POSIX 標準保留) | unistd.h | 重定向核心 API |