How to Use sound() and nosound() in C Programming Language

sound() and nosound() functions in C Programming Language

Introduction :

During creating software project, sometime you need to include audio effect within your project for various purposes. So, sound generation on the pc speaker is an important task in any programming language. In C programming language, you can produce and stop simple sound effects on the pc speaker using sound() and nosound() functions.

In this article, I shall show you how to use sound() and nosound() functions of dos.h library in the Turbo C++ IDE using C programming language. Here, I will also show you the syntax, usage and program of sound() and nosound() functions.

What is sound() and nosound() functions :

Generally, sound() and nosound() functions are used to produce and stop sound in C programming language. They are not part of the standard C library. They are typically used in the Turbo C++ IDE. To use sound() and nosound() functions, you have to include dos.h library or header file in your program. Because they are declared in the dos.h library.

About sound() function :

The sound() function is used to generate a tone at a specified frequency (Hertz) on the pc speaker. Using the sound() function, you can produce audible tones for various purposes such as alerts, notifications, or simple sound effects.

Syntax of the sound() function :

The following code is the syntax of sound() function in C programming language.

The sound() function takes an integer argument or parameter (frequency) which represents the frequency of the sound. The parameter indicates the frequency of the sound in Hertz (Hz). The sound() function returns nothing.

About nosound() function :

The nosound() function is always called after the sound() function. It stops the sound that was produced by the sound() function. You can call nosound() function when you want to stop the sound generated by sound() function.

Syntax of the nosound() function :

The following code is the syntax of nosound() function in C programming language.

The nosound() function does not take any argument or parameter and it also returns nothing.

About the program :

This program is a basic example of the usage of sound() and nosound() functions for audio output in C programming language. It continuously generates sound (tone) at random frequencies on your pc speaker. When you run the program, You can hear different sound (tone) on your pc. At the same time, you can also see the frequency of sound on the console screen. If you want to stop the sound as well as the program, you have to press any key.

Explanation of the program :

At first, you have to include necessary library or header files such as dos.h, conio.h, stdlib.h and stdio.h on your program. In the main() function of the program, you candDeclare an integer variable “i” to store the frequency of the sound (tone). Inside the main() function, there is an infinite while loop that continues until a keyboard key is pressed. Here, the kbhit() function is used to check for keyboard input by the user.

Within the while loop, you can use clrscr() function which clears the screen to display the next tone frequency cleanerly.After that, print the current frequency of the tone on the screen using printf() function. Then, using the sound() function generates a sound (tone) according the value of “i” variable. To hear the sound properly you have to introduce a delay using delay() function. Now, to update the value of “i” variable, you can use rand() function.

Once user presses any key, the while loop terminates and stop the sound using nosound() function.

How run the program :

To run the program, first you have to install the Turbo C++ IDE on your pc. Next, open it and create a C file with .c extension in Turbo C++ IDE. Now, copy the below code of the program and paste into your C file in the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, follow the link.

Source code :

The following code is used for sound() and nosound() functions by C programming language.

Output :

If you run the program on your pc, you can see the output of the program of sound() and nosound() functions in the C programming language.

output of sound() and nosound() functions in C Programming Language

Conclusion :

By following the above article, you have learned how to use sound() and nosound() functions in the C programming language. Now, you can use sound() and nosound() functions in your program to create your own project. Thank you for visiting my site.

Scroll to Top