50 Fun Facts About C++
Learn something new, then test yourself with the quiz.
Know these facts? Prove it.
Take the 50-question quizC++'s creator joked that C makes it easy to shoot yourself in the foot, but C++ blows off what?
Your whole leg is the punchline: he said C++ makes the mistake harder to make but far worse when it happens. He confirms saying it around 1986, and adds that it is true to some extent of every powerful language.
Which Danish computer scientist began work in 1979 on the extension of C that grew into C++?
Bjarne Stroustrup wanted a language as efficient and flexible as C that also offered high-level features for organising large programs. Nearly two decades passed before it was first standardised, in 1998.
The '++' in the name C++ is which operator from C, signalling a step beyond the older language?
Increment is what ++ does in C: it adds one to a variable. As the language's creator likes to point out, the slightly shorter name "C+" is a syntax error.
At which research lab, also the birthplace of the transistor and Unix, was C++ first developed?
Bell Labs, then the research arm of AT&T, is also credited with the laser, information theory and the C language on which C++ was built.
Which game engine from Epic Games, the technology behind Fortnite, is written in C++?
Unreal Engine is written in C++, and Epic removed its home-grown UnrealScript language from version 4 so that game logic would be written in C++ as well.
Before it was renamed in 1983, what descriptive name did the language that became C++ go by?
C with Classes began as additions to a C compiler, among them derived classes, strong typing, inlining and default arguments. It was also briefly called C84 before the final name stuck.
Which punctuation mark, used the same way in C and C#, must end almost every statement in C++?
A semicolon is a terminator in the C family, required after every statement, whereas languages such as Python and Go can infer the end of a statement from the end of the line.
Which Sun Microsystems language kept a C++-style syntax to feel familiar, but dropped pointer arithmetic?
Java was given a syntax that C and C++ programmers would recognise, but with fewer low-level facilities than either: object addresses cannot be manipulated by adding or subtracting an offset.
Which two characters start a single-line comment in C++, a style revived from the older BCPL?
// comments run to the end of the line. BCPL had them, C left them out in favour of /* */ blocks, and C++ brought them back, with C itself following in 1999.
Under the C++ standard, which type must main return, its value going back to the system?
int is the only return type the standard permits for main. C++'s creator notes that void main() has never been valid C++, and that falling off the end of main returns 0, meaning success.
Which standard header, stripped of its .h when C++ was standardised, provides the console input and output streams?
<iostream> defines the standard stream objects for input, output, errors and logging. Its name is short for input/output stream, and until the 1998 standard it was written <iostream.h>.
Which stream object, used with the << operator, writes text to a C++ program's standard output?
std::cout is pronounced "see-out". The c stands for character, because the stream library converts values to and from their character representations.
Which directive, followed by a file name in angle brackets or quotes, pastes a header into C++ source?
#include is replaced by the text of the named file before compilation proper. Angle brackets search the system directories, while double quotes look beside the including file first.
Which operator tests two C++ values for equality, a notorious source of bugs when confused with assignment?
== compares, while a single equals sign assigns. Because an assignment has a value in C and C++, writing if (x = y) still compiles, which is what makes the slip so hard to spot.
Which C++ operator gives the remainder of an integer division, and refuses floating-point operands?
% is the remainder operator, so 7 % 3 is 1. Both of its operands must be integers; for floating-point values the library function std::fmod does the same job.
Which keyword, shared by C and C++, marks a variable as read-only so the compiler rejects later assignments?
const turns a promise into something the compiler enforces. Careful C++ style, known as const-correctness, is to declare every variable that way unless it truly needs to change.
Which C++ iteration statement always executes its body at least once, testing its condition afterwards?
A do-while loop is a post-test loop: the body runs first and the condition is checked only afterwards. The other forms test before the first pass, so their bodies may never run at all.
Which C++ keyword, the counterpart of 'next' in Perl and Ruby, skips the rest of the current loop iteration?
continue jumps straight to the start of the following pass, whereas its partner break abandons the loop altogether.
Which C++ operator requests memory on the heap, builds an object there and returns its address?
new signals failure with a std::bad_alloc exception, so its result needs no checking. Each use must later be matched by delete, which hands the memory back to the free store.
Which special C++ member function shares its class's name, has no return type and runs when an object is created?
A constructor has the task of initialising the object's data members. If a C++ class does not say how it should be copied, the compiler quietly generates a copying version of its own.
Which symbol, also C++'s bitwise NOT operator, is placed before a class's name to form its destructor?
A tilde marks the destructor, which runs automatically at the end of an object's lifetime, so a class named X does its cleaning up in ~X().
Which object-oriented feature lets a C++ class take on the members of an existing base class and add its own?
Inheritance lets programmers reuse existing code and extend it. C++ is among the languages that go further, allowing one class to derive from several parents at once.
Which access level do members of a C++ struct get by default, unlike those of a type declared with class?
public is the default for a struct and private for a class. Otherwise a C++ struct is simply a class: it can have member functions and base types of its own.
Which symbol after a type name declares a C++ reference, an alias that can never be made to refer elsewhere?
An ampersand after the type makes a reference, which must be bound to an object when it is declared and cannot be null. An asterisk in the same spot would declare a pointer instead.
Which C++ operator reaches a member through a pointer, as shorthand for a dereference followed by a dot?
-> folds two steps into one: p->x means exactly (*p).x. Class types such as smart pointers can define their own version, which lets them be used just like raw pointers.
What is the C++ operator :: called, as used in std::string to say which library a name belongs to?
Scope resolution is the official name for the double colon. In PHP the same symbol is also called Paamayim Nekudotayim, which is Hebrew for "double colon".
Which C++ feature groups identifiers under a prefix such as std, so two libraries can use the same one without clashing?
A namespace keeps names apart, and the entire standard library lives in the one called std. Before standardisation, many of its components were simply global.
Which C++ keyword, a pointer for historical reasons, refers to the object a member function was called on?
this is a pointer only because it entered the language before references did. Its designer has said that it would otherwise have been a reference.
Which keyword gave C++ a type-safe way to say 'points at nothing', replacing a bare 0 or an old C macro?
nullptr has a type of its own, so it converts to any pointer but not to an ordinary integer. The old macro was usually just 0, which could send a call to a function expecting a number.
Which C++ keyword, once an obscure storage specifier, now makes the compiler deduce a variable's type?
auto takes a variable's type from its initialiser, fixed at compile time. It spares programmers from spelling out long template type names by hand.
Which standard C++ container is a resizable array, storing its elements contiguously and growing as needed?
std::vector keeps its elements side by side in one block of memory, so they can be reached through ordinary pointers as well as by index, just like a built-in array.
Which C++ keyword launches an exception, and can do so with an object of any type, even a plain number?
throw transfers control to the nearest matching handler. By convention programmers use types derived from std::exception, but the language itself does not insist on it.
Which keyword makes C++ pick a derived class's version of a function at run time, even through a base pointer?
virtual makes a call depend on the actual type of the object, regardless of the declared type of the pointer or reference used to reach it. That is the basis of run-time polymorphism in C++.
Which term describes several C++ functions sharing one name but differing in their parameter lists?
Overloading lets the compiler pick a version from the number and types of the arguments in each call. In C++ a different return type alone is not enough to tell two versions apart.
Which term in the C++ standard covers reading past the end of an array, a result jokingly dubbed 'nasal demons'?
Undefined behavior means the compiler may do anything it chooses. The joke comes from a newsgroup post which said that could even include making demons fly out of your nose.
Which objects, modelled on pointers, do C++ library algorithms use to step through the elements of any container?
Iterators borrow the syntax of pointers, with * to read an element and ++ to move on. They are the feature that lets a single algorithm work across many different kinds of container.
What nickname do programmers give <=>, the three-way comparison operator that C++ adopted in its 2020 standard?
The spaceship is said to owe its name to the Perl programmer Randal Schwartz, whom <=> reminded of a ship in an old Star Trek computer game. One use reports whether a value is less than, equal to or greater than another.
Which Norwegian language of the 1960s, considered the first object-oriented one, inspired the design of C++?
Simula was built in Oslo for running simulations. The creator of C++ has said he invented his language because he wanted to write efficient systems programs in the styles Simula encouraged.
Linus Torvalds called C++ 'a horrible language' in 2007 while defending his choice of plain C for which tool?
Git is written in C, and its author told the project's mailing list that the choice would be worth it even if it did nothing but keep C++ programmers out.
Which C++ standard, nicknamed C++0x when it was due before 2010, added move semantics and range-based for loops?
C++11 arrived so late that the language's creator suggested reading the x in its old nickname as a hexadecimal digit, since B in hexadecimal is eleven.
Which C++ idiom, found while exceptions were being designed, ties a resource to the lifetime of an object?
RAII stands for resource acquisition is initialization. The creator of C++ has said he held exceptions back from the language for half a year until he had discovered the technique.
Which C++ conversion operator checks an object's real type at run time, yielding a null pointer on failure?
dynamic_cast is used for downcasting, from a base type to a more specific one. When a reference rather than a pointer is being converted, a failure produces a std::bad_cast exception instead.
Which C++20 feature lets a template spell out named requirements on its type arguments, checked at compile time?
Concepts were first suggested for the 2011 standard, and the specification was revised several times before it was finally standardised nine years later.
What is the term for a C++ compiler encoding a function's parameter types into the symbol that the linker sees?
Name mangling gives every C++ function a distinct symbol. Declaring a function extern "C" switches it off, which is what allows that function to be called from C code.
Which C++ design principle is summed up by the slogan 'you don't pay for what you don't use'?
Zero overhead has a second half that is quoted less often: what you do use should be just as efficient as what you could reasonably write by hand.
Which name did Scott Meyers give the C++ quirk where an object definition is read as a function declaration?
The most vexing parse follows from a C++ rule that anything which can be read as a declaration is one. Brace initialisation, added in 2011, offers a way round it.
What was the name of the original C++ compiler, which worked by translating C++ source code into C?
Cfront was itself written in C++, which made it a puzzle to install on a machine that had no C++ compiler yet. It was abandoned in 1993 after a failed attempt to add exceptions.
Which colleague of C++'s creator is credited with coining the name C++ in the summer of 1983?
Rick Mascitti's suggestion beat a runner-up, ++C, which connoisseurs of C semantics consider the better name. The winning name was first used that December.
Which Moscow-born programmer designed the Standard Template Library that C++ adopted, while at Hewlett-Packard?
Alexander Stepanov presented his library, built on generic programming, to the C++ standards committee in November 1993. Much of it was folded into the standard library.
Which C++ guideline says a class that defines its own destructor should also define both of its copy operations?
The rule of three covers the destructor and the two ways of copying an object. Once objects could also be moved rather than copied, it was broadened into the rule of five.