Sometimes, when writing a program, you need to create a way to get the attention of the user to bring their focus back to the program. Alerts are a very useful way to do that. If you want to make alerts in C, read on!

Part 2
Part 2 of 3:
Beep()

  1. 1
    On Windows operating systems, you can use the Beep(int frequency, int ms). It makes a beep of a specified duration and frequency.[2]
    • On the Windows7 operating system, this function sends the beep to the sound card. This works only if the computer has speakers or headphones.
    • On previous Windows versions, it sends the beep to the motherboard. This works on most computers and no external devices are required.
  2. 2
    Include the windows library. Add the following code at the beginning of your program:
      #include <windows.h>
      
  3. 3
    When you need a beep, use the following code:
      Beep(500, 500);
      
  4. 4
    Change the first number with the frequency of the beep you want. 500 is close to the beep you get with \a.
  5. 5
    Change the second number with the duration of the beep in milliseconds. 500 is a half of a second.

Part 3
Part 3 of 3:
Sample Code

  1. 1
    Try a program that uses \a to make a beep when a key is pressed, uses ESC to exit:
      #include <stdio.h>
      #include <conio.h>
      
      int main()
      {
        while(getch() != 27) // Loop until ESC is pressed (27 = ESC)
          printf("\a");  // Beep.
        return 0;
      }
      
  2. 2
    Try a program that makes a beep of a given frequency and duration:
      #include <stdio.h>
      #include <windows.h>
      
      int main()
      {
        int freq, dur; // Declare the variables
        printf("Enter the frequency (HZ) and duration (ms): ");
        scanf("%i %i", &freq, &dur); 
        Beep(freq, dur);  // Beep.
        return 0;
      }
      

Community Q&A

  • Question
    My system appears to be missing windows.h. How do I solve this?
    Living Concrete
    Living Concrete
    Top Answerer
    Ensure that you are on the Windows operating system and that you have the Windows SDK installed. Also, make sure that you are using a C Compiler that is capable of using the Windows API.

Warnings


About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 10 people, some anonymous, worked to edit and improve it over time. This article has been viewed 33,900 times.
How helpful is this?
Co-authors: 10
Updated: May 7, 2020
Views: 33,900