Thread: Printf buffer

  1. #1
    Registered User
    Join Date
    Sep 2019

    Printf buffer

    Dear all, look at this:
    printf("%d %d");
    It prints 333
    The first 3 is printed as expected, the second goes to the buffer and is printed instead of the second "%d"
    But the third "%d" not referred to any number or buffered data , it prints the third 3, where is this coming from?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You're looking at the effects of undefined behaviour. The format specifiers must correspond with the arguments, for both printf calls.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    As @laserlight says: undefined behavior.

    Tip: If you are using glibc and want to use only one argument, you can do, for example:
    printf( "%1$d -> 0x%1$02x", 3 );
    Will print:
    3 -> 0x03

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy the content of buffer to temp buffer
    By Megha Bhirade in forum C Programming
    Replies: 1
    Last Post: 11-12-2018, 07:19 AM
  2. Class for both dynamic allocated buffer and static buffer
    By TotalTurd in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2012, 09:09 PM
  3. feed printf output into buffer
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 08:00 AM
  4. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 10:58 PM
  5. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 10:41 AM

Tags for this Thread