In this part of the tutorial, we’ll cover the basic output functions for simple console (terminal) programs.
The most common operating systems today use graphical user interfaces, so you might ask why I'm writing about simple terminal output here.
This classic form of output has several advantages:
More modern forms of output specialized for graphical user interfaces are more complex and often cannot be used for different output channels without bigger code modifications.Once you know the basic forms of output, the more complex forms are easier to understand.
Before we start properly, you should know what I mean by “console” or “terminal” programs or applications.
In today’s operating systems with graphical user interfaces (GUIs), programs designed for direct interaction with a user open up their own application window. The input and output of the application happens inside this window.
In contrast to this, a console or terminal program is started from the command line inside a terminal window. The terminal window in itself is an application that simulates the behavior of a classic, text-only physical terminal combined with a command interpreter.
This is the kind of interaction that was normal for any computer user up to the beginning of the 1980s, especially the users of mainframe computers. Back in the 1950s and 1960s, the terminals didn’t even have a CRT or LCD display, but used printer mechanics instead.
Terminal programs run inside this simulated environment. The “Hello World” program from before and the upcoming examples use the Qt “QCoreApplication” object. This automatically adds the terminal window with its integrated environment to the current program, which allows us to use the classic forms of output and input which were originally developed for terminal programs.
Let’s start out with a variation of the “Hello World” program from the second part of the tutorial which I’ve named “Console_IO”:
As you can see, I’ve combined the two different versions for text output: the “printf” function and the stream-based “cout” function (which have already been used in the "Hello World" program).
When you run this program, it opens a console window and prints out the text “Hello, World!” followed by “Hello Universe!” in two different lines. The program can be ended by closing its window.
The “Console_IO” program from above uses the special character “\n” as part of the string of characters which are printed to the screen within the console window. You cannot see these characters however, they seem to be invisible.
This is because “\n” describes a special “control”
character that is part of the unprintable control characters in the ASCII
character table (in this case the new line or carriage return character, depending on the operating system). These kinds of characters were originally defined
in order to tell the printer or terminal to perform specific actions as part of
The newline character which is referred to by the string “\n” tells the printer or terminal to go to a new line and continue the output there. So any text that is printed after that appears in the next line. If you delete the newline character from the string, the following text is printed on the same line, up until the physical end of the line, when the line break occurs automatically.
Go ahead and change the text after “printf” and “cout” in the “Console_IO” program. Try to add additional “\n” characters or delete “\n” from the strings, and run the program anew after each change. You will quickly see how the text output works.
Here are some other control characters for the so-called “escape sequences” which can be included in an output string:
|? or \?
|the null character
|octal number "ON"
|hexadecimal number "HXN"
|Unicode (UTF-8) character "xxxx"
|Unicode (UTF-16) character "xxxxxxxx"
Several of these character sequences have an influence on the position of the following output on the screen (or inside the current window etc.). I'll leave it to you to experiment a little. A small demonstration program is available on the Microsoft Visual Studio C++ reference website about C++ character literals.
In C++, there are some additional output channels for text output streams that can be used in the same way as “cout”, the default console output:
So far, our text output has been straight forward,
character after character. While the classic output functions mentioned above
don’t allow complex text formatting like in a word processing application (with
different fonts, text sizes, text styles etc.), it is possible to use a
different output function which offers some basic formatted output: the
“sprintf” function from the C programming language.
Its formatting capabilities are limited to controlling
the positioning and width of text output, especially regarding the output of
values from variables as part of text output. The syntax for "sprintf" looks like this:
I’ll cover variables and functions in more detail
later in this tutorial, so here is just a quick run-down for the “sprintf”
Here is a short example of how this is used:
In the example above, the “%d” placeholders for decimal, numeric values are replaced by the current values of “x” (4), “y” (10) and “x+y” (14) by the “sprintf” function. The resulting string is stored inside the variable “myoutput”, and the length of this string is returned by the function and stored in the variable “stringlength”.
At the end, the “printf” function then prints the formatted string between two square brackets, followed by some additional text including the length of the string.
The output of this program should look like this:
[4 plus 10 equals 14] is a string that is 19 characters long.
Different placeholders are used in the formatting string, depending on the type of variable that holds the values which should be included in the final output string:
Up until now, we have only printed text strings made up of several characters and words. But occasionally we also need the option to print a single character, especially the current values of single character variables.
This can be done with the put character function “putc”. The syntax of this function is as follows:
The first parameter “c” receives the value of the single character that should be written to the output channel. The second parameter “file” points to the output channel. While the syntax seems to point out that this is a file, it actually is an output stream. This can be a text file, a window or monitor or any other kind of output stream.
The return value can usually be ignored.
Here is a short example for using “putc”:
As usual, a console window is initialized and opened here. Then the “putc” function writes all the characters which are stored inside the variables “c1” to “c3” to the output stream, followed by a “new line” character. This produces the output “ABC”.
In this example, the output stream is "stdout", the standard output channel. In a console application, this is the "inside" of the current application window.
That's it for the current part of this tutorial.
The code can be found in the "Console_Output" subdirectory of the git repository on github. You can clone the complete tutorial repository if you already have git or GitHub Desktop installed on your computer, or you can download it as a ZIP file.
The next part of the tutorial will deal with the basic forms of data input.