C++ insertion(<<) and extraction(>>)
The insertion and extraction operators are used to write information to or read information
to or read information from, respectively, ostream and istream objects.
By default, white space is skipped when the insertion and extraction operators are used.
The insertion operator << (Common Output - cout) :
The insertion operator ( << ) points to the ostream object wherein the information is inserted.
The normal associativity of the << -operator remains unaltered, so when a statement like :
cout << "hello" << "world";
is encountered, the leftmost two operands are evaluated first (cout << hello).
cout is an ostream object, into which information can be inserted. This stream is normally
connected to the screen.
cout << is used for displaying the message or a result of an expression.
It is same as of printf() function used in c language.
Syntax :
cout <<"Message to print'' or result to be show
Eg :
cout << "Welcome to c++ ";
The extraction operator >> (Common Input- cin) :
cin is an istream object from which information can be extracted. This stream is normally
connected to the keyboard.
cin is used to insert the information or the value of a variable.
This works same as scanf() works in C language.
Syntax :
cin >> variable value;
Eg :
cin >> x;
where x is a variable.
iostream.h header file :
To use cin >> and cout << we have to include the "iostream.h" header file in our program.
This header file contains the definition (prototype) of both cout and cin.
Prev NEXT
0 comments:
Post a Comment