Input and Output in C++

C++ 141 views Dec 22, 2025 2 min read

Input and output operations allow interaction between the user and the program.


1. Input in C++

C++ uses the cin object to take input from the user.

Syntax

cin >> variable;

Example

int age; cin >> age;
  • >> is called the extraction operator

  • Takes input from the keyboard


2. Output in C++

C++ uses the cout object to display output on the screen.

Syntax

cout << variable>

Example

cout << age>
  • << is called the insertion operator

  • Displays output on the screen


3. Header File Required

To use cin and cout, include:

#include using namespace std;

4. Displaying Text and Variables

cout << "Age: " << age>
  • Multiple values can be displayed using <<


5. Taking Multiple Inputs

int a, b; cin >> a >> b;

6. New Line in Output

Using endl:

cout << "Hello" << endl>

Using \n:

cout << "Hello\n";

7. Complete Example Program

#include using namespace std; int main() { int x, y; cin >> x >> y; cout << "Sum = " << x>return 0; }

8. Input and Output with Strings

string name; cin >> name; cout << name>

Note: cin reads input only until a space.


Key Points

  • cin is used for input

  • cout is used for output

  • >> and << are stream operators

  • endl moves the cursor to a new line


Conclusion

Input and output operations are essential for making C++ programs interactive and user-friendly.

Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials