printf in C++

This is a discussion on printf in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I am changing to work in C++ from C. In my C++ code, I have a line like this ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    printf in C++

    Hi,
    I am changing to work in C++ from C. In my C++ code, I have a line like this
    Code:
      printf("%d %d %d %d \n", x0,y0,x1,y1);
    There is nothing printed out on the console. If I call cout next, then I can get the output of both
    Code:
      printf("%d %d %d %d\n", x0,y0,x1,y1);
      cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<endl;
    It is like cout push printf to do what it is supposed to. Any idea how to correctly use printf in C++? Thanks!

    Update:
    There was mistake in my first post.
    with "\n" printf works, while without "\n" it doesn't.
    Just out of curiosity, why the latter happens?
    Last edited by lehe; 02-05-2009 at 10:32 AM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    printf should flush the buffer when you print '\n', but i'm not sure if that's still a rule in c++ like it is in c. you can make sure it gets flushed by adding fflush after printf.
    Code:
    printf("%d %d %d %d \n", x0,y0,x1,y1);
    fflush(stdout);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    According to Josuttis (The C++ Standard Library: A Tutorial and Reference):
    By default, these standard streams are synchronized with the standard streams of C. That is, the C++ standard library ensures that the order of mixed output with C++ streams and C streams is preserved. Before any buffer of standard C++ streams writes data it flushes the buffer of the corresponding C streams and vice versa. Of course, this synchronization takes some time. If it isn't necessary you can turn it off by calling sync_with_stdio(false) before any input or output is done.
    I do not think sync_with_stdio() is the solution here, but it explains why the use of std::cout causes stdout to be flushed. Perhaps you can try fflush(), e.g.,
    Code:
    printf("%d %d %d %d \n", x0, y0, x1, y1);
    fflush(stdout);
    but I had the impression that writing a new line should trigger the flush anyway since the stream is line buffered.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Does the program continue to run or terminate normally after the print statement. In any case you should see the output at latest when the program stops for next input or when it finishes. What should happen so that you are able to tell that it just doesn't produce output?

    Also, endl definitely flushes the output of cout.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Are you getting just one line print out, because if you are it would indicate it's nothing to do with the buffer. Though this may not be relevant, do you have the correct #includes? because I think (though I can't remember at the mo) you need the #include <cstdio> directive.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    There was mistake in my first post.
    with "\n" printf works, while without "\n" it doesn't.
    Just out of curiosity, why the latter happens?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by lehe
    with "\n" printf works, while without "\n" it doesn't.
    Just out of curiosity, why the latter happens?
    As I noted, stdout is typically line buffered, so it flushes when a new line is printed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    printf is supposed to flush the buffer when it sees '\n'. if it doesn't see '\n', it doesn't have to flush the buffer unless it gets full. and you don't see output until the buffer is flushed.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    So why do you need to use printf in C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Elysia View Post
    So why do you need to use printf in C++?
    complex formatting can be easier and shorter with printf.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Meldreth
    complex formatting can be easier and shorter with printf.
    That's true, and for the best of both worlds (and something non-standard, heh) there is the Boost format library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Non-standard perhaps, but it's darn portable, so usually I say boost is worth it
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21