Thread: escape sequences

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    escape sequences

    Hi all.

    I am trying to play a sound 5 times. If I remove the \n in printf(), the sound plays only once. What is the relationship between the \n and the \a that would cause this behaviour? The printf()'s are seperate statements, so I don't see the correlation.

    Thanks.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count;
    
        for(count = 0; count < 5; count++)
        {
            int count;
    
            for(count = 0; count < 95000000; count++)
            {
                ;
            }
    
            printf("hello\n");
            printf("\a");
        }
    
        return 0;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    That's surprising to me! What compiler are you using?

    The only thing I can think of is output buffering. If all the output is buffered, then output in a big chunk at the end then it would probably just sound like a single beep.

    This page for GCC Flushing Buffers - The GNU C Library says that newlines flush the buffer, so it could be that but I'm really not very convinced. You could try to confirm this by taking the \n out and calling fflush(stdout) after the print of \a.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's not surprising at all.

    stdout is buffered. A '\n' character causes stdout's buffer to be flushed.

    Using fflush(stdout) would also flush stdout, without having to output a '\n'. You would also find that fprintf(stderr, ....) will do what you want, without any flushing, since stderr is not buffered.

    Also, be careful of your inner loop. There is nothing stopping a compiler from eliding it (i.e. removing it completely) since a compiler can detect it has no effect on observable output (except timings). Depending on operating system, you would be better off using a function such as sleep() [Most unix variants] or Sleep() [Windows].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Escape sequences in c
    By linuxlover in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 02:39 PM
  2. Problems with escape sequences
    By stdq in forum C Programming
    Replies: 1
    Last Post: 10-10-2010, 07:13 PM
  3. C Escape sequences
    By Tool in forum C Programming
    Replies: 10
    Last Post: 11-20-2009, 09:38 PM
  4. Escape sequences in VC++
    By emilyh in forum Windows Programming
    Replies: 7
    Last Post: 09-26-2001, 07:02 AM
  5. Escape Sequences
    By Me-Again-Again in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 06:24 AM