Thread: Flushing the output buffer from printfs?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Flushing the output buffer from printfs?

    I have this piece of code, used as a debugging tool:

    printf("%d", p->digits[0]);
    printf("%d", p->digits[0]);

    It printed out 21. This is logically impossible, and it's not a junk number. I tried putting just the first line alone, and even the first line alone with different indexes and got correct output.

    I figure the only possible way to get this error, and the trash numbers I got from

    printf("%d", p->digits[0]);
    printf("%d", p->digits[1]);

    Is some sort of buffer flushing. How do I flush buffers in printf?


    As a side note, p points to a structure from main() that I'm manipulating within a function. I don't think that's the problem though.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Call fflush(stdout) is one way.
    Adding \n on the end of some printf's is another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    I tried both of those methods. fflush gave me large arbitray numbers as output, an intervening \n just gave me the 1 on a new line.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Bugs somewhere else in your code then.

    If you've mucked up some array overrun, or memory allocation, then anything could be happening.

    It's unlikely that a standard library function is making a mess of something as simple as %d
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    /change majors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help flushing buffer
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 02-15-2009, 07:30 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. feed printf output into buffer
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 08:00 AM
  4. Flushing the input buffer.
    By civix in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2002, 06:41 PM
  5. Does anyone Know How to..?
    By kwigibo in forum C Programming
    Replies: 12
    Last Post: 09-20-2001, 08:16 AM