14

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 ?

msc
  • 33,420
  • 29
  • 119
  • 214
Parikshita
  • 1,297
  • 5
  • 15
  • 23
  • 4
    That's up to the terminal, which depends how you're running the program. If stdout is redirected to a file, no beep until you display the file. Even so, not all terminals actually beep when they receive an ascii BEL character. I for one disable it if I can. For that matter on machines used at work, I generally unplug the cable to the PC internal speaker, and mute the rest of the sound except when I have headphones plugged in. Good luck getting a beep out of that... – Steve Jessop Oct 02 '10 at 12:09
  • 1
    The most tragic beeps are those from laptops, that are emitted ad incredible volume through the "normal" speakers. – Matteo Italia Oct 02 '10 at 12:10
  • 3
    btw, you *must* run the program from a terminal, since beeping when printing '\a' is a terminal feature (not C nor OS feature). – Lie Ryan Oct 02 '10 at 12:51
  • @Lie Ryan I've also tried it in terminal but no beep sound.I typed the following command printf '\a' on the terminal but nothing happened... – Parikshita Oct 02 '10 at 14:41
  • 1
    @Parixit: what's your OS, and what Terminal program do you use? – Lie Ryan Oct 02 '10 at 15:54
  • @Lie Ryan I'm using Ubuntu 10.04 – Parikshita Oct 03 '10 at 10:10
  • to avoid the flushing issues of printf() alter your program try these replacements: #include write(1,"\a",1); which also must be run in a terminal. – Gilbert Jun 13 '16 at 01:13

6 Answers6

2

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);
user332422
  • 41
  • 3
  • OP was asking about Linux, not Windows. – absoluteAquarian Oct 02 '18 at 16:37
  • Char values of 7 works on Windows 10 in Scala too as a beeper (not on topic per se but good to note that this fun feature still has W10 support). @absoluteAquarian complained about OP asking about Linux, but I do not see where specified that, and therefore this post can be a discussion in general for "printing" beep sounds to the console in general. – Gregory Fenn Apr 25 '21 at 15:29
  • @GregoryFenn see the comments in the OP's question. "I'm using Ubuntu 10.04" – absoluteAquarian Apr 25 '21 at 19:42
2

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;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • You could at least admit that C99 exists, even if you think the questioner isn't using it ;-) – Steve Jessop Oct 02 '10 at 12:42
  • Even in `C99`, `int main()` is not equivalent to one of the 2 published definitions. – pmg Oct 02 '10 at 13:34
  • It is in a definition. It isn't in other declarations. 6.7.5.3/10 and /14. They both mean "no parameters", and 5.1.2.2.1/1 specifies that `main` with no parameters is OK. It only gives one of the two ways of doing that as example code, but I don't *think* it means to imply that the other way of doing it is unacceptable. – Steve Jessop Oct 02 '10 at 14:40
  • I read the Standard the same way you do, but the text in my C89 reference is the same. I have a "gut feeling" we're missing something somewhere ... but I'm not feeling like interpreting Standardese right now. Anyway, better use one of the 2 documented definitions no matter what :) – pmg Oct 02 '10 at 15:14
  • 2
    Given `int main(void) { /* ... */ }` a recursive call `main(42)` is a constraint violation. Given `int main() { /* ... */ }`, it's not, though it does have undefined behavior. I'd say that makes them not equivalent. On the other hand, if `int main() { /* ... */ }` is invalid, then it's not possible to write a parameterless main program that's valid in both pre-ANSI and ANSI C, which I'm sure was not the intent. In practice, I've never heard of a C compiler that rejects or otherwise mishandles `int main()` -- but I still prefer `int main(void)`. – Keith Thompson May 03 '13 at 17:20
  • Or even `char beep = '\007';` – Yousha Aleayoub Mar 19 '18 at 10:17
  • `int main() {…}` is a proper definition and does not result in undefined behavior. The C standard says `main` shall be defined as `int main(void)`, `int main(int argc, char *argv[])` **or equivalent** or some other implementation-defined manner. In a non-definition declaration, `int main();` would leave the parameters unspecified. However, in a definition, `int main() {…}`, the *declaration-list* of the *function-definition* grammar is empty, indicating the function takes no parameters, making this equivalent to `int main(void)`. – Eric Postpischil Aug 04 '21 at 15:08
1

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)

Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
Edward Leno
  • 6,257
  • 3
  • 33
  • 49
0

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.

0

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.

qht
  • 21
  • 3
-2
#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;

}
Kevin
  • 53,822
  • 15
  • 101
  • 132