#Best way to print a string

82 messages · Page 1 of 1 (latest)

steady crater
#

Hi, in C++ what's the best way to print a string? will be fine if I just always use std::cout? Or should I use other functions?

deep spokeBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

solid epoch
#

depends on what your goals are

#

std::cout is the best way if you want to use streams or if you want to keep compatibility with old versions of C++

#

if you are using c++23 or newer you can use std::println

#

I personally keep large projects on c++20 still and write my own small abstraction for printing

#

but for small projects I use c++23/c++26 and std::println

toxic pewter
steady crater
#

so I should update to C++26 whenever possible and use std::println

toxic pewter
#

C++23

solid epoch
solid epoch
#

using older c++ versions is usually prefared for large scale projects

#

for better support

toxic pewter
steady crater
#

oh my bad I'm new to this, so what standard should I use

#

23?

solid epoch
#

I already said

#

I like using c++20 myself

#

it has pretty good support

toxic pewter
#

if you are just learning , trying stuff out, use ⁨20⁩ or try ⁨23⁩ if u can

solid epoch
#

c++17 if really wide support is essential

toxic pewter
#

MSVC doesnt have 23 fully supported yet so im just using 20 for everything

steady crater
#

and what about c++11

solid epoch
#

if you are in school use whatever the school uses so you dont try to use a feature that doesnt exist on an exam

toxic pewter
solid epoch
#

its prety old

steady crater
#

but if I can code on it

#

will the code be more compatible

solid epoch
solid epoch
steady crater
#

ok thanks

solid epoch
#

c++20 is generally widely supported rn

toxic pewter
solid epoch
#

I woudent dip belowe c++17 if I were you tho

#

my golden standard is c++20 for big projects and c++26 for someting I can write in a day

#

just for convience

steady crater
#

ok ty

#

very helpful

deep spokeBOT
# deep spoke

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

wanton pike
solid epoch
#

if you need it to run on old or niche machines support can be an issue

#

but usually it isnt

wanton pike
#

hmmm well as long as they don't care about MSVC, C++26 support is good

solid epoch
#

the older something is the less likely for a undescovered security vurnerability too

solid epoch
wanton pike
#

stable enough for me :P

solid epoch
#

modules are still pretty shakey from way back in c++20

#

especially if your running on older machines with older compilers

formal fossil
#

I mean, I couldnt see an option for cpp98 on msvc

solid epoch
#

no one likes msvc its ok

#

we haved mingw :3

north karma
#

use std::print and std::println anyway

#

you can just pass ostream into it

solid epoch
#

thats c++23

north karma
#

well, c++26 is comming so

cobalt shore
#

Because of the buffer when debugging, std::cout can send outputs in a non-linear way. Therefore, for debugging, std::cerr is generally used, which sends outputs directly to the output.

wanton pike
#

Really? Since when is std::format constexpr

#

cppref says it isn't :(

cobalt shore
wanton pike
#

oh, that's nothing special though

cobalt shore
#

Well, I think std::cout and std::format are still a good combination. std::cout has its buffer to optimize output, and you can use std::format to improve readability.

solid epoch
steady crater
#

Best way to print a string

north karma
#

nvm default is std::print(stdout, ...);

steady crater
#

personally i use std::cout << ... + std::cerr << ... + std::clog << ... + std::cin >> ... + std::getline std::cin, ... + std::wcout + std::wcerr + std::wclog + std::wcin + std::getline std::wcin, ... + std::printf + std::fprintf stdout, ... + std::puts + std::putchar + std::putc ..., stdout + std::scanf + std::fscanf stdin, ... + std::gets_s + std::getchar + std::getc stdin + std::wprintf + std::fwprintf stdout, ... + std::putwchar + std::getwchar + std::format + std::cout + std::vformat + output stream + std::print + std::println + std::ostringstream → std::cout + std::istringstream ← std::cin + std::stringstream + std::ostream::write + std::istream::read + std::istream::get + std::istream::getline + std::istream::ignore + std::ostream::put + std::sync_with_stdio false + C stdio + mixed usage + std::basic_ostream<char> + std::basic_istream<char> specializations + std::basic_ostream<wchar_t> + std::basic_istream<wchar_t> + std::spanstream + std::basic_syncbuf + std::osyncstream + std::ranges-based input via iterators over std::cin + std::istream_iterator<T> + std::ostream_iterator<T> + std::freopen "CON", ... + std::system "echo ..." + std::streambuf

steady crater
solar harness
#

i mean u want to wrap the std::cout sure, want to add more functionalities sure

#

kinda of a win win i think

#

although 40 years for printf but newer is indeed crazy

cobalt shore
#

Yeah, std::cout has linked to stdout that is the default C output stream. Then i'll use std::print now. This has the same effect of std::cout + std::format