COUT COMMAND IN C++ LANGUAGE
Introduction:
“Character (C) + Output (Out) = Character Output (Cout). In C++, the court object represents an instance of the stream class. The attribute is declared within the iostream header file.”
In C++ programming language, the output of an program is shown on the screen via cout. The characters 'c' & 'out' in cout represent characters and output, respectively. Character output is thus denoted by cout. To output structured data, we utilize cout in conjunction with the insertion operator. The iostream header file contains the C++ definition for cout. We need to include namespace std into our code in order to use cout. In the absence of it, our program would have to use std::cout rather than cout.
In C++ programming language, cin, and cout are actually object-oriented and provide type-safe input as well as output; they are regarded as a user-friendly method of performing input as well as output in C++ programming language. The "<<" operator is employed to show the result on display, which is a common output device. It is connected to the stdout standard C output stream. The insertion operator (<<) is utilized for inserting the data that has to be displayed on the screen into the standard output stream ( cout ).
Syntax for Cout in C++ language:
The following syntax will show us how to use cout in C++:
cout << output
Here, the result is printed using the C++ insertion operator <<. Subsequently, the output required will be displayed on the screen. This output could be a constant, variable, element inside an array, or any of the elements of a container (set, map, etc.).
Exceptions for Cout in C++ language:
The only kind of output that cout can provide is for primitive data types if we attempt to process non-primitive data types structures, classes, etc. We will receive an exception. We must overload the << operator in order to prevent the mistake.
Example for using cout command:
We'll use the following example to show how to utilize C++ through assigning a variable's value and then printing that variable.
Code:
# include <iostream>
using namespace std;
int main () {
int var;
var = 100;
cout << var;
return 0;
}
Output:
In the code above, we called our variable var; then we gave it its value as 100. By employing cout, we displayed the variable on the screen. Therefore, the output showed the variable's value, which is 100.
What does C++'s Cout mean?
We must first comprehend what streams constitute in order to fully understand the cout parameter. A series of bytes is referred to as a stream in C++. They enter and exit the program. In C++, the different operations for input and output are handled by the stream classes. The header file contains these classes. Both input as well as output streams are the two different types of streams. The stream class manages an input stream, whereas the ostream class handles the output stream. Data bytes are transferred from the application into the device display via the output stream. The istream as well as the ostream classes both derive from the ios class. In C++, an object that belongs to the ostream class is called a cout object.
As was previously mentioned, the output is shown on the screen via the standard output device, or cout. Since cout is a component within the iostream header file, it is also an aspect of the identical library as the ostream class. As a result, each time we need to utilize the cout object inside our program, we must add the isotream header file.
Data that is formatted as well as unformatted may both be utilized with cout. Unformatted data is simply unconverted bytes; formatted data represents data that has had its bytes changed into a specific data type, like float, int, etc. The insertion operator (<<) is utilized when executing cout through formatted data, while member functions (such as put ()) are employed when accessing cout through unformatted data.
Now, let's look at how we could present our work better. For formatting our output, we make use of cout in conjunction with manipulators. Let's go over the most often utilized ones first:
- setprecision: This command sets the float or decimal value's precision.
- setw: This command sets the output operation's field width.
- setfill: This command inserts a specific character into the stream.
If you utilize any of the manipulators listed above, make sure that your program includes the <iomanip> header file.
Additional Examples of Cout in C++:
Let's look at some more examples to show you how to make use of cout in C++.
Program 1: Write a program that prints each element in an array:
# include <iostream>
using namespace std;
int main () {
int num;
cin >> num;
int array [ num ];
for (int j = 0; j < num; j ++)
{
cin >> array [ j ];
}
for (int j = 0; j < num; j ++)
{
cout << array [ j ] << " ";
}
return 0;
}
Output:
Explanation: We display every element of an array with size n on the monitor after receiving input from it.
Program 2: Writing a Program to demonstrate how to use cout.precision ():
# include <iostream>
using namespace std;
int main () {
double num;
num = 2.578964;
cout << num << endl;
cout.precision ( 5 );
cout << num << endl;
cout.precision ( 3 );
cout << num << endl;
return 0;
}
Output:
Explanation: As shown in the program above, cout.precision ( int num ) fixes the value of the decimal to Num.
Program 3: Write a Program to show how to use cout.write ():
# include <iostream>
using namespace std;
int main () {
int num;
cin >> num;
char array [ num ];
for (int j = 0; j < num; j ++)
{
cin >> array [ j ];
}
cout.write (array, 4) << endl;
cout.write (array, 1) << endl;
cout.write (array, 5) << endl;
return 0;
}
Output:
Explanation: The program mentioned above illustrates that, the character array's initial N elements are printed using the command cout.write ( char array, int N ). T
Program 4: Write a program to illustrate many insertion operators:
# include <iostream>
using namespace std;
int main () {
int num;
cin >> num;
char array [ num ];
for (int j = 0; j < num; j ++)
{
cin >> array [ j ];
}
cout << " The first element of the input array is : " << array [ 0 ] << " " << " The last element of the input array is : " << array [ num - 1 ];
return 0;
}
Output:
Explanation: To print several outputs on the display, we can utilize multiple insertion operators. We call this as insertion operator cascading.
Program 5: Write a program to show how to use cout.put ():
# include <iostream>
using namespace std;
int main ()
{
char array [7] = "klmngp";
char x = 'k';
cout.put ( x );
cout << endl;
x = 'm';
cout.put ( x );
return 0;
}
Output:
Explanation: The character retained in the character variable x is printed using cout.put ( c ). The program mentioned above illustrates this.
Conclusion:
In this tutorial, we have covered briefly about the cout command in C++ programming language. To show output on the monitor, we make use of cout. Initially, we covered the details of both input and output commands which are cin and cout, then we discussed about cout command. The insertion operators (<<) are used for cout along with the formatted data. When dealing with unformatted data, we employ member functions like put () that utilize cout. Other member functions like cout.write (), cout.precision (), and others additionally make use of cout. When using cout, manipulators like set precision, setw, and other functions are utilized to format and improve the appearance of our output. Hope this information will provide value in your day to day programming life.