How to Create an Alert in C

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! If you want your...

Part 1 of 3:

Character alert

  1. Picture 1 of How to Create an Alert in C
    If you want your alert to be portable and work on every computer, you can use the escape code "a".
    1. a is defined as an audible alert, usually a beep. [1] However, on some Unix operating systems it might produce a screen flash instead of a sound.
  2. Picture 2 of How to Create an Alert in C
    Use this example code.
    printf("a"); 
Part 2 of 3:

Beep()

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

Sample Code

  1. Picture 6 of How to Create an Alert in C
    Try a program that uses a to make a beep when a key is pressed, uses ESC to exit:
    #include #include int main() { while(getch() != 27) // Loop until ESC is pressed (27 = ESC) printf("a"); // Beep. return 0; } 
  2. Picture 7 of How to Create an Alert in C
    Try a program that makes a beep of a given frequency and duration:
    #include #include 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; } 
Update 05 March 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile