
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Standard Input Stream (cin) in C++
std::cin is an object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin. The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.
As an object of class istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read. The object is declared in header <iostream> with external linkage and static duration: it lasts the entire duration of the program.
You can use this object to read from standard input to a variable. For example, if you want to read an int value in a variable my_intand then print it to the screen, you'd write −
Example
#include<iostream> int main() { int my_int; std::cin >> my_int; std::cout << my_int; return 0; }
Then save this program to the hello.cpp file. Finally, navigate to the saved location of this file in the terminal/cmd and compile it using −
$ g++ hello.cpp
Run it using −
$ ./a.out
Output
If you give it the input: 15, this will give the output−
15
- Related Articles
- How to get the Standard Input and Output Stream through Console in C#?
- Getting the Standard Output and Standard Error Output Stream through Console in C#
- What is C++ Standard Output Stream (cout)?
- What is C++ Standard Error Stream (cerr)?
- How can we read from standard input in Java?
- Haskell program to read numbers from standard input
- How to convert an input stream to byte array in java?
- Java Program to mark the current position in this input stream
- Java Program to close this input stream and release any system resources associated with the stream
- Java Program to Read The Number From Standard Input
- Kotlin Program to Read The Number From Standard Input
- How to Read The Number From Standard Input in Swift Program?
- How to convert/read an input stream into a string in java?
- Read a character from standard input without waiting for a newline in C++
- How do I clear the cin buffer in C++?
