I/O operation in C++

How to perform I/O operation in c++

 What is input ?

  • Information given to the program, is called input
  • In input data is flow inside the program
  • It required a source(for reading )

stadin - - > standard input used as scanf function

stdout --->  standard input used as printf function taken by monitor(gives destination)

What is output :

  • Information given by the program is called output
  • In output data is flow outside the program
  • In c++ I/O operations are alone using streams
  • A stream is class is provide set of functions to perform input and output operations
  • A stream represents input source (reading) and output destination (writing)
  • C++ provides 2 stream classes

  1. ostream
  2. istream


  • .IIstream The istream consists of input functions to read a streams of characters from the keyboard.

  • Ostream The ostream consists of output functions to write a character onto the screen.



stdout is a file pointer which is pointing to console


 I/O operation in C++ Example



Cout ->

  • It is a predefined object of class Ostream
  • Cout represent console output
  • Cout represent standard output devide(Monitor)
  • For cout object destination is standard output device
  • Ostream cout(“ con ”);
  • This predefined object is a variable in IOstream.h file
  • This object uses << (insertion) operator to perform formatted output operation
  • This object uses all the member function of stream class to perform output operation
  • << ( insertion operator) is overloaded operation on member function


ex : printf(“%d”,IO)//                               This operation performs O/P operation without using formatting specifier.



  • This operation insert one values
  • Insertion means writing / printing Cout << ’10,20 ; // it will print 10


Syntax :

cout<< variable/constant/expression;

In order to print multiple values cout uses multiple insertion operators.

Cout<value 1<value2<value 3…….

  • Using multiple insertion operator with cout is called cascading , it is a method of organizing
  • evaluation of insertion operators are are done from right to left and insertion is done from left to right


Example :

# include<iostream.h>

void main()


cout<< “welcome to c++”;


Eg :-

#include <iostream.h>

void main()


cout<<10;                 // cout <<10<<end l;

cout<< 1.5;                // cout<<1.5<<end l;

cout << ‘A’;                  // cout<<’A’<<end l;

cout<<1/5;                  // cout<<1.5<<end l;

cout<<’A’;                    // cout << ‘A’ << end l;

cout<< “c++”               // cout << “c++” <<endl;


endl : It is a manipulator used for inserting new line

  1. Q) What is the difference between \n and endl ?

In is a back slash is escape sequences



it is called manipulatorit is called escape sequence
It doesn’t occupy any spaceIt occupy 1 byte
It is available in iostream.hit is available in ‘C’





# include <iostream.h>

void main()


cout<<10<<1.5<<’A’<<”c++”;     // cascading



Cin : It is an object of istream class.

  • istream class provide set of functions to performs input operations
  • Cin uses >> (extraction) operation to read any type of value
  • It does not required any formatting specifiers

Formatting is an implicit functionality of extract of operation

  • It is an overloaded operation // reading (source) writing Cdestination which extend functionality of >> (shift operator)

syntax :

Cin >> variable –name;

  • One extraction operator read only one value cin>>var1>>var2>>var3----
  • Using multiple extraction is called as cascading

Variable (local) :

The variable declared inside function is called local variable.

  • Memory for this variable is allocated on execution of function and after completion of function
  • The scope of this variable within function
  • Local variable are called auto variable.
  • Auto is storage class and variable is never declared without class
  • Storage class define scope life time and storage of variable.

Eg :


void main()


int x;

int Y;

int Z;

Cout << X << endl;



o/p : The output of above program is garbage value, Because local variables are not given any default values.

  • C++ allows to declare local variable anywhere within function

eg :

# include <iostream.h>

void main()


int n1;

cout<< “\n input n1 value”;

cin >> n1;

int n2;

cout<< “\n input n2 value”;

cin>> n2;

int n3;


cout<< “\n sum is ” << n3


C++ allows to declare block level variables