Pure Programmer
Blue Matrix


Cluster Map

Console Output

Every program, every function, everything we do when we program consists of three fundamental operations: input, processing and output. This pattern of accepting input, doing some sort of processing action, then emitting output is at the heart of writing programs. Once you master these ideas, you will be able to construct many types of programs from console utilities, to background daemons to [[GUI]] (Graphical User Interface) programs.

The first of these that we will explore is output. For console-type programs, output consists of a stream of characters that are displayed on the console. In C++ our programs automatically have access to the standard output stream ([[stdout]]) which is represented by the symbol cout. We can send characters and strings of characters to cout to cause them to appear on the console. This is done using the "put to" operator represented by the symbol <<.

ConsoleOutput1.cpp
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
	cout << 'H' << 'e' << 'l' << 'l' << 'o' << endl;
	return 0;
}
Output
$ g++ -std=c++17 ConsoleOutput1.cpp -o ConsoleOutput1 -lfmt $ ./ConsoleOutput1 Hello

Starting with our boilerplate code for a console C++ program, we then change the code inside the body of the main method. The body of the main method consists of the statements between the curly braces {} in the above example. Note that programming statements in C++ always end with a semicolon.

The output statement sends five individual characters to the standard output represented by cout. This is accomplished by using the "put to" operator << five times in sequence. Each of the characters are wrapped in single quotes which is the C++ way of representing character literals. The final symbol that is sent to cout is the special symbol endl which represents the new line character (or characters).

In the above example we have two statements in the body of the main method. The first statement is an output statement that sends five characters to cout. The second statement is our boilerplate return statement.

The end-of-line character is not universal across different types of computers. Text output (and text files) on Unix/Linux systems use the [[ASCII]] line feed character (LF is value 10) to mark the end of a line. Macintosh OS X text uses the ASCII carriage return character (CR is value 13) as the end-of-line character. DOS/Windows systems use the CR LF character pair to signify end-of-line. Output to the standard output in C++ is smart enough to use the correct end-of-line character(s) for the operating system on which our program is running when we use the special endl symbol in our code.

The cout object is used to write [[ASCII]] characters to the standard output device generally the console. If you want to write [[Unicode]] characters in C++ you need to use the wcout or wide character output object. Also you need to put the modifier symbol 'L' in front of all character literals in your program. Also don't forget to include the setlocale and wcout.imbue code.

You can not mix usage of cout and wcout in your program. You must use one or the other consistently.

Below is an example of the previous example using [[Unicode]] (wide) characters in C++.

ConsoleOutput1u.cpp
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8"); 
#ifdef __APPLE__
	wcout.imbue(locale("en_US.UTF-8"));
#endif
	wcout << L'¡' << L'H' << L'o' << L'l' << 'a' << '!' << endl;
	return 0;
}
Output
$ g++ -std=c++17 ConsoleOutput1u.cpp -o ConsoleOutput1u -lfmt $ ./ConsoleOutput1u ¡Hola!

Compile and Run

To try out the program above, copy and paste the source into your programming text editor, then save it in a file called "Hello.cpp". To organize your work, it is easiest to create a folder on your desktop to hold your source files. For our purposes we will assume that you name this folder "tutorial". After saving your source file, bring up the command-line terminal and execute the commands illustrated below.

$ cd Desktop $ cd tutorial $ g++ Hello.cpp -o Hello $ ./Hello Hello $

The cd commands cause the current directory (cd) to be changed. Since in a new terminal we start with the current directory set to your own home directory, we change once to the "Desktop" directory, then again into the "tutorial" directory. The next command invokes the C++ compiler to compile our "hello.cpp source file into an executable. The "-o" option is used to specify the name of the executable program that is created. The final command runs the "hello" executable. Since it is often the case that the current directory is not in your PATH, we must specify the current directory "." when running the executable. If all goes well you will see the five characters "Hello" printed on the console. If the compiler command or run command prints out error messages, take note of the error and line number and then correct your source file. Then you can try the process again.

If you don't specify the -o option when compiling your source code the resulting executable will be named a.out on Unix/Linux/Mac OS X. On Windows the executable will be named a.exe

The C++ compiler that comes with XCode on Mac OS X is accually the [[Clang C++ Compiler]] that is part of the LLVM development environment but it works the same as the Gnu C++ compiler.

String Output

While on a fundamental level all output is done one character at a time, it can be rather inconvenient to write code that outputs one character at a time. To make things easier, we can also output a sequence of characters known as a string. While a single character literal can be written inside single quotes, a sequence of characters or string is written inside a pair of double quotes. The following program performs the same output as the program above but uses a string literal instead.

ConsoleOutput2.cpp
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
	cout << "Hello, world!" << endl;
	return 0;
}
Output
$ g++ -std=c++17 ConsoleOutput2.cpp -o ConsoleOutput2 -lfmt $ ./ConsoleOutput2 Hello, world!

As mentioned before, output to wcout must be used for [[Unicode]] (wide) characters. The same is true for [[Unicode]] (wide) strings which must start with the modifier 'L'. Below is the previous example written using wide character strings. Remember not to mix [[ASCII]] (narrow)) and [[Unicode]] (wide) output streams. Also don't forget to include the setlocale and wcout.imbue code.

ConsoleOutput2u.cpp
#include <iostream>

using namespace std;

int main(int argc, char **argv) {
	setlocale(LC_ALL, "en_US.UTF-8"); 
#ifdef __APPLE__
	wcout.imbue(locale("en_US.UTF-8"));
#endif
	wcout << L"¡Hola Mundo!" << endl;
	return 0;
}
Output
$ g++ -std=c++17 ConsoleOutput2u.cpp -o ConsoleOutput2u -lfmt $ ./ConsoleOutput2u ¡Hola Mundo!
Common Escape Sequences
CharacterEscape
Sequence
'\'
"\"
\\\
tab\t
linefeed\n
carriage return\r
backspace\b
null\0
any char\x##
\u####
\U########

Special Characters

While we can represent most of the printable characters inside character or string literals using the character itself, there are some exceptions: single quote, double quote and the control characters. These exceptions can, however, be embedded into our literals by using a special escape notation. The escape notation consists of the backslash followed by another character. See the table to the right for the most common escape sequences.

It turns out that any character can be printed using an escape sequence if needed. The escape sequences \x##, \u#### or \U######## can be used to represent any character as long as you know the [[hexadecimal]] [[Unicode]] codepoint for that character. Just replace the #'s with the corresponding hexadecimal value (8-bit, 16-bit and 32-bit codepoints respectively) and you have the escape sequence for any character.

Questions

Projects

More ★'s indicate higher difficulty level.

Windows command line terminal does not support [[Unicode]] characters so some programs will not display Unicode characters correctly. If you redirect the program output to a file then open it in a Unicode aware text editor you should see the correct output. For example: C:> ./a.out > temp.txt But if you use [[Cygwin]], the default terminal program mintty supports Unicode so this is not necessary.

References