Thread: strange program behaviour...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    strange program behaviour...

    Code:
    #include <stdio.h>
    #define WAIT system("sleep 2")
    
    main()
    {
     printf("Helllo\n");
     WAIT;
    }
    The desired program output is as follows:
    1)Program prints the "Hello" msg
    2)It halts for 2 seconds
    3)It finally exits

    But if we write something like:
    Code:
     printf("Hello");
     WAIT;
    (\n character ommited)

    then the output changes:the program first halts and
    then prints the msg.

    Is there anybody out there that happens to know why this happens?

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Does that still happen if you #include <stdlib.h>
    R.I.P C89

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    The program continues to behave the same.
    oh by the way i am using the gcc compiler in a Unix system.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Okay, what about...

    Code:
    ...
        printf("string");
    
        fflush(stdout);
    
        WAIT;
    ...
    R.I.P C89

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The reason it pauses and then displays the text when you remove the newline is because the system hasn't flushed the output buffer.

    The system always tries to be as effiecent as possible. As such it doesn't write characters one at a time to the screen. It waits till it has a bunch of stuff to write.

    Normally if you do something like printf("hello"); and then wait for user input it will display find but by putting the sleep in the mix you are causing the system to be busy and as such it won't display the text.

    Putting a newline tells the system to flush the output buffer. You can also use the fflush(stdout); like c99 suggested.

    Hope this helps.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Thantos
    Putting a newline tells the system to flush the output buffer.
    That's quite interesting to know. Thanks for the extra info.
    R.I.P C89

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Something I found interesting for this code:

    When using a function like system(), YMMV (Your Mileage May Vary). For Windows systems, Borland bcc exhibits the same behavior with or without the \n. Note that Windows version of gcc (installed with Cygwin) acts like the UNIX gcc: behavior with \n is not same as without \n.

    (Note: with Borland, you have to #include <stdlib.h> to use the system() function.)

    Why do I find this interesting?
    Many times people use tried-and-true pieces of code when changing to a new programming environment and find that things don't work the way they used to.
    Last edited by Dave Evans; 03-06-2004 at 10:11 AM.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    system() calls aren't portable, either. An example using clock() (in time.h):

    Code:
     void wait(unsigned timeout)
    {
     timeout += clock();
     while(clock() < timeout)
      continue;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Note: with Borland, you have to #include <stdlib.h> to use the system() function.
    That's because the Standard says system() lives in stdlib.h, so you should always #include that library.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    On last thing on strange behaviour...

    Gee,guys,thanks for all the help!!

    I mean,i can't believe that so many people would like to comment my piece of code,and help me understand why this output occurs.Really appreciated,fellows!

    On last thing though:does anyone know where i can find more info about the fflush function and the way the output buffer works?THe books i have about C don't say much about this.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    try man fflush

    and

    man 3 fflush

    Not a lot of info, but might get you started.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Strange Direct Input Behaviour
    By Diamonds in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2003, 04:34 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM