I/O operation in C++

Ratings:
(4)
Views: 1054
Banner-Img
Share this blog:

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 Screenshot_3  

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. printf(“%f”,1.5)  

  • 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  

endl

  \n
it is called manipulator it is called escape sequence
It doesn’t occupy any space It occupy 1 byte
It is available in iostream.h it is available in ‘C’

      eg: # 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 : #include<iostream.h> void main() { int x; int Y; int Z; Cout << X << endl; cout<<Z<<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; n3=n1+n2; cout<< “\n sum is ” << n3 } C++ allows to declare block level variables  

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox