Learn the concept of standard input and output stream in C/C++ programming. Before learning about the input and output in C/C++ you first need to be clear about what’s exactly happening on the back end during this process. And how the standard streams handle input and output operations.
Table of Contents
Real-World Example of Stream in Programming
For a moment let’s forget about the technical stuff and learn using imagination. In real-world water flowing a river is a representation of a stream that is coming from a source and reaching a destination. In programming the only difference is instead of water, the bits (data) flow through standard streams from a source to a destination. The standard streams are like a pathway that lets your program read or write data using standard input/ output devices. It’s a useful functionality for dealing with input (getting information) and output (displaying or saving data).
Types of Streams in C/C++
stdin – Standard Input Stream in C++
The standard input stream connects your program to the standard input device i-e the keyboard, so you can read the data entered by the user.
stdout – Standard Output Stream in C++
The standard output stream connects your program to the standard output device i-e monitor so that you can write (display) data on the screen to the user.
stderr – Standard Error Stream in C++
This stream displays error messages on the screen encountered during the program’s execution.
What Happens During Input Output Process in C/C++ Program?
Now let’s understand in slow motion what exactly happens on the back-end during the process of input and output. The visual below represents a side-by-side comparison of the process and elements in programming and the real world. Each element in programming has its corresponding element in the real world that helps understand the process.
In the real world, first, you have the source i-e the water well. The water travels through the pipe (input stream) to the water drum in your home. The drum (program) is an intermediary. So, the pipe that originates from the source acts like an input channel to the drum. Similarly, the pipe that goes outward from the drum acts like an output stream. Because the water then travels through this pipe to the destination i-e the garden.
Now let’s connect this example in programming. You have the source i-e keyboard (standard input device) from where the data is generated. This data travels to your program using the standard input stream and gets stored in the variables. The program is also connected to the standard output device i-e monitor using the standard output stream. When you display the variables the values in your program travel through the standard output stream to get displayed on the screen.
Conclusion
To sum up, standard streams are the channels that through which your data travels in and out of your program from/to the standard I/O devices. Most programmers simply jump to user input and output without having a clear idea of what’s exactly happening on the back end. To make a difference, you must be clear about the core concept of standard I/O streams that help you be a better programmer. After that, you are good to go for user input and output in C and C++.
Pingback: User Input / Output In C++ - Syntax Scenarios