C and C++ are both languages with long histories and many, many users. With C being used for everything from the Linux kernel and device drivers to the code that powers the RFID chip that you use to swipe in the work every day, it is the undoubted lingua franca of the systems programming world. C is often used for single-threaded situations on systems with very limited memory and processing power, being as minimal as possible to allow for the most efficient use of the available resources. C++, on the other hand, is used in a different array of situations, ones that require fast performance, multithreading and native execution but also a fair share of abstractions, such as 3D game engines, management systems and high-demand web servers. The systems that C++ are used on often support more than one thread and have very powerful CPUs (such as servers) and so these systems can support the abstractions provided by the C++ standard library.

From above, we can see that C and C++ have very different historical and modern use cases. It is thus important, especially for the C programmer, to not treat C++ as “C with classes”. They are two distinct languages with distinct use cases. Referring to C++ as a language with only low-level, systems use cases in this day and age is erroneous, as well-written modern C++ code makes good use of abstractions provided by the standard library and very little unsafe, direct memory access. This series is intended to train the curious C programmer to master the intricacies of modern C++, but it will only work if he or she treats C++ as its own language with its own techniques for writing good code. For the remainder of the series, the fact that C++ code is mostly backwards-compatible with C code should be treated as nothing but a coincidence.

Without further ado, let’s proceed by comparing the “Hello World” programs in both C and C++.

/* C hello world program */

#include <stdio.h> int main(int argc, char *argv[]) {

printf("Hello, world!

");

return 0;

}

I have decided to use printf instead of puts for this implementation as it lets us explore the differences between C and C++ in a more detailed and interesting manner when we compare the two languages.

// C++ hello world program

#include <iostream> int main(int argc, char *argv[]) {

std::cout << "Hello, world!" << std::endl;

}

Pretty similar, huh? At first glance, the programs resemble each other largely. Both programs contain an include directive, both programs have a main function with the exact same format (with the exception of the C++ program lacking a return 0 statement, as it is automatically inserted as per the standard) and both programs have a statement which outputs the string Hello, world! to the screen (also known as standard output). However, the differences between C and C++ begin to emerge when one studies the actual line that makes the string print.

In the case of C, this line is printf("Hello, world!

"); . Let’s break this down. Opening up stdio.h shows us that the definition of printf looks something like this:

/* Definition of printf in stdio.h */

int printf(const char * __restrict fmt, ...);

printf is a variadic function which takes a pointer to a format string in the parameter fmt and uses the remaining parameters accordingly to insert any values that could be represented by placeholders in the format string, and writes the string to standard output. The return value is the number of bytes written. Our use of the function to print Hello, world! makes no use of format strings, as no values need to be printed with that. The only formatting performed is because of

, which adds a new line on the console after printing the string. Let’s look at some more complex uses of printf.

printf("%d

", 5); /* Prints the number "5" and a newline */ char *msg = "Vote for me!

";

printf("The mayor says for his re-election campaign: %s", msg); /* Prints a char array using a format string */

printf("Type a character: ");

int c = getc(stdin);

printf("The character you typed was: %c

", c); /* Prints a char read from standard input to the standard output using a format string */

From these examples we can see that printf is very much a function, which is called by the program and given parameters to work with. We will soon see that the case with C++ is not quite the same. Let’s open up iostream and see how std::cout is defined.

// Definition of cout in iostream

namespace std {

extern ostream cout; /// Linked to standard output

}

The first thing that’s apparent is that C++ code can be organised into namespaces. A namespace is simply a collection of names that is unique to a specific unit of code, be it a library, file or in C++ 17, a module. We use namespaces to prevent name conflicts with other units, meaning that unlike C, two different declarations can have the same name so long as they belong to different namespaces from each other. This saves us effort from having to think of a new name for everything and also helps us organise our code to improve readability. We see namespaces in action when we use cout in our program, as we prepend the name of the standard namespace, std , when using the member, thus making std::cout . The same syntax is used to access any member of any namespace.

The second thing that is apparent from the declaration of cout is that it is not a function. It is a variable of type ostream , which means output stream. In C++, any kind of I/O is performed by use of streams. Internally, streams are nothing but arrays of bytes that can be written to or read from. The output stream cout is a link to the computer’s standard output, which is usually the screen. The << operator, usually the bitshift operator, is overloaded (to be covered in a future article) by the ostream class to be a “write” operator. Thus, in the line std::cout << "Hello, world! << std::endl , we are not making a function call. Instead, we are writing the string Hello, world! to the computer’s standard output directly. Let’s translate our uses of printf above to C++:

std::cout << 5 << std::endl; // Prints the number "5" and a newline const char *msg = "Vote for me!

";

std::cout << "The mayor says for his re-election campaign: " << msg;

/* Prints a constant char array after printing the initial string */ int c;

std::cout << "Type a character: ";

std::cin >> c;

std::cout << "The character you typed was: " << c << std::endl;

We can see first of all that no format strings are required, the way to put the data on the screen is inferred from the data itself by the cout stream. Rather than the position of the data we want to print being contained in a format string, we use the << output operator as many time as we want to print values. Here we also see another stream defined by the standard library, cin , which we use to read from standard input. Notice that the >> operator is used instead, which is overloaded by the input stream class to be a “read” operator.

As stated before, all I/O in C++ is performed using streams, including file I/O (which will be covered in a later article). I have decided to leave a few exercises to the reader to reinforce what this article has stated.

Exercise 1: Mess around printing and reading various values from the standard input and output streams.

Exercise 2: Write a program that takes two characters, one by one, as input, converts them to integers, and displays their sum. As a bonus, also tell the user if the character they inputted is not a valid number.

Exercise 3: Write a program that reads a string inputted by the user and uses it in the sentence My name is <the string they wrote>, but you can call me Daddy. (you may need to research the std::string type).

Happy programming!