1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Help in debugging this simple code snippet.

Discussion in 'C' started by rj81inmit, Aug 10, 2008.

  1. rj81inmit

    rj81inmit New Member

    Joined:
    Aug 10, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Why does it always throw stderr ? Kindly help...

    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
    while(1)
    {
    fprintf(stdout,"hello-out");
    fprintf(stderr,"hello-err");
    sleep(1);
    }
    return 0;
    }


    thanks, robin.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What do you mean by "throw stderr"? What is the output from your program, and what are you expecting it to do?

    I changed it slightly (while(1) -> for (int i=0; i<5; i++)), built and ran it in Visual Studio 2005 (without the sleep() call either cos I couldn't find where this was displayed) and the output was:
    hello-out
    hello-err
    hello-out
    hello-err
    hello-out
    hello-err
    hello-out
    hello-err
    hello-out
    hello-err

    - as you see, displaying both hello-out and hello-err.

    If you change it from while(1) to a short loop, what is the output then?
     
  3. rj81inmit

    rj81inmit New Member

    Joined:
    Aug 10, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thats fine if you test with a finite for loop. but why is it first printing set of hello-err ? should it not print hello-out and then hello-err?
    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
    for(int i=0;i<5;i++)
    {
    fprintf(stdout,"hello-out");
    fprintf(stderr,"hello-err");
    // sleep(1);
    }
    return 0;
    }

    output: hello-errhello-errhello-errhello-errhello-errhello-outhello-outhello-outhello-outhello-out
    expected (??) : hello-out hello-err hello-out hello-err hello-out hello-err hello-out hello-err hello-out hello-err
    Just wanted to understand how the streams are interpreted by the compilers.
     
  4. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    This is Not Correct Section for That Go to C/C++ Section for making this.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Moved to C-C++ forum.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    It probably depends on how streams work on your platform. What are you using? (If you're not sure show me the output of uname -a)
    Try adding fflush(stream-name); after each printf and see what happens, i.e.:
    fprintf(stdout,"hello-out\n"); fflush(stdout);
    fprintf(stderr,"hello-err\n"); fflush(stderr);
    Output is often buffered rather than printed immediately so this could be the cause of the behaviour.
     

Share This Page