| 函數 | 所屬庫 | 標準性 | 現代替代方案 |
delay(ms) | dos.h (Turbo C) | **非標準** | Windows: Sleep(ms) (windows.h) Linux: usleep(us) / nanosleep() (unistd.h) C11: thrd_sleep() (threads.h) |
sound(freq)/nosound() | dos.h | **非標準** | Windows: Beep(freq, dur) (windows.h) Linux: ioctl 控制蜂鳴器 / system("beep") 跨平台: SDL2/SFML 音頻播放 |
gotoxy(x,y) | conio.h | **非標準** (Borland/MS 擴展) | Windows: SetConsoleCursorPosition() (windows.h) ANSI 轉義序列: printf("\033[%d;%dH", y, x); (現代終端通用) 跨平台: ncurses / PDCurses |
cprintf | conio.h | **非標準** | printf + ANSI 顏色碼 \033[31m 等 |
現代標準 C 重寫數字時鐘(跨平台、無依賴)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// 跨平台睡眠毫秒
void msleep(long ms) {
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
// 跨平台清屏
void clear_screen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 獲取當前時間字符串
void get_time_str(char *buf, size_t len) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(buf, len, "%H:%M:%S", t);
}
int main() {
char time_str[32];
printf("數字時鐘 (Ctrl+C 退出)\n");
while (1) {
get_time_str(time_str, sizeof(time_str));
// 使用 ANSI 轉義序列:移動光標到行首( \r )或指定位置,清除行
printf("\r\033[K%s", time_str); // \r 回行首, \033[K 清除到行尾
fflush(stdout); // 關鍵:刷新緩衝區立即顯示
msleep(1000); // 睡眠 1 秒
}
return 0;
}
關鍵知識點
- 拒絕 Turbo C / VC++ 6.0:這些 20+ 年前的產物不支持現代 C 標準 (C99/C11/C17/C23),教學有害。**必須用 GCC/Clang/MSVC (VS 2019+) + VS Code/CLion**。
- 標準庫優先:
<stdio.h>、<time.h>、<stdlib.h>、<string.h>、<math.h> 等標準頭文件功能強大、跨平台。
- 平台相關功能隔離:用
#ifdef _WIN32 封裝 Sleep/SetConsoleCursorPosition,Linux 用 usleep/ANSI 轉義序列。
- ANSI 轉義序列:現代終端 (Windows 10+ CMD/PowerShell/WT, Linux/macOS 終端) 均支持
\033[ 碼控制光標、顏色、清屏,**無需 conio.h/ncurses 即可實現豐富 TUI**。
- 控制台緩衝區:
printf 默認行緩衝,**動畫/時鐘必須 fflush(stdout)** 強制刷新。
- 信號處理優雅退出:可添加
signal(SIGINT, handler) 捕獲 Ctrl+C 退出循環。
進階:用 ncurses 實現專業 TUI 時鐘
// 需安裝 libncurses-dev / pdcurses
#include <ncurses.h>
#include <time.h>
#include <unistd.h>
int main() {
initscr(); // 初始化
noecho(); curs_set(0); // 關閉回顯、隱藏光標
nodelay(stdscr, TRUE); // 非阻塞輸入
while (getch() != 'q') { // 按 q 退出
time_t now = time(NULL);
struct tm *t = localtime(&now);
char buf[32];
strftime(buf, sizeof(buf), "%H:%M:%S", t);
clear();
mvprintw(LINES/2, (COLS-8)/2, "%s", buf); // 正中間顯示
refresh();
sleep(1);
}
endwin(); // 結束
return 0;
}
總結
原代碼錯誤核心:**使用了 30 年前 Turbo C 專有非標準函數 (delay/sound/nosound/gotoxy/cprintf),在現代編譯器下無法編譯/鏈接/運行**。
**解決之道**:擁抱現代 C 標準 (C11/C17),使用標準庫 + 平台封裝層 + ANSI 轉義序列 / ncurses 實現跨平台控制台交互。時鐘核心邏輯極簡:循環 { 取時間 → 格式化 → \r\033[K 輸出 → fflush → sleep(1) }。拒絕考古級教材與工具,從第一行代碼開始寫現代、可移植、標準合規的 C 程序。
- 上一篇文章: C語言排序算法對決:冒泡排序 vs 選擇排序,誰效率更高?
- 下一篇文章: 什麼是腳本?什麼是 C 語言?兩者本質區別與聯繫
Copyright ©
程式碼之家 All Rights Reserved