0

Its a simple think i want to know i have tried searching for it but i haven't found anything on it i already tried using sleep(5) so i can separate two sounds it still worked only once

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

i am using ubuntu i found one old question but it didn't mention if \a is written twice why it ring only once ,does the bell is executed only at the end of the program any source,document that have info please feel free to share it! and if bells escape functions works only during termination, why? if there is way the bell can ring (sound) twice how?

MONUDDIN TAMBOLI
  • 152
  • 1
  • 11
  • 3
    `printf("\a"); fflush(stdout); sleep(5); ...` – pmg Mar 20 '21 at 18:42
  • 2
    It is so rarely used feature. I am surprised that it bells at all :) – 0___________ Mar 20 '21 at 18:43
  • @pmg thanks it worked is there any document on it or some – MONUDDIN TAMBOLI Mar 20 '21 at 18:45
  • 1
    `stdout` is, by default, line-buffered... so it keeps adding the `"\a"` to the buffer and only effectively prints them once the program ends (after `sleep()` and the two at the same time). `fflush(stdout)` forces a output (and clearing) of the current buffer. – pmg Mar 20 '21 at 18:47
  • I know that question isn't about bells specifically, but it explains the underlying principle: data that you `printf` isn't necessarily sent to the OS/hardware right away, only after the buffer is flushed. – Nate Eldredge Mar 20 '21 at 18:49
  • 1
    @MONUTAMBOLI Two identical musical notes that are close enough will sound like one slightly longer note. This is about the physics of sound, not about programming. How exactly that works on your platform is a matter of implementation. – dxiv Mar 20 '21 at 18:49
  • @NateEldredge my doubt is cleared now should i delete the question ? or just leave it – MONUDDIN TAMBOLI Mar 20 '21 at 18:53
  • Might as well leave it. Now that it's linked to the other question, it does no harm, and could help someone with a similar problem in the future. Stack Exchange can afford the disk space :) – Nate Eldredge Mar 20 '21 at 19:12
  • @dxiv: Yeah, I think the programming part is why there wasn't the intended 5-second delay between them (which you certainly would have heard). And the answer to that is the buffering, of course. – Nate Eldredge Mar 20 '21 at 19:13

0 Answers0