C++

Using std::cout

You may have heard about the concept of objects in programming i.e., object-oriented programming. Just like those objects, C++ came up with the “cout” object that has been predefined in its output stream class. This cout object or statement has been specifically designed to output or print out the input data on any output area. But the word “std” stands for “standard” i.e., standard output on the standard output screen. It is a namespace that must be used within the C++ code to display the standard output. Without the standard namespace, we cannot use the “cout” object in the code. So, we have decided to write this article for our Linux users who want to see the use of std::cout in C++ while using the G++ compiler on the Ubuntu 20.04 Linux operating system. Let’s just look at it once.

Make sure you have been already logged in from your Ubuntu 20.04 system. After the login, you need to open up Ubuntu’s shell console to do work within it. To make sure that your system is up to date, try using the “apt” instruction to update and upgrade it with sudo rights. After that, we need a C++ file to start adding our C++ code to it. If you don’t have one, don’t worry. Try making one with Ubuntu’s “touch” instruction on the shell with the new name of a file having a “cc” extension with it. Most probably, this file can be found in Ubuntu’s home folder. You need to open it so that we can create code in it. For opening it, you may use the Linux built-in editors like a text editor, nano editor, or vim editor. We will recommend you to use either the “nano” editor or the text editor because both these editors are easy and convenient to use while coding and execution.

Example 01:

Let’s begin with our article’s first example. Within this example, we will not be making use of the “std” namespace along with the cout object to see how it results. So, within the empty C++ file, add the C++ standard input-output stream header in our code. This will allow the C++ program to take input and display the output value on screen. We have been utilizing the main() function of C++ to perform the implementation of logic in the code. Within the main() function, we have been simply using the “cout” object or statement to display a string-type text value on the shell screen of the Ubuntu 20.04 system. After this, the return 0 statement is used to quit the program with zero errors. Now the program is completed and ready to be compiled. Make sure that your Ubuntu 20.04 system has the G++ compiler for the C++ language already installed and configured to make the code error-free. Save the code shown below by utilizing the “Ctrl+S” shortcut and exit the Gnu Nano editor via the Ctrl+X shortcut usage.

After you have done coding, try using the g++ compiler to compile the code. In doing so, we have got the error showing that we have not been using the standard namespace before the cout object. This means, to make our code error-free and cout works, we need to add the “Std” keyword as a namespace for the cout statement object. Let’s change the code now.

So, open the file “stdcout.cc” once again in the Nano editor by utilizing the “nano” instruction on the shell. Now, the file is opened. We will be updating it by adding the “std” namespace before the “cout” object in the main() function separated by the “::” double colon sign. As we have been using the “endl” object to add a line break after the display of text, we need to use the “std” namespace for this object as well. So, we have to use the “std” namespace before the “endl” object separated by double colon “::” as shown in the below image. Without the use of the std namespace, we may not be capable of accomplishing the results. Let’s save our code with the Ctrl+S shortcut key on the nano editor and exit the editor with the Ctrl+X shortcut.

After compiling the above-updated code with the G++ compiler, we got the code error-free and it was successful as it doesn’t return anything. After the use of the execution command “./a.out” on the shell, we have got the text string displayed on our shell screen. You can see the use of both the commands and the output in the image below.

Example 02:

This was the first method to make use of the “std” namespace along with the “cout” object to display the output on our shell screen while using the g++ compiler. If you want to avoid using the “std” namespace with the cout like objects and “endl” object, you can also do that in your C++ code. For that, you need to define the “std” namespace at the start of the code before the main() function and after the headers or libraries. So, we will be using this example to see the second method of using the “std” namespace separately to get the same result as we got with the use of “std” with the “cout” object. So, we have opened the same file and added the “using namespace std” line before the main() function shown in the image below. We didn’t use the “std” namespace with the “cout” object after that within the main() function. Let’s save the code and see how it results.

After compiling and executing this C++ program, we have got the same results as we got in the above example. But, in general, the use of std namespace before the main() function doesn’t consider good practice in C++ programming. So, make sure to avoid it.

Example 03:

Let’s take a look at the last but not the least example of using the “std::cout” in our C++ program. Within this example, we will also looked at the use of “std::cin” to get input from the user to display it on the shell after that. So, the first “std::cout” is used to display that we have to enter something as an input. An integer variable “var” of size 5 has been declared. After this, the “std::cin” is here to take input from the user and save it to the variable “var”. This input must be of “integer” type otherwise the compilation will through an error. The last “std::cout” statement is utilized to exhibit the variable value in the shell.

After the code compilation and execution, the user has added the integer value separated by space. But the variable has taken only the first value before the space and displayed it and doesn’t consider the space and after that as an integer value.

Conclusion

This was all about the use of the “std::cout” statement in C++ using different methods. We have taken a look at the program which was not using the “std” with cout statements and understood how much the “std” keyword is necessary for our C++ code. The code examples also include the use of “std” before the “cout” object and before the main() function so that our users can not miss anything while learning.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.