Home > Blogs > The C++ Features I Like Most Series: The ++ Operator

The C++ Features I Like Most Series: The ++ Operator

By  Dec 4, 2008

Topics: Programming, C/C++

The first feature in this series is the increment operator. In this context, "increment operator" is used as a generic term for four tightly-related operators: pre-increment, post-increment, pre-decrement and post-increment. Here's what's so special about this operator family.

When Brian Kernighan and Dennis Ritchie started to design C, they must have been familiar with a couple of academic programming languages that were supposed to be "pedagogically-correct". The most prominent example is Pascal. Pascal creators got many things right but they surely weren't designing the Multics kernel, as opposed to K&R (and Ken Thompson) who were after a real-world, hard-core programming language that gets the job done even if syntactically it doesn't win a beauty contest. Incrementing an integral value is such a frequently-used operation that I'm surprised that the designers of so many programming languages didn't come up with a special, concise and efficient operator that does exactly that. Instead, PL/1, Pascal and many other languages insisted on the cumbersome and clumsy form x=x+1; or in the case of Pascal, x:=x+1;

The ++ operator of C (which dates back to its earlier ancestors) was an immediate success not just because it was concise and widely used but for at least three other reasons:

Design Philosophy. In the early 1970s most CPUs already had a special machine instruction for incrementing an integer. Unlike the cumbersome form x=x+1; which can be translated in its non-optimized form to a sequence of 3-6 assembly directives, K&R actually invented a high-level C construct that compilers could map directly into the INC directive, thereby expressing the design philosophy of C -- a programming language that is close enough to the hardware but not too close.

Iconicity. In linguistics, the term "iconicity" refers to a lexical or grammatical aspect of a natural language that is self-evident from its form. Take pluralization for example. In certain languages, the plural is expressed by reduplicating the singular form. Thus, in Indonesian "person" is orang, whereas "people" is orang-orang. Even if you don't speak the language, you can usually guess that reduplicating orang means two or more orang. Similarly, even if you haven't used C before, you can easily guess what x++; means.

The iconicity is not confined to the ++ token itself. Its position is also iconic: ++x; is a pre-increment operation whereas x++; means post-increment.

Iterators Precursor. The increment operator also applies to pointers. Assuming that p is a pointer, p++; means something quite different from x++; (when x is int). The former advances the pointer value stored in p by one logical unit.Today we rarely get excited about this but back in the 1970s, this high level pointer increment stuff was as cool as the latest iPhone. Furthermore, by extending ++ to pointers, K&R opened the door to what would later become iterators -- an abstract pointer that you can use to traverse a sequence.

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.