Parikshita
Parikshita

Reputation: 1327

How to produce beep sound using "\a" escape character?

If I write the following program, then there is no beep sound on running the code.

#include <stdio.h>
int main()
{ 
    printf("\a");
    return 0;
 }

Can you tell me how to use \a for producing beep sound using C program ?

Upvotes: 15

Views: 61155

Answers (6)

qht
qht

Reputation: 21

Please list your operating system, how did you run your code, and if your computer has a beeper.

If you use Windows, maybe this Q&A How to make a Beep sound in C on Windows? would help you.

If you use a GUI desktop Linux distro, terminal emulator like gnome-terminal or xfce4-terminal have a preference option bell to check. Then, make sure your speaker works.

The code below works well for me:

/* name: bell.c
 * run: gcc bell.c && ./a.out
 */

#include <stdio.h>

int main(void) {
    printf("\a");
    return 0;
}

By the way, your code is not the problem.

Upvotes: 0

user332422
user332422

Reputation: 41

i usually use another way to get the beep sound it works 100% on windows 7.

    int x=7; //7 is beep sound other numbers may show emoji and characters
    printf("%c",x);

Upvotes: 2

user2654137
user2654137

Reputation: 17

If you are using Windows, the Windows-versions making beep very different ways. Some of them enable that and working fine, but some windows versions don't. In some windows, it works just that case, if you have an internal motherboard speaker. But some other windows, it works fine without internal motherboard speaker, directly from sound-card (that would be nice!).

If you are lucky, and using the appropriate windows-version, beep/Beep/printf("\a") will work (on internal speaker (if you have), or best case via soundcard). But if you are using an other windows version, it will not work. If in your computer it's okay, your friend's / family member's pc will silent, and he/she will think that you wrote a bad program :-D but not.

My advice, that you should use a library for audio. It's simple, cross-platform, and it will be working always all times all computers etc. For example, Allegro_v4, Allegro_v5, SDL (Simple DirectMedia Layer), or something like that. These librarys works fine with OpenGL / DirectX, and with these librarys, you can load images, play videos, and things like that. Native OpenGL / GLUT / DirectX can't do things like that.

Upvotes: 0

#include<stdio.h>
#include<windows.h>

int main()
{   
    Beep(1000, 1000); /* you can use any number starting from 250, but make sure you use                           it both times


    return 0;

}

Upvotes: -2

Edward Leno
Edward Leno

Reputation: 6327

I agree with @Steve Jessop. People will go to great lengths to keep their computers quiet.

In Windows: As an alternative to "\a", you could use WinAPI's Beep command. If you are using Windows 7, this may not work as expected.

Beep Function (Windows)

Upvotes: 1

pmg
pmg

Reputation: 108978

The only thing wrong (half wrong) with your program is main signature.

To be 100% portable it should be int main(void) or int main(int argc, char **argv) or equivalent: int main() is not equivalent.


And I'd print a '\n' too, or flush the output buffer rather than relying on the runtime flushing all buffers for me automatically, but your program should sound the bell as it is. If it doesn't the problem is elsewhere, not with C.

#include <stdio.h>
int main(void)
{
    printf("\a\n");
    return 0;
}

Upvotes: 2

Related Questions