Box Box Box Box
Box Box Box Box

Reputation: 5370

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.

Upvotes: 9

Views: 39909

Answers (6)

Mats Stenfeldt
Mats Stenfeldt

Reputation: 1

This one works to on Windows 7 compiled with Visual Studio 2017. No problems with that.

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

Upvotes: 0

Lundin
Lundin

Reputation: 215305

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.

Upvotes: 0

user4936706
user4936706

Reputation:

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

Upvotes: -1

user8137222
user8137222

Reputation: 1

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

Upvotes: 0

Damian Yerrick
Damian Yerrick

Reputation: 4664

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.

Upvotes: 10

mcleod_ideafix
mcleod_ideafix

Reputation: 11448

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.

Upvotes: 1

Related Questions