Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/cin"

From cppreference.com
< cpp‎ | io
m (Text replace - "{{example cpp" to "{{example")
m (fmt, http -> https)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{cpp/title|cin|wcin}}
 
{{cpp/title|cin|wcin}}
{{cpp/io/basic_istream/sidebar}}
+
{{cpp/io/basic_istream/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | iostream}}
+
{{dcl header|iostream}}
{{ddcl list item | num=1 | 1=
+
{{dcl|num=1|1=
 
extern std::istream cin;
 
extern std::istream cin;
 
}}
 
}}
{{ddcl list item | num=2| 1=
+
{{dcl|num=2|1=
 
extern std::wistream wcin;
 
extern std::wistream wcin;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
The global objects {{cpp|std::cin}} and {{cpp|std::wcin}} control input from a stream buffer of implementation-defined type (derived from {{cpp|std::streambuf}}), associated with the standard C input stream {{cpp|stdin}}.
+
The global objects {{tt|std::cin}} and {{tt|std::wcin}} control input from a stream buffer of implementation-defined type (derived from {{lc|std::streambuf}}), associated with the standard C input stream {{lc|stdin}}.
  
These objects are guaranteed to be constructed before the first constructor of a static object is called and they are guaranteed to outlive the last destructor of a static object, so that it is always possible to read from {{cpp|std::cin}} in user code.
+
These objects are guaranteed to be initialized during or before the first time an object of type {{lc|std::ios_base::Init}} is constructed and are available for use in the constructors and destructors of static objects with [[cpp/language/initialization#Non-local_variables|ordered initialization]] (as long as {{tt|<iostream>}} is included before the object is defined).
  
Unless {{cpp|sync_with_stdio(false)}} has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input.
+
Unless {{c|sync_with_stdio(false)}} has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input.
  
Once {{cpp|std::cin}} is constructed, {{cpp|std::cin.tie()}} returns {{cpp|&std::cout}}, and likewise, {{cpp|std::wcin.tie()}} returns {{cpp|&std::wcout}}. This means that any formatted input operation on {{cpp|std::cin}} forces a call to {{cpp|std::cout.flush()}} if any characters are pending for output.
+
Once {{tt|std::cin}} is constructed, {{c|std::cin.tie()}} returns {{c|&std::cout}}, and likewise, {{c|std::wcin.tie()}} returns {{c|&std::wcout}}. This means that any formatted input operation on {{tt|std::cin}} forces a call to {{c|std::cout.flush()}} if any characters are pending for output.
 +
 
 +
===Notes===
 +
The 'c' in the name refers to "character" ([https://www.stroustrup.com/bs_faq2.html#cout stroustrup.com FAQ]); {{tt|cin}} means "character input" and {{tt|wcin}} means "wide character input".
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
struct Foo {
+
 
 +
struct Foo
 +
{
 
     int n;
 
     int n;
     Foo() {
+
     Foo()
      std::cout << "Enter n: "; // no flush needed
+
    {
      std::cin >> n;
+
        std::cout << "Enter n: "; // no flush needed
 +
        std::cin >> n;
 
     }
 
     }
 
};
 
};
 +
 
Foo f; // static object
 
Foo f; // static object
 +
 
int main()
 
int main()
 
{
 
{
 
     std::cout << "f.n is " << f.n << '\n';
 
     std::cout << "f.n is " << f.n << '\n';
 
}
 
}
| input=
+
|p=true
10
+
|input=10
| output=
+
|output=
 
Enter n: 10
 
Enter n: 10
 
f.n is 10
 
f.n is 10
Line 43: Line 51:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/ios_base/dcl list Init}}
+
{{dsc inc|cpp/io/ios_base/dsc Init}}
{{dcl list template | cpp/io/dcl list cout}}
+
{{dsc inc|cpp/io/dsc cout}}
{{dcl list end}}
+
{{dsc inc|cpp/io/c/dsc std streams}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 11:35, 11 September 2023

 
 
 
 
Defined in header <iostream>
extern std::istream cin;
(1)
extern std::wistream wcin;
(2)

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.

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 (as long as <iostream> is included before the object is defined).

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.

Once std::cin is constructed, std::cin.tie() returns &std::cout, and likewise, std::wcin.tie() returns &std::wcout. This means that any formatted input operation on std::cin forces a call to std::cout.flush() if any characters are pending for output.

[edit] Notes

The 'c' in the name refers to "character" (stroustrup.com FAQ); cin means "character input" and wcin means "wide character input".

[edit] Example

#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

[edit] See also

initializes standard stream objects
(public member class of std::ios_base) [edit]
writes to the standard C output stream stdout
(global object)[edit]
expression of type FILE* associated with the input stream
expression of type FILE* associated with the output stream
expression of type FILE* associated with the error output stream
(macro constant) [edit]