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 -
The sound played in the C# code sample is determined by the Beep system sound setting. For more information, see SystemSounds.
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.
6 Answers
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.
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.
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.
Beep does work again in Windows since windows 7. The format is:
See https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx
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.
This one works to on Windows 7 compiled with Visual Studio 2017. No problems with that.