Related Articles

Related Articles

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Note: The program won’t produce the sound in online IDE. Please try to run it in offline compilers.
Program 2:
 

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :