C++ Output Statement

cout object of iostream is used to print data to on the console device (Monitor). It is read as (see-out). cout is always used with insertion operator <<. Each cout statement must ends with semicolon.

Syntax of cout statement

The syntax of cout statement is as.

cout << Format String | variable | constant | expression ;

cout

cout is the object name from ostream class. which is part of iostream header file.

<< (insertion operator)

Insertion operator is used with cout object object it can be used multiple times with single cout object.

Format String

Format string is enclosed in double quotes, and written after insertion operator. A valid format string may contains the following elements.

1. Text

This text will printed as it is written in the double quotes.

2. Escape Sequence

Escape Sequence are the special characters that have special effect in format string. These are always starts with \ (backslash).

Variable

Variable is a named memory location which may contain some value. When name of variable is provided in cout statement then it print its value not the name of variable.

Constant

Constants are defined words that never change after its defination. Whenever constants are used with cout statement, it prints it's value.

Expression

Expressions are the of mathematical formula's. These are combination of operators and operands. Whenever expressionsa are used with cout object it first evaluates (solve) and then print it's value.

Example of cout Statement with single insertion operator.

#include<iostream>
using namespace std;

#define MAX_LIMIT 100

int main() {
    int a = 10;
 
    // print format string.
    cout<<"Welcome to IMS";
    //print variable
    cout<<a;
    //print constant
    cout<<MAX_LIMIT;
    //print expression
    cout<<MAX_LIMIT + a;

return 0;
}

Example of cout Statement with multiple insertion operator.

#include<iostream>
using namespace std;

#define MAX_LIMIT 100

int main() {
    int a = 10;
 
    // print format string, variable, constant, expression in single cout statement.
    cout<<"Welcome to IMS"<<a<<MAX_LIMIT<<MAX_LIMIT + a;

return 0;
}

Important Note: The output of cout statement with single insertion operators and cout statement with multiple insertion operators will be the same.

Comments
Login to TRACK of Comments.