Monday, July 20, 2009

c++ cout (displays output to console)


Display output to console

In c++, cout is used to display the data to the console. The statement #include <iostream> is used add the file - iostream to the source code. The file - iostream (input-output-stream) is used by cout and its related function.

Syntax of cout ?

Type cout followed by insertion operator (<<) followed by your data and terminate it by semi-colon. The following program demonstrates the use of cout that displays integers, decimal equivalents string and so on.


A C++ Program example that demonstrate the various implementation of c++ cout


#include <iostream>

int main ()
{

    std::cout << "Lets have a look on cout !!" << endl;
    std::cout << "The number 99 : " << 99 << endl;
    std::cout << "The sum of of 9 + 8 : " << 9 + 8 << endl;
    std::cout << "The division of 7 / 18: "<< float (7 / 18) << endl;
    std::cout << "The multiplication of 6000 & 6000: "
                   << double (6000 * 6000) << endl;
    std::cout << "Replace Mohammed Homam with your name ..."
                   << std::endl;
    std::cout << "Mohammed Homam is a C++ programmer"
                   << std::endl;

    return 0;
}


C++ Notes :

The \n symbol is an special formatting character. It is used to write new line to the screen. The manipulator std::endl is also used to write new line to the screen. like cout, endl is also provided by standard library. Therefore, std:: is added in front of endl just as it was added for cout. But use of endl is preferred over \n because it is adapted to the operating system in use. The term float and double tells cout to display the number in floating-point value.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

1 comment:

  1. But use of endl is preferred over \n because it is adapted to the operating system in use.

    sir please elaborate the above that how it is adapted to the operating system?

    ReplyDelete