C++ OUTPUT

In this tutorial, we will learn about cout and also give some examples. cout is an object of the output stream that is used to show output.

In C++, The cout object along with the << operator (insertion operator), is used to displaying the output on the screen.

Example1: String Output

#include "iostream"
using namespace std;
void main() {
          // print the string inside the double quotes
cout << "this is programming" ;       
}
Output 
 
this is programming

How does this program work?

  1. Firstly include the header file iostream that provides basic input and output services of C++ programs.
  2. The cout object is defined inside the std namespace. If this namespace is not used, Computer cannot identify cout and therefore it throws errors.
  3. Every C++ program starts from the main() function. The code is executed begins from the main() function.
  4. cout is an object that prints the value inside quotation marks " " using the insertion operator <<.
Example2: Numbers Output

#include "iostream"
using namespace std;
void main() {
int number1 = 34;
float number2 = 56.3;
cout << number1 ;        //print integer value
cout << number2 ;        //print float value
}
Output

34
56.3

Example3: Character Output

#include "iostream"
using namespace std;
void main() {
char ch = 'A';
cout << ch ;        //print character
}
Output

A

Example4: Print more than one thing one the same line

#include "iostream"
using namespace std;
void main() {
cout << "Hello" << " world!"; //print Hello world!
}
Output

Hello world!

Insertion operator (<<) can be used to multiple pieces of output on the same line.

Example5: Print both text and the value of a variable in the same line

#include "iostream"
using namespace std;
void main() {
int x = 5;
cout << "x is equal to: " << x; //print x is equal to: 5
}
Output

x is equal to: 5

Post a Comment

0 Comments