C++ I/O Streams (cin, cout)
Introduction
C++ uses stream objects cin (standard input) and cout (standard output) with >> (extraction) and << (insertion) operators.
Definition
Stream: A flow of bytes. cin/cout are predefined objects in <iostream>.
Code Example
#include <iostream>
using namespace std;
int main(){
int x; cout<<"Enter: "; cin>>x;
cout<<"You entered: "<<x<<endl;
return 0;
}Important Questions
Short Questions
- What is cin?
- What is cout?
- Difference between >> and <<?
- What is endl?
Long Questions
- Explain stream I/O in C++.
- Discuss cascading of I/O operators.
MCQ Practice
1. cin is defined in:
- <iostream>
- <stdio.h>
- <conio.h>
- <stdlib.h>
Answer: <iostream>.
2. Extraction operator is:
- >>
- <<
- ::
- .*
Answer: >>.
3. Insertion operator is:
- <<
- >>
- ::
- ->
Answer: <<.
4. endl does:
- Newline + flush
- Only newline
- Nothing
- Clears stream
Answer: newline + flush.
5. Cascading means:
- Multiple <</>> in one statement
- Looping
- Recursion
- Inheritance
Answer: multiple operators.
Quick Revision
- cin/cout streams.
- >> extract, << insert.
- endl newline+flush.