2

This is my code in C.

#include <stdio.h>

int main(){
   printf("\a\n");
   printf("Startled by the sudden sound, Sally shouted, \"By the Great Pumpkin, what was that!?\"");
   return 0;
}

Supposedly, "\a\n" is supposed to sound an alert right? For some reason, however, it's not doing so. I'm on Ubuntu Zesty, the bash command beep does sound a beep, but echo -e '\a' doesn't, so I'm not sure what the problem is exactly.

Made this question because other such questions didn't end up producing answers that were detailed/understandable enough to me, or had a different problem entirely whose solution didn't apply to me.

The post linked above, frankly, had an answer that was too vague. "The problem isn't with C, it's with something else." doesn't help at all.

UPDATE: Just incase someone else stumbles onto this, here was the problem: I have GNOME, and the WM is Gnome-Shell as a result. Since that was the case, I had to open the sound settings, travel over to alerts, enable them, and make the volume higher. It never came to my attention that the shell itself could have been the problem. Realised this after running metacity --replace and suddenly being able to hear alerts.

ENBYSS
  • 819
  • 1
  • 10
  • 22
  • probably a shell option. – Stargateur Oct 13 '17 at 11:08
  • 3
    Interpreting the `BEL` character to sound an alert is the job of the terminal emulator. Look at its settings and see if audible bell is disabled. The terminal sounds the alert using the desktop, so you should also look at the sound settings of your desktop to see if sounds are turned on. – user4815162342 Oct 13 '17 at 11:11
  • @user4815162342 If the `beep` command presumably running in the same terminal succeeds, that points at the terminal itself and not the desktop. – unwind Oct 13 '17 at 11:36
  • @unwind Looking at [the manual](https://linux.die.net/man/1/beep), `beep` appears to be a command that directly accesses the PC speaker. I would expect the terminal to go through a desktop API in order to be a polite desktop citizen and respect the user's desktop preferences. – user4815162342 Oct 13 '17 at 12:58
  • @user4815162342 Ah, okay if that's true that makes sense of course. Thanks. – unwind Oct 13 '17 at 13:20
  • @user4815162342 I checked the settings and the option is on. I'm starting to guess that the option is just disabled in the distro itself, and that some digging needs to be made to enable it again. Beep works by the way, but I could guess why. – ENBYSS Oct 13 '17 at 19:39

1 Answers1

5

You have indeed written this correctly: and the appended \n is the idiomatic way of not delaying the beep output.

But alas modern operating systems allow users to configure their environment so such beeps are suppressible. That's probably what's happening here: looking a little deeper into your question it seems that your shell has disabled beeping on standard output.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    *the appended `\n` is the idiomatic way of not delaying the beep output.* That's idiomatic only for C implementations that default to line buffering `stdout`. `FILE` buffering is implementation-defined. – Andrew Henle Oct 13 '17 at 11:14
  • 2
    ...and `fflush(stdout)` is better IMO, because the appended `\n` has a side effect that is not necessarily desired. – Jabberwocky Oct 13 '17 at 11:28
  • That makes sense. I DID try to find a way to not suppress such an alert, but I think that it's going to be a complicated road to travel down. Thanks! – ENBYSS Oct 13 '17 at 19:37