Saturday 2 February 2013

cout


The cout is built in stream for standard output on the screen. It inserts the sequence of characters into the standard output stream. The cout is defined in the header file iostream within the std namespace. The header file iostream contains the definition of cout. The compiler will give error if header file is not included in the program if cout is used. The objects are written to cout using insertion operator. The insertion operator is <<. It is also called output operator. For example,

            cout << “All work and no play makes Jack a dull boy” << endl;

will display message “All work and no play makes Jack a dull boy” on the screen. Let us consider another example which displays the value of expression.

#include<iostream>
using namespace std;

int main()
{
            int x=5;
            int y=6;
            cout << "The addition of two numbers" << endl;
            cout << x+y << endl;
            return(0);
}

The result is:-

program output

The statement
           
            cout << "The addition of two numbers" << endl;

displays the message “The addition of two number” on the screen. The keyword endl is used so that next characters are displayed from new line. The statement

              cout << x+y << endl;

displays the addition of two numbers 5 and 6 and result 11 is displayed on the screen. The expression is evaluated and result is displayed on the screen.
                       

No comments:

Post a Comment