• Gustephan@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        2 dagen geleden

        You’re right, that is incredibly dumb. Just not for the reasons you think it is. Imagine using iostream rather than stdio and unironically trying to clown on \n

        • DragonTypeWyvern@midwest.social
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          2 dagen geleden

          Is this not a debate for freshman students and other assorted opinionated know-nothings? Or just people shitposting.

          My mistake. You think it’s srs.

          (And your opinion is still bad)

          • Gustephan@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            2 dagen geleden

            Oh no! Did I hurt your feelings by clapping back when you insulted me on a shitpost comment chain? Your lack of self awareness is astounding

  • Oinks@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    3 dagen geleden

    I am very sorry to remind everyone about the existence of Visual Basic, but it has:

    • VbCrLf
    • VbNewLine
    • ControlChars.CrLf
    • ControlChars.NewLine
    • Environment.NewLine
    • Chr(13) & Chr(10)

    And I know what you’re asking: Yes, of course all of them have subtly different behavior, and some of them only work in VB.NET and not in classic VB or VBA.

    The only thing you can rely on is that “\r\n” doesn’t work.

  • ulterno@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    3 dagen geleden

    Simple. \n when you just want a newline.
    println 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.

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        2 dagen geleden

        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

  • r00ty@kbin.life
    link
    fedilink
    arrow-up
    0
    ·
    3 dagen geleden

    Maybe c# has similar. There’s \r\n or \n like c++ and Environment.NewLine.

    Probably it’s similar in that Environment.NewLine takes into account the operating system in use and I wonder if endl in c++ does the same thing?

    • vithigar@lemmy.ca
      link
      fedilink
      arrow-up
      0
      ·
      3 dagen geleden

      C# also has verbatim strings, in which you can just put a literal newline.

      string foo = @"This string 
      has a line break!";
      
  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    3 dagen geleden

    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.

    • SqueakyBeaver@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      0
      ·
      2 dagen geleden

      I went digging in cppref at the format library bc I thought c++20 or c++23 added something cool.

      Found std::print and was about to reply to this comment to share it bc I thought it was interesting. Then I read the last sentence.

      Darn you and your predicting my every move /j

  • palordrolap@fedia.io
    link
    fedilink
    arrow-up
    0
    ·
    3 dagen geleden

    If endl is a function call and/or macro that magically knows the right line ending for whatever ultimately stores or reads the output stream, then, ugly though it is, endl is the right thing to use.

    If a language or compiler automatically “do(es) the right thing” with \n as well, then check your local style guide. Is this your code? Do what you will. Is this for your company? Better to check what’s acceptable.

    If you want to guarantee a Unix line ending use \012 instead. Or \cJ if your language is sufficiently warped.

    • pelya@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 dagen geleden

      Ah don’t worry, if you do fopen(file, "w") on Windows and forget to use "wb" flag, it will automatically replace all your \n with \r\n when you do fwrite, then you will try to debug for half a day your corrupted jpeg file, which totally never happened to me because I’m an experienced C++ developer who can never make such a novice mistake.

    • Fonzie!@ttrpg.network
      link
      fedilink
      arrow-up
      0
      ·
      2 dagen geleden

      For me the answer is “Building backend applications with it instead of CLI applications, like Lerdorf intended.”

      But also "\n" because it’s easier and PHP_EOL is just an alias for "\n"; it’s not even platform-dependent.

      • Rikudou_Sage@lemmings.world
        link
        fedilink
        arrow-up
        0
        ·
        2 dagen geleden

        PHP_EOL depends on your host system, it’s \r\n on Windows.

        I don’t really want to use what Lerdorf intended, PHP <= 4 was horrible, 5.x was mainly getting slowly rid of nonsense and with 7.x PHP started its slow path of redemption and entered its modern era.

        While Lerdorf’s vision was great at that time for its intended use case, I wouldn’t want to build anything serious in it.

    • edinbruh@feddit.it
      link
      fedilink
      arrow-up
      0
      ·
      3 dagen geleden

      Unix needed only \n because it had complex drivers that could replace \n with whatever sequence of special characters the printer needed. Also, while carriage return is useful, they saw little use for line feed

      On dos (which was intended for less powerful hardware than unix) you had to actually use the correct sequence which often but not always was \r\n (because teleprinters used that and because it’s the “most correct” one).

      Now that teleprinters don’t exist, and complex drivers are not an issue for windows, and everyone prefers to have a single \n, windows still uses \r\n, for backward compatibility.

  • Sibbo@sopuli.xyz
    link
    fedilink
    arrow-up
    0
    ·
    3 dagen geleden

    Wasn’t this {fmt} library merged into STL now? Does this solve this issue?

    Anyways, there was also a constant that is the OS line ending without a flush, right?

    • uranibaba@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      3 dagen geleden

      I haven’t looked at the code but I always assumed that println was a call to print with a new line added to the original input.
      Something like this:

      void print(String text) { ... }
      void println(String text) { this.print(text + '\n'); }
      
      • Scoopta@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        2 dagen geleden

        That is pretty much what it does except it doesn’t hardcode \n but instead uses the proper line ending for the platform it’s running on.