简体   繁体   中英

How to make a Beep sound in C on Windows?

I am trying to make a program which includes a beep noise. I work on a 32 bit Windows Vista. I am using the Code::Blocks IDE which runs on a GNU compiler. My sample code is -

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

int main(void)
{
    Beep(750, 300);
    printf("\n \n \t This is a dummy program for Beep.");
    getch();

    return 0;
}

On the Internet I read that we could also use \\a in printf to make a beep. I tried that but it is not working. I checked my speakers and sound card. Everything is perfect but I hear no beep. Even the method I displayed in my sample code does not work.

The C standard recommends that writing '\\a' to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep() and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeep function, which plays a sound "asynchronously" (in the background while your program continues to run). The user can configure which sound is associated with each of these four values in the Sound control panel.

#include <windows.h>

/* Include one of these in a function */
MessageBeep(MB_OK);              /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION);    /* play question sound */
MessageBeep(MB_ICONWARNING);     /* play warning sound */

MessageBeep() is defined in User32.dll , so if this gives you link errors, make sure you're linking to the corresponding import library. In MinGW GCC (the compiler in Code::Blocks), add -lUser32 to the list of libraries passed to the linker.

MessageBeep(-1);

From the MSDN documentation:

MessageBeep function

Plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry.

BOOL WINAPI MessageBeep( _In_ UINT uType ); ... ...

Value for uType : 0xFFFFFFFF

Meaning: A simple beep. If the sound card is not available, the sound is generated using the speaker.


Also, and to my surprise, I've tested that. at least Windows 7 32 bits (and Windows Vista surely too) do some sort of emulation for the old 8253 I/O ports and the keyboard port, available to ring 3 processes, so the old implementation of sound() and nosound() should work. Unfornately, I haven't got any 32 bit machine available ATM so I cannot confirm this.

For the Beep() function in windows.h to actually work, you have to have a "PC speaker" buzzer in your PC, as stated in the function's documentation. So you need to have a fairly old PC and with Windows XP or older, since support for the function was apparently dropped in Windows Vista.

On newer Windows versions, calling Beep() gives a beep in the speakers instead, using your sound card. If you aren't getting any beep, it is possibly not related to the program, but perhaps to your specific computer hardware.

Beep does work again in Windows since windows 7. The format is:

Beep(frequency, duration) where frequency is the pitch in hertz, and duration is the length in milliseconds

See https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx

这个适用于使用Visual Studio 2017编译的Windows 7。没有问题。

printf("\n Bad request - check status code parameter\a");

You can use \\a . At least it works in my computer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM