• ulterno@programming.dev
    link
    fedilink
    English
    arrow-up
    10
    ·
    edit-2
    4 days ago

    Simple. \n when you just want a newline.
    endl when you need to flush at the moment.

    Useful in case you are printing a debug output right before some function that might do bed stuff to buffers.


    Edit: I wrote println instead of endl somehow. Guess I need more downtime

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        4 days ago

        It depends on whether you are printing to a terminal or to a file (and yes the terminal is also a file), and even then you can control the flushing behaviour using something like unbuffer

  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    5 days ago

    printf is superior and more concise, and snprintf is practically the only C string manipulation function that is not painful to use.

    Try to print a 32-bit unsigned int as hexadecimal number of exactly 8 digits, using cout. You can do std::hex and std::setw(8) and std::setfill('0') and don’t forget to use std::dec afterwards, or you can just, you know, printf("%08x") like a sane person.

    Just don’t forget to use -Werror=format but that is the default option on many compilers today.

    C++23 now includes std::print which is exactly like printf but better, so the whole argument is over.