You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中delay()函数在64位编译器下是否可用?如何实现延时?

Fixing Delay Issues in 64-bit C Compilers

Ah, I’ve run into this exact problem before! The delay() function you’re relying on is a non-standard, compiler-specific helper—mostly tied to older 32-bit MinGW builds that included legacy DOS/Windows support. When you switch to 64-bit compilers like TDM-GCC-64 or modern MinGW-w64, these old non-standard functions get removed, which is why you’re seeing those "implicit declaration" and "undefined reference" errors.

Here are three reliable alternatives to get your program working again, sorted by portability and ease of use:

Use the <time.h> header to implement a delay using standard clock functions. This works on any C compiler (32/64-bit, Windows/Linux/macOS) and doesn’t rely on system-specific APIs.

#include <time.h>

// Custom delay function that takes milliseconds
void delay_ms(unsigned int ms) {
    clock_t start = clock();
    // Wait until the elapsed time exceeds our target milliseconds
    while ((clock() - start) * 1000 / CLOCKS_PER_SEC < ms) {
        // Empty loop to burn time
    }
}

// Usage example
int main() {
    printf("Pausing for 1.5 seconds...\n");
    delay_ms(1500);
    printf("Resume!\n");
    return 0;
}

Note: This uses a "busy wait" (it keeps the CPU occupied during the delay). For most small delays, this is fine, but if you need a more efficient solution (especially for longer waits), check the next option.

2. Windows-Specific Efficient Delay (Best for Windows Apps)

On Windows, use the official Sleep() API from <windows.h>. This tells the OS to pause your program without hogging CPU resources, which is more efficient than a busy wait. The parameter is in milliseconds, and note the capital S (case matters!).

#include <windows.h>
#include <stdio.h>

int main() {
    printf("Waiting 2 seconds...\n");
    Sleep(2000); // Pause for 2000 milliseconds = 2 seconds
    printf("Done waiting!\n");
    return 0;
}

3. Cross-Platform Conditional Compilation (For Multi-OS Projects)

If you need your code to work on both Windows and Linux/macOS, use conditional compilation to pick the right delay method automatically:

#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#define DELAY_MS(ms) Sleep(ms)
#else
#include <unistd.h>
#define DELAY_MS(ms) usleep((ms) * 1000) // usleep uses microseconds, so convert ms to μs
#endif

int main() {
    printf("Pausing for 3 seconds...\n");
    DELAY_MS(3000);
    printf("All done!\n");
    return 0;
}

Key takeaway: Ditch delay() entirely—it’s not part of the C standard and will break across different compilers. Stick to one of the above methods for reliable, portable delays.

内容的提问来源于stack exchange,提问作者Rhythm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:32:43