Beep() function in C with Examples
Last Updated :
22 Nov, 2020
The Beep function in C is used to make a Beep sound. It generates a tone on the speaker. The function is synchronous, i.e. it waits and doesn’t return to its caller function until the sound is finished. It can be very useful during the Debugging process for finding errors.
Header File:
#include <windows.h>
Syntax:
BEEP(x, y)
Parameters: This method accepts two parameters:
- x: which is the frequency of sound
- y: which is the duration in ms till sound is ON.
Return Type:
- If the function produces the sound then it return any nonzero value.
- If the function doesn’t produces the sound then it returns zero.
Below is the illustration of the BEEP() function:
Program 1:
C
#include <stdio.h>
#include <windows.h>
int main()
{
BEEP(750, 800);
getch();
return 0;
}
|
Note: The program won’t produce the sound in online IDE. Please try to run it in offline compilers.
Program 2:
C
#include <stdio.h>
#include <windows.h>
int main()
{
int x;
for (x = 0; x < 2; x++) {
Beep(523, 500);
}
Beep(523, 800);
Sleep(200);
for (x = 0; x < 2; x++) {
Beep(523, 500);
}
Beep(523, 800);
Sleep(200);
Beep(523, 500);
Sleep(50);
Beep(659, 400);
Sleep(50);
Beep(440, 400);
Sleep(50);
Beep(494, 400);
Sleep(50);
Beep(523, 750);
Sleep(400);
Beep(600, 400);
Sleep(100);
Beep(600, 350);
Sleep(200);
Beep(600, 300);
Sleep(150);
Beep(600, 250);
Sleep(150);
Beep(600, 150);
Sleep(150);
Beep(550, 250);
Sleep(150);
Beep(555, 350);
Sleep(50);
Beep(555, 200);
Sleep(150);
Beep(555, 200);
Sleep(150);
Beep(690, 200);
Sleep(150);
Beep(690, 200);
Sleep(150);
Beep(610, 200);
Sleep(150);
Beep(535, 160);
Sleep(100);
Beep(500, 150);
Beep(500, 50);
Sleep(200);
Beep(700, 200);
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...