Open In App

Beep() function in C with Examples

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: 
 

Return Type: 
 



Below is the illustration of the BEEP() function:
Program 1:
 




// C program to illustrate BEEP() function
 
#include <stdio.h>
#include <windows.h>
 
// Driver Code
int main()
{
 
    // Function that beeps a sound of
    // frequency 750 for 0.8 sec
    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 program to play song Jingle Bell
// using the BEEP() function
 
#include <stdio.h>
#include <windows.h>
 
// Driver Code
int main()
{
    int x;
 
    // Loop for sound Jingle
    for (x = 0; x < 2; x++) {
        Beep(523, 500);
    }
 
    // sound Bell
    Beep(523, 800);
 
    Sleep(200);
 
    // Loop for sound Jingle
    for (x = 0; x < 2; x++) {
        Beep(523, 500);
    }
 
    // sound Bell
    Beep(523, 800);
 
    // Sound for rest of the tone
    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;
}


Article Tags :