Standard Output (cout) & Input (cin) in C++

Standard Output (cout)

  • The cout and insertion (<<) operator can print a message on the screen.

  • Example:
    cout<< "Welcome to TutorialRide!!!";

  • To print the variables, there is no need to used double quotes ( “” ).

  • Example:
    int a=10;
    count << a;

  • The insertion operator (<<) can be used multiple times in a single statement.

  • Example:
    cout<< "Welcome" << "to" << "TutorialRide";

  • It is possible to combine variables and text.

  • Example:
    int a = 10;
    cout<< "Print number"<<a;

Standard Input (cin)

  • The cin and extraction (>>) operator reads input from the keyboard.

  • Example

    int num;
    cout<< "Enter number:"
    cin>>num;
    cout<<num;


  • The cin operator returns the variable type.

Manipulators

  • Manipulators are used to manipulate the data by the programmer's choice of display.
  • It is used to specify certain formats of the output to be displayed when using the cout statement.
  • It performs certain special operations such as moving the cursor to new line, fixing the width of a data value etc.
Following are the manipulators used to display program's output properly:

ManipulatorsDescription
endlIt feeds the whole line and then points the cursor to the beginning of the next line. '\n' is used instead of endl for the same purpose.
setwIt sets the minimum field width on output.
setfillIt is used after setw manipulator. It is used for filling the fields.
setprecisionIt is used to set the precision for a data after the decimal point.

Storage Classes

  • Storage class is a type specifier that governs the lifetime, linkage and memory location of objects.
  • It defines the scope and lifetime of variables or functions within a C++ program.
Following are the C++ storage classes:

Storage ClassDescription
autoIt is the default storage class for all local variables.
registerThis storage class is used to define local variables that should be stored in a register instead of RAM.
staticThis class is used for specifying the static variable.
externThis class is used to give a reference of a global variable which is available to all program files.
mutableThis class allows a member of an object to override constness that means this class can be modified by a const member function.