std::cin,std::wcin(3)
π std::cin, std::wcin π
okay so bestie, welcome to the C++ Standard Libary page for the two most iconic input girlies in the whole standard library. no cap, these two are the main characters of "hello user, type something" energy, and we're gonna get you fully aquainted. β¨
πΈ NAME πΈ
std::cin, std::wcin β literally just std::cin, std::wcin. that's it. that's the name. the naming committee said "we're done here" and honestly? periodt. π
π Synopsis π
Defined in header <iostream>
extern std::istream cin; (1)
extern std::wistream wcin; (2)
so there's two of them, right:
- (1)
std::cinβ thestd::istreamone π¦ - (2)
std::wcinβ thestd::wistreamone, i.e. the wide version π
The global objects std::cin and std::wcin control input from a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C input stream stdin. like, they're the C++ glow-up of stdin, iykyk. π
these objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed, and are available for use in the constructors and destructors of static objects with ordered initialization β but ONLY as long as <iostream> is included before the object is defined. that's the fine print, bestie, don't skip it. π
unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input. so if you did turn that off, be careful, okay? that's the vibe check. π«Ά
Once std::cin is constructed, std::cin.tie() returns &std::cout, and likewise, std::wcin.tie() returns &std::wcout. what does that mean fr fr? it means that any formatted input operation on std::cin forces a call to std::cout.flush() if any characters are pending for output. so your prompt actually shows up before it blocks. slay. π₯
β Notes β
The 'c' in the name refers to "character" (stroustrup.com FAQ). so cin means "character input" and wcin means "wide character input". the more you know, bestie π«
π Example π
// Run this code
#include <iostream>
struct Foo
{
int n;
Foo()
{
std::cout << "Enter n: "; // no flush needed
std::cin >> n;
}
};
Foo f; // static object
int main()
{
std::cout << "f.n is " << f.n << '\n';
}
Possible output:
Enter n: 10
f.n is 10
not the static object constructor reading from std::cin and then main printing it π this example is lowkey showing off that whole "available during ordered initialization of static objects" thing from up top. iconic behaviour honestly. π
π See also π
Initβ initializes standard stream objects (public member class ofstd::ios_base)coutβ writes to the standard C output streamstdoutwcoutβ (global object)stdinβ expression of typeFILE*associated with the input streamstdoutβ expression of typeFILE*associated with the output streamstderrβ expression of typeFILE*associated with the error output stream (macro constant)
anyway bestie, that's the whole page. go forth and read your input, synchronize responsibly, and remember to tie your streams so your prompts flush. so obsessed with you for reading all the way to the end π§π
source/version: http://cppreference.com β dated 2024.06.10 πΈ mwah, ily, bye π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms