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 using namespace std;
void main() {
// print the string inside the double quotes
cout << "this is programming" ;
}
this is programming
- Firstly include the header file iostream that provides basic input and output services of C++ programs.
- The cout object is defined inside the std namespace. If this namespace is not used, Computer cannot identify cout and therefore it throws errors.
- Every C++ program starts from the main() function. The code is executed begins from the main() function.
- cout is an object that prints the value inside quotation marks " " using the insertion operator <<.
#include "iostream"
using namespace std;
void main() {
int number1 = 34;
float number2 = 56.3;
cout << number1 ; //print integer value
cout << number2 ; //print float value
}
Outputusing namespace std;
void main() {
int number1 = 34;
float number2 = 56.3;
cout << number1 ; //print integer value
cout << number2 ; //print float value
}
34
56.3
56.3
Example3: Character Output
#include "iostream"
using namespace std;
void main() {
char ch = 'A';
cout << ch ; //print character
}
Outputusing namespace std;
void main() {
char ch = 'A';
cout << ch ; //print character
}
A
Example4: Print more than one thing one the same line
#include "iostream"
using namespace std;
void main() {
cout << "Hello" << " world!"; //print Hello world!
}
Outputusing namespace std;
void main() {
cout << "Hello" << " world!"; //print Hello world!
}
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
}
Outputusing namespace std;
void main() {
int x = 5;
cout << "x is equal to: " << x; //print x is equal to: 5
}
x is equal to: 5
0 Comments
if you have any doubt in programming concept please let me know