Input & Output (Basics)Input & Output (Basics)I/O Basics
Terminal I/O Streams
#include <iostream>
int main() {
int i;
// read value into i
std::cin >> i;
// print value of i
std::cout << i << '\n';
}
Sources and Targets of Data
std::cin |
characters from stdin |
reads from buffer |
std::cout |
characters to stdout |
writes to buffer first,
output to console when buffer full |
std::clog |
characters to stderr |
writes to buffer first,
output to console when buffer full |
std::cerr |
characters to stderr |
immediatly writes to console |
Stream Operators
>> |
"get from" | source >> target |
<< |
"put to" | target << source |
- work for fundamental types and strings (support for other types can be added)
>>
reads until next whitespace character (space, tab, newline, …)- can be chained:
int i = 0;
double d = 0.0;
// read 2 values of different type:
cin >> i >> d;
// print 2 values and some text:
cout << "your input:\n"
<< i << " and " << d << '\n';