<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Martin Licht</title>
    <description>The latest articles on DEV Community by Martin Licht (@martinlicht).</description>
    <link>https://dev.to/martinlicht</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2651802%2F038b8511-3a91-474b-8b45-159dae5db4de.png</url>
      <title>DEV Community: Martin Licht</title>
      <link>https://dev.to/martinlicht</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martinlicht"/>
    <language>en</language>
    <item>
      <title>Universal character names in C and C++</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:53:08 +0000</pubDate>
      <link>https://dev.to/martinlicht/universal-character-names-in-c-and-c-33c0</link>
      <guid>https://dev.to/martinlicht/universal-character-names-in-c-and-c-33c0</guid>
      <description>&lt;p&gt;Universal character names (UCNs) are a mechanism in C and C++ to designate characters from the ISO/IEC 10646 character set (Unicode).&lt;br&gt;
The C language is not restricted to the English alphabet, and non-ASCII characters can appear in source codes. For example, German umlauts or emojis may appear in character and string literals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'😂'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hätte 😂 Würde 😍 Könnte 😀"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They can even appear in identifiers (e.g., variable names) or in names of preprocessor macros.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// variable names&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="err"&gt;öß&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="err"&gt;ä&lt;/span&gt;&lt;span class="n"&gt;nge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="err"&gt;水&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="err"&gt;π&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14159&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// macro names&lt;/span&gt;
&lt;span class="cp"&gt;#define müll 42
#define 火 100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difficulty is portability: different platforms use different source encodings. Universal character names provide a portable way to encode non-ASCII characters.&lt;br&gt;
This blog post will explore the concept of universal character name (UCN) in the C programming language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;A universal character name encodes a Unicode code point using only ASCII characters. The character is identified by its code point in hexadecimal form.&lt;br&gt;
UCNs start with a single &lt;code&gt;\&lt;/code&gt; followed by either &lt;code&gt;u&lt;/code&gt; or &lt;code&gt;U&lt;/code&gt;, and a sequence of hexadecimal digits. Depending on whether the leading &lt;code&gt;u&lt;/code&gt; is capitalized or not, either four or eight hexademical digits must appear:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;uABCD&lt;/span&gt; &lt;span class="c1"&gt;// // 4 hex digits, code point up to U+FFFF&lt;/span&gt;
&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;U1A2B3C4D&lt;/span&gt; &lt;span class="c1"&gt;// // 8 hex digits, code point up to U+10FFFF&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The examples above can be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;'\&lt;/span&gt;&lt;span class="n"&gt;U0001F602&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 😂&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"H\u00E4tte \U0001F602 W\u00FCrde \U0001F60D K\u00F6nnte \U0001F600"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;u00F6&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;u00DFe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// größe&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;u00E4nge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// länge&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;u6C34&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// 水&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;u03C0&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14159&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// π&lt;/span&gt;

&lt;span class="cp"&gt;#define m\u00FCll 42      // müll
#define \u706B 100        // 火
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the leading &lt;code&gt;\&lt;/code&gt; must be single. A succession of &lt;code&gt;\&lt;/code&gt; will not be interpreted as a UCN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\u03C0"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// prints "π"&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;u03C0"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "\\u03C0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparision with escape sequences
&lt;/h2&gt;

&lt;p&gt;The syntax of universal character names look similar to escape sequences but their implementation is quite different.&lt;br&gt;
Universal character names are processed in an earlier translation phase than escape sequences, during the phase that maps source characters into the character set internally used by the compiler.&lt;br&gt;
By comparison, escape sequences (like &lt;code&gt;\n&lt;/code&gt; or &lt;code&gt;\x41&lt;/code&gt;) are processed when string or character literals are interpreted.&lt;br&gt;
Obviously, the syntax of UCN is inspired by escape sequences but the mechanism is different.&lt;/p&gt;

&lt;p&gt;Hexadecimal esacpe sequences serve a similar purpose but are less portable: hexadecimal escape sequences produce a character in the execution character set (the character set in which character and string literals are saved). The execution character set might be too small. Instead, UCNs produce a unambigous character according to UNICODE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison with trigraphs
&lt;/h2&gt;

&lt;p&gt;Universal character names are close in spirit to trigraphs, an ancient and now largely deprecated language feature of C and C++. Trigraphs were introduced to substitute for certain special characters such as &lt;code&gt;#&lt;/code&gt; when these were not available on the keyboard. These special characters were then replaced by a trigraph sequence.&lt;/p&gt;

&lt;p&gt;For example, the trigraph &lt;code&gt;??=&lt;/code&gt; acts like &lt;code&gt;#&lt;/code&gt; at any position in the source code.&lt;/p&gt;

&lt;p&gt;Trigraphs are obsolete because most keyboards now support the C basic character set. UCNs serve a similar purpose but will remain relevant much longer, since no keyboard can cover the full range of characters used in the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;The universal character names (UCNs) can appear in most places in the source code. They provide a portable way to include non-ASCII characters regardless of source file encoding. Whether the program compiles and executes as intended depends on how those characters are allowed in identifiers or how they are represented in the execution character set.&lt;/p&gt;

&lt;p&gt;For example, identifiers (such as variable names, functions, preprocessor tokens, ...) can include many non-English characters, such as German umlauts or Chinese hanzi, but they cannot include symbols such as emojis.&lt;/p&gt;

&lt;p&gt;How a non-ASCII character in a character or string literal is interpreted is a separate matter. For example, a character literal such as &lt;code&gt;'火'&lt;/code&gt;, which is equivalent to &lt;code&gt;'\u706B'&lt;/code&gt;, will probably not behave as expected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In C, an ordinary character literal has type &lt;code&gt;int&lt;/code&gt;. Writing &lt;code&gt;'\u706B'&lt;/code&gt; yields an &lt;code&gt;int&lt;/code&gt; value corresponding to U+706B. If this value is assigned to a &lt;code&gt;char&lt;/code&gt; variable, the result is implementation-defined. Some compilers issue a diagnostic warning, and other compilers truncate to the low byte (U+006B, 'k').&lt;/li&gt;
&lt;li&gt;In C++, an ordinary character literal has type &lt;code&gt;char&lt;/code&gt;. If the character cannot be represented by &lt;code&gt;char&lt;/code&gt;, the program is ill-formed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, both &lt;code&gt;u'火'&lt;/code&gt; and &lt;code&gt;u'\u706B'&lt;/code&gt; will produce the same UTF-16 character literal of type &lt;code&gt;char16_t&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restrictions
&lt;/h2&gt;

&lt;p&gt;However, there are some constraints: not all characters of UTF-32 may appear. The excluded characters are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code points within the surrogate range (D800  through DFFF) of UNICODE are disallowed.&lt;/li&gt;
&lt;li&gt;They cannot denote characters that are not valid Unicode scalar values. That means the numerical value cannot be greater than 10FFFF&lt;/li&gt;
&lt;li&gt;Outside of character and string literals, they cannot denote basic source characters. The 96 characters in the basic execution character set, such as ASCII letters, digits, and punctuation, must always be written literally.
Identifiers must still obey the general rules: cannot start with a digit, cannot clash with keywords, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Furthermore, they cannot appear in escape sequences inside character constants or strings where they would be ambiguous with existing escape rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applications
&lt;/h2&gt;

&lt;p&gt;There are numerous features and reasons why this language feature is useful.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Embedding non-ASCII text in character/string literals:&lt;/strong&gt; UCNs allow unambiguous portable inclusion of characters in string or character literals.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ensuring identifiers with non-ASCII names are valid C:&lt;/strong&gt; Variable names such as &lt;code&gt;größe&lt;/code&gt; can be written as &lt;code&gt;gr\u00F6\u00DFe&lt;/code&gt;, ensuring validity even if the source editor defaults to plain ASCII.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Precise specification of characters:&lt;/strong&gt; UCNs make explicit which Unicode scalar value is intended, avoiding confusion in visually similar glyphs (e.g., Greek alpha vs. Latin a).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Portability of source code across encodings:&lt;/strong&gt; source files may be saved in various encodings such as ASCII, UTF-8, or a legacy code page. UCNs are pure ASCII, so the compiler always interprets them the same way. For example, &lt;code&gt;\u00FC&lt;/code&gt; is always &lt;code&gt;ü&lt;/code&gt;, regardless of whether the file is opened in UTF-8, Latin-1, or Windows-1252.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Substitution by editors:&lt;/strong&gt; editors may offer reading and modifying the source code in any desired character set but then save any non-ASCII characters as UCNs. This preserves the convenience of editting a broader character set but ensures portability because the source code uses only ASCII characters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interoperability with generated code:&lt;/strong&gt; C code generators may output identifiers or strings containing international text. To guarantee correct compilation, they can emit UCNs instead of raw characters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unicode beyond the basic multilingual plane:&lt;/strong&gt; Characters like emojis or rare scripts (U+1F600 😀, U+20000 CJK ideographs) require \UXXXXXXXX. These cannot be represented with simple \xNN escapes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>coding</category>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Character types in C</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:52:13 +0000</pubDate>
      <link>https://dev.to/martinlicht/character-types-in-c-13l1</link>
      <guid>https://dev.to/martinlicht/character-types-in-c-13l1</guid>
      <description>&lt;p&gt;We want to continue the discussion of escape sequences. As a preparation, we review the different character types in C.&lt;/p&gt;

&lt;p&gt;The source code, with the possible exception of char and string literals, is written in the &lt;strong&gt;source character set&lt;/strong&gt;. This character set includes the basic character sets, which is necessary to write the source code. The basic character set is the alphanumeric characters, numerous white space characters, and the special characters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_ # { } [ ] ( ) = &amp;lt; &amp;gt; + - * / % ? : , ; . ^ &amp;amp; | ~ ! \
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;String and character literals may comprise additional characters beyond the basic source character set. These depend on the source file's encoding. It is implementation-defined how these are mapped to internal character representations.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;execution character set&lt;/strong&gt; is the set of characters used at runtime to represent values of type &lt;code&gt;char&lt;/code&gt; and related character types. Character and string literals are translated into this set during compilation in an implementation-defined manner. &lt;/p&gt;

&lt;h3&gt;
  
  
  Code units, code points, and glyphs
&lt;/h3&gt;

&lt;p&gt;Let us review the terminology, which is not necessarily consistent across areas.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;character&lt;/strong&gt; is a visible symbol such as a letter, a digit, or an emojis. Such printable characters are rendered in accordance to a font. Non-printable characters like tab or newline influence layout do generally not correspond to a distinct glyph but may affect the layout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code points&lt;/strong&gt; are the encoding of a character in terms of bytes. An &lt;strong&gt;encoding&lt;/strong&gt; is basically a list of code points, which specifies what byte sequences are supposed to represent what glyph.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;code point&lt;/strong&gt; is a numerical value assigned to a character in a character set. For example, the Unicode code point for 'A' is U+0041.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;code unit&lt;/strong&gt; is the smallest unit of data used by a character encoding. The size of the code unit depends on the respective encoding, it can practically vary from one byte to four bytes. Depending on the specific encoding, one or several code units constitute a single code point. The code units are the items of a string in C. &lt;/p&gt;

&lt;p&gt;A code unit is the smallest unit of data used by a character encoding. In encodings such as UTF-8, UTF-16, or UTF-32, a code point may be represented by one or more code units. In C, strings of type &lt;code&gt;char*&lt;/code&gt; consist of 8-bit code units; strings of type &lt;code&gt;wchar_t*&lt;/code&gt;, &lt;code&gt;char16_t*&lt;/code&gt;, or &lt;code&gt;char32_t*&lt;/code&gt; use wider code units.&lt;/p&gt;

&lt;p&gt;In many encodings, namely the fixed-length encodings, one code unit always represents one code point. One example is UTF-32. By contrast, in variable-length encodings, one or more code unit may represent a code point. Examples are UTF-8 and UTF-16. The number of code points in a variable-length encoding is not just the same as the number of code units. As you can imagine, string algorithms on fixed-length encodings are considerably simpler than on variable-length encodings.&lt;/p&gt;

&lt;p&gt;So the code units are the basic building blocks of the code points, which encode the actual characaters. We continue how this works out. &lt;/p&gt;

&lt;h3&gt;
  
  
  Fixed-length encodings
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;char&lt;/code&gt; holds a code unit of the &lt;strong&gt;narrow execution encoding&lt;/strong&gt;. The encoding and exact semantics are implementation-defined but often correspond to UTF-8 or some legacy encoding such as ISO-8859 or Windows-1252. The width of &lt;code&gt;char&lt;/code&gt; is at least 8 bits. Here, we see that the terminology is somewhat inconsistent: for such legacy encodings, the character, code points, and code units coincide, and character can be used interchangeably with the other two terms.&lt;/p&gt;

&lt;p&gt;The type &lt;code&gt;char&lt;/code&gt; has had many different purposes since the ancient days of C. Char can be signed or unsigned, depending on the implementation, but it is always a distinct type from &lt;code&gt;signed char&lt;/code&gt; and &lt;code&gt;unsigned char&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;wchar_t&lt;/code&gt; holds a code unit of the &lt;strong&gt;wide execution encoding&lt;/strong&gt;. Once again, its size and encoding are implementation-defined. On Windows, it is typically 16-bit and corresponds to UTF-16; on POSIX systems, it is usually 32-bit and corresponds to UTF-32. Such fixed-width representations simplify indexing but sacrifice compatibility and space efficiency.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;char32_t&lt;/code&gt; holds a single 32-bit code unit that directly encodes one &lt;strong&gt;Unicode&lt;/strong&gt; code point. Each code unit maps one-to-one with a code point, which is why this constitutes a fixed-length encoding for Unicode.&lt;/p&gt;

&lt;p&gt;The character and string literals for these types are marked with prefixes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no prefix &lt;/span&gt;
&lt;span class="kt"&gt;wchar_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;L"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// L indicates a wide character string &lt;/span&gt;
&lt;span class="kt"&gt;char32_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;U"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// U indicates a Unicode string &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generally speaking, we use &lt;code&gt;char&lt;/code&gt; for raw byte data or single-byte text encodings (such as ASCII, ISO-8859). The type &lt;code&gt;char32_t&lt;/code&gt;, introduced in C11, can represent a much larger scope of characters, at the expense of quadrupled memory usage. The wide character type &lt;code&gt;wchar_t&lt;/code&gt; strikes a balance between memory usage and flexibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable-length encodings
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;char8_t&lt;/code&gt; holds a code unit of the UTF-8 encoding of size one byte. UTF-8 is a variable-length encoding where code points are represented by one to four 8-bit code units. The standard ASCII characters occupy one single byte, but higher code points require multiple bytes.&lt;/p&gt;

&lt;p&gt;Similarly, a &lt;code&gt;char16_t&lt;/code&gt; holds a code unit of the UTF-16 encoding, where each code unit is composed of 16 bits. UTF-16 is a variable-length encoding where code points can be stored as multiple bytes. Characters in the &lt;strong&gt;Basic Multilingual Plane&lt;/strong&gt; (BMP) are stored as one 16-bit code unit, while supplementary characters (above U+FFFF) use a pair of 16-bit code units called &lt;strong&gt;surrogate pairs&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;char8_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;u8"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// u8 indicates UTF-8&lt;/span&gt;
&lt;span class="kt"&gt;char16_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;u"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// u indicates UTF-16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type &lt;code&gt;char8_t&lt;/code&gt; was introduced in C23, whereas &lt;code&gt;char16_t&lt;/code&gt; was introduced in C11.&lt;br&gt;
Notably, &lt;code&gt;char8_t&lt;/code&gt; is backward-compatible with ASCII for code points U+0000–U+007F.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Encoding&lt;/th&gt;
&lt;th&gt;Storage Type&lt;/th&gt;
&lt;th&gt;Width (bits)&lt;/th&gt;
&lt;th&gt;Variable-length?&lt;/th&gt;
&lt;th&gt;Literal Prefix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Narrow exec&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≥8&lt;/td&gt;
&lt;td&gt;impl-defined&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"..."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wide exec&lt;/td&gt;
&lt;td&gt;&lt;code&gt;wchar_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;impl-defined&lt;/td&gt;
&lt;td&gt;impl-defined&lt;/td&gt;
&lt;td&gt;&lt;code&gt;L"..."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char8_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u8"..."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-16&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char16_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;Yes (surrogates)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u"..."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-32&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char32_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;&lt;code&gt;U"..."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>beginners</category>
      <category>c</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Escape sequences in C</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:51:24 +0000</pubDate>
      <link>https://dev.to/martinlicht/escape-sequences-in-c-1kf3</link>
      <guid>https://dev.to/martinlicht/escape-sequences-in-c-1kf3</guid>
      <description>&lt;p&gt;It is easy to write a string that contains alphanumeric characters. Non-alphanumeric characters must be encoded with an escape sequence. Let us review the details of escape sequences in the C standard.&lt;/p&gt;

&lt;p&gt;Escape sequences are special character combinations within your C source code that appear within a string or when specifying a &lt;code&gt;char&lt;/code&gt; value. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Escape sequences begin with a backslash (&lt;code&gt;\&lt;/code&gt;) followed by one or more characters that specify the escape sequence. An escape sequence consists of more than one character in your source code, but each escape sequence is converted into a single character during compilation (assuming characters are encoded in a single byte). If the escape sequence is not recognized, then the compiler issues an error.&lt;/p&gt;

&lt;p&gt;Let us review the escape sequences, categorized into a few groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Special Characters
&lt;/h2&gt;

&lt;p&gt;To use the characters &lt;code&gt;'&lt;/code&gt;, &lt;code&gt;"&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, and &lt;code&gt;\&lt;/code&gt;, we must use the corresponding escape sequences &lt;code&gt;\'&lt;/code&gt;, &lt;code&gt;\"&lt;/code&gt;, &lt;code&gt;\?&lt;/code&gt;, and &lt;code&gt;\\&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Escape Sequence&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\\&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;backslash&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\?&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;question mark&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;single quotation mark&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;double quotation mark&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Evidently, we need an escape sequence for the backslash character since the backslash is already part of any escape sequence.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello\? Is &lt;/span&gt;&lt;span class="se"&gt;\"\\\\\\\"&lt;/span&gt;&lt;span class="s"&gt; a string of three backslashes in &lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s"&gt;C&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s"&gt;\?"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A Note on Line Continuation
&lt;/h2&gt;

&lt;p&gt;The backslash character is also used to continue a string across lines. Specifically, any backslash that is &lt;em&gt;immediately&lt;/em&gt; followed by a newline character in the source code is deleted. Effectively, this is used to connect source lines into one logical line. This happens before escape sequences are processed by the compiler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"This string ends with &lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;; // ends with a backslash&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Non-printable Characters
&lt;/h2&gt;

&lt;p&gt;Many of these character sequences come from a time when terminals had limited display capabilities:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Escape Sequence&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;a&lt;/strong&gt;lert&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;b&lt;/strong&gt;ackspace&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\f&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;f&lt;/strong&gt;orm feed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;n&lt;/strong&gt;ewline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;carriage &lt;strong&gt;r&lt;/strong&gt;eturn&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;horizontal &lt;strong&gt;t&lt;/strong&gt;ab&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\v&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;v&lt;/strong&gt;ertical tab&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let us summarize them with simple examples:&lt;/p&gt;

&lt;p&gt;The escape sequence &lt;code&gt;'\a'&lt;/code&gt; is supposed to produce an alert beep. This feature is less common on modern machines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This may trigger an alert beep!&lt;/span&gt;&lt;span class="se"&gt;\a\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using &lt;code&gt;'\t'&lt;/code&gt; (horizontal tab):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Column1&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;Column2&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;Column3&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using &lt;code&gt;'\b'&lt;/code&gt; (backspace):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"12345&lt;/span&gt;&lt;span class="se"&gt;\b\b&lt;/span&gt;&lt;span class="s"&gt;67&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 12367&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using &lt;code&gt;'\r'&lt;/code&gt; (carriage return):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!&lt;/span&gt;&lt;span class="se"&gt;\r&lt;/span&gt;&lt;span class="s"&gt;Bye!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: Bye!o, world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using &lt;code&gt;'\v'&lt;/code&gt; (vertical tab):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Line1&lt;/span&gt;&lt;span class="se"&gt;\v&lt;/span&gt;&lt;span class="s"&gt;Line2&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using &lt;code&gt;'\f'&lt;/code&gt; (form feed, rarely used today):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First page&lt;/span&gt;&lt;span class="se"&gt;\f&lt;/span&gt;&lt;span class="s"&gt;Second page&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Numerical escape sequences
&lt;/h2&gt;

&lt;p&gt;Escape sequences in string and character literals allow us to define characters directly using their numerical code value. This is done either in octal or hexadecimal form.&lt;/p&gt;

&lt;p&gt;An escape sequence in octal form consists of &lt;code&gt;\&lt;/code&gt; followed by one, two, or three octal digits (0-7). The integer value determines the character that is put into that position. Notice that at most three octal digits are read, any digits after that (and any non-octal digit) is not part of the escape sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\101\102\103\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints ABZ&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\170&lt;/span&gt;&lt;span class="s"&gt;11&lt;/span&gt;&lt;span class="se"&gt;\171&lt;/span&gt;&lt;span class="s"&gt;11&lt;/span&gt;&lt;span class="se"&gt;\172&lt;/span&gt;&lt;span class="s"&gt;11&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints x11y11z11&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\77&lt;/span&gt;&lt;span class="s"&gt;89"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints A89  &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most importantly, this is the standard way to manually insert the null character manually into a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hallo&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;Welt!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints Hallo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, hexadecimal escape sequences begin with &lt;code&gt;\x&lt;/code&gt; followed by one or more hexadecimal digits (&lt;code&gt;0&lt;/code&gt;-&lt;code&gt;9&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;-&lt;code&gt;f&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt;-&lt;code&gt;F&lt;/code&gt;). Any number of digits may follow &lt;code&gt;\x&lt;/code&gt;, but whitespace or non-hex characters terminate the sequence. Unlike octal escape sequences, hexadral escape sequences can have an arbitrary number of digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x41\x42\x43\n\x41&lt;/span&gt;&lt;span class="s"&gt;G&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints: ABC followed by AG&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\xAa\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Mixed uppercase/lowercase is possible&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In practice, hexahedral digits for one 8-bit characters are sufficient for any English text. More hexahedral digits are meaningful if the string character type uses more than one byte.&lt;/p&gt;

&lt;p&gt;A technical detail is the fact that numerical escape sequences in char constants are always interpreted as unsigned chars. If the numerical value of the escape sequence does not fit into the character type, then an overflow occurs. The compiler may also emit a warning. The result of that overflow is used to determine the character of the escape sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\777'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 511 mod 256 = 255, value stored is 255&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints: 255&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Technically, numerical escape sequences within a string are parsed as an &lt;code&gt;int&lt;/code&gt;, and the result is cast into a character. The translation of escape sequences and overflow management depends on the implementation and the type of characters in the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x4142\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0x4142 = 16706. The result depends on the implementation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Different string types and glyph encodings shall be a topic for another blog post.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>c</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Not-a-Number in C Floating-Point Arithmetic</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:49:54 +0000</pubDate>
      <link>https://dev.to/martinlicht/not-a-number-in-c-floating-point-arithmetic-o08</link>
      <guid>https://dev.to/martinlicht/not-a-number-in-c-floating-point-arithmetic-o08</guid>
      <description>&lt;p&gt;The C programming language adopts the IEEE 754 floating-point arithmetic. This includes the representation of non-finite floating-point values: on the one hand, there are positive and negative infinity. On the other hand, there is NaN (Not a Number), signifying invalid arithmetical operations.&lt;/p&gt;

&lt;p&gt;This post discusses the NaN values in more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arithmetic and comparisons in C
&lt;/h2&gt;

&lt;p&gt;To understand the purpose of the NaN floating-point values, we first study their arithmetic behavior. In particular, we see a few examples where NaN is created.&lt;/p&gt;

&lt;p&gt;NaN values appear as the result of operations that do not have a valid value, neither a finite value nor an infinity. Some examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;±0 / ±0 = NaN&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;∞ + (−∞) = NaN&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;±∞ / ±∞ = NaN&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;sqrt( -1.0 ) = NaN&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any ordinary arithmetic operation in C will yield a NaN result if any of the operands is NaN. That way, once a NaN appears in the computations, it will propagate throughout the computations.&lt;/p&gt;

&lt;p&gt;C comparison operators adhere to the following rules if a NaN appears:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Any ordered comparison (&lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt;) involving NaN is false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Equality (&lt;code&gt;==&lt;/code&gt;) with NaN is false, even NaN == NaN is false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inequality (&lt;code&gt;!=&lt;/code&gt;) with NaN is true.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In that sense, NaN is the only value that is not equal to itself. The preferred way of detecting NaN in a C code is the function &lt;code&gt;isnan()&lt;/code&gt;, included from &lt;code&gt;&amp;lt;math.h&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quiet and signaling NaNs
&lt;/h2&gt;

&lt;p&gt;NaNs have two subtypes according to IEEE:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Quiet NaN (qNaN)&lt;/strong&gt;: Propagates through most arithmetic without triggering an exception. Used to represent missing or undefined results (e.g., &lt;code&gt;0/0&lt;/code&gt;, &lt;code&gt;sqrt(−1)&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Signaling NaN (sNaN)&lt;/strong&gt;: Intended to raise an invalid-operation exception on first use. Used for uninitialized data or data explicitly marked invalid. In most C environments, hardware exceptions are masked by default, so sNaNs are often immediately converted to qNaNs on use.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If exceptions are unmasked, then sNaNs trigger an invalid-operation exception before becoming qNaNs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bit representations
&lt;/h2&gt;

&lt;p&gt;In binary IEEE 754 formats, a floating-point number has three fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sign bit&lt;/strong&gt;: 0 for positive, 1 for negative.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Exponent field&lt;/strong&gt;: All bits set to 1 (maximum exponent value) marks infinities and NaNs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fraction (mantissa) field&lt;/strong&gt;: Distinguishes between infinities and NaNs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NaNs are implemented as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Exponent: all ones&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fraction: nonzero&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distinction between quiet and signaling NaN is done via the leading bit of the fraction part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;qNaN: leading mantissa bit = 1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sNaN: leading mantissa bit = 0&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The remaining bits of the fractional part, and the sign bit, generally have no further meaning. However, they may carry additional diagnostic information, the specifics of which are implementation-defined.&lt;/p&gt;

&lt;p&gt;Example in single precision:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;qNaN example: 0 11111111 10000000000000000000000  (0x7FC00000)
sNaN example: 0 11111111 01000000000000000000000  (0x7FA00000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Purposes and applications
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NaN&lt;/strong&gt; encodes undefined results in floating-point computations. Upon an invalid operation, a NaN is introduced and propagates through subsequent arithmetic operations. This concept is deemed preferable to just stopping the computations since it allows to catch the data at a later point in time.&lt;/p&gt;

&lt;p&gt;The difference between signaling and quiet NaNs is mostly historical.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;sNaNs&lt;/strong&gt; are intended to cause an &lt;em&gt;invalid operation&lt;/em&gt; exception on first use, so the program can catch the event immediately. This is useful in debugging numerical code, detecting uninitialized values, or halting on unexpected invalid input before further contamination of results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;qNaNs&lt;/strong&gt; propagate silently through computations, carrying payload bits until a result is examined. This avoids interrupting long computations where invalid intermediate values are tolerated or expected.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distinction is less visible in modern C user code. In practical C on general-purpose OSes, the NaN subtype distinction mostly survives as an unused bit pattern convention. Practically,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On most mainstream platforms (x86, ARM) the default floating-point exception mask prevents trapping, so sNaNs automatically convert to qNaNs before most user code can react.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Libraries and languages that require uninterrupted computation almost always generate qNaNs directly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The sNaN mechanism is still in hardware for standards compliance and for specialized domains (high-reliability, safety-critical systems, debugging numerical kernels), but typical applications never see them unless explicitly constructed and tested with exceptions unmasked.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Infinity in C Floating-Point Arithmetic</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:47:18 +0000</pubDate>
      <link>https://dev.to/martinlicht/infinity-in-c-floating-point-arithmetic-2lp2</link>
      <guid>https://dev.to/martinlicht/infinity-in-c-floating-point-arithmetic-2lp2</guid>
      <description>&lt;p&gt;The C programming language adopts the IEEE 754 floating-point arithmetic. This includes the representation of non-finite floating-point values: on the one hand, there are positive and negative infinity. On the other hand, there is NaN (Not a Number), signifying invalid arithmetical operations. &lt;/p&gt;

&lt;p&gt;This post discusses the infinities in more detail. There are two different infinities in floating-point arithmetic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Positive infinity&lt;/strong&gt; (+∞) represents a value larger than any finite value of the floating-point type. It results from adding and multiplying two very large numbers, or from the division of a finite positive value by zero. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Negative infinity&lt;/strong&gt;  (−∞) is very similar but negative. For example, it results from the division of a negative finite value by zero. &lt;/p&gt;

&lt;p&gt;Notice that technically, each floating-point type (&lt;code&gt;float&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt;, &lt;code&gt;long double&lt;/code&gt;) has its own version of positive and negative infinity. However, casting values between floating-point numbers preserves +/- infinity. &lt;/p&gt;

&lt;h2&gt;
  
  
  Arithmetic and comparisons with infinities
&lt;/h2&gt;

&lt;p&gt;To understand the purpose of these floating-point types, let us first study their arithmetic behavior. Via IEEE 754, the following behaviors are required. &lt;/p&gt;

&lt;p&gt;The infinities behave completely intuitively in the basic arithmetic operations. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;finite ± ±∞ = ±∞&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;finite × ±∞ = ±∞&lt;/code&gt; unless the finite value is ±0&lt;/li&gt;
&lt;li&gt;&lt;code&gt;±∞ × ±∞ = ±∞&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;finite / ∞ = ±0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some arithmetic operations involving infinities do not have an intuitive value (at least, there is no general consensus what these operations should have). In that case, the result will be NaN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;∞ + (−∞) = NaN&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;±0 / ±∞ = NaN&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;±∞ / ±0 = NaN&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;±∞ / ±∞ = NaN&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The comparisons involving infinities behave expected. +∞ is greater than all finite numbers, and −∞ is less than all finite numbers. Naturally, +∞ is greater than −∞. And as one would expect, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;+∞ == +∞&lt;/li&gt;
&lt;li&gt;−∞ == −∞&lt;/li&gt;
&lt;li&gt;+∞ != −∞&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bit Representations of infinities
&lt;/h2&gt;

&lt;p&gt;Let us recapitulate how infinities are represented in the floating-point standard. Floating-point numbers in any binary IEEE 754 format consist of the three fields of bits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sign bit&lt;/strong&gt;, where &lt;code&gt;0&lt;/code&gt; designates positive and &lt;code&gt;1&lt;/code&gt; designates negative.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponent field&lt;/strong&gt;, which encodes the exponent of the floating-number&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mantissa field&lt;/strong&gt;, which contains the (non-leading) bit representation of the mantissa&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The non-finite floating values are encoded by special values of the exponent: if all exponent bits are set to 1, then the floating-point number is non-finite.&lt;/p&gt;

&lt;p&gt;The different types of non-finite values are then distinguished by the bit values in the mantissa and the the sign bit. Infinities are encoded by the setting all exponent bits to &lt;code&gt;1&lt;/code&gt; and all mantissa bits to to &lt;code&gt;0&lt;/code&gt;. The sign bit distinguishes between positive (&lt;code&gt;0&lt;/code&gt;) and negative infinities (&lt;code&gt;1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Effectively, the IEEE 754 format sacrifices one order of magnitude in each floating-type to represent the non-finite values. That is not a huge sacrifice: the uttermost order of magnitude of a floating-point value is rarely reached, and if computations ever operate within that order of magnitude, then one should consider switching to a larger floating-point type (or arbitrary precision packages) to avoid undesired overflows. &lt;/p&gt;

&lt;p&gt;With 32-bit precision, when the sign bit is followed by the exponent field and then the mantissa, the bit patterns for the infinities look as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+∞: 0 11111111 00000000000000000000000
−∞: 1 11111111 00000000000000000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Purpose of infinities
&lt;/h2&gt;

&lt;p&gt;The implementation of infinities requires some effort in chip design. &lt;/p&gt;

&lt;p&gt;Typically, operations involving infinities are assumed to be rare and hence the floating-point circuitry is not optimized for such values. If your numerical computations suddenly slow down, it might that a non-finite floating-point value has occurred and propagated through the calculations. &lt;/p&gt;

&lt;p&gt;So what is a justification to include non-finite values such as infinity in the floating-point standard? Here are some reasons&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Overflow Behavior:&lt;/strong&gt; we would like to handle the case where the result of an operation is not representable within the floating-point type. Including infinities seems to be a smoother choice than (sign changing) wrap around and triggering hardware exceptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Division by zero:&lt;/strong&gt; rather than, say, triggering undefined behavior or a hardware exception, we can represent the result of such a division as a regular arithmetic value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Representation of asymptotics:&lt;/strong&gt; infinities can encode the limit behavior of numerical operations. For example, &lt;code&gt;tan(π/2) = ∞&lt;/code&gt; or &lt;code&gt;1.0/∞ = 0.0&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Order preservation:&lt;/strong&gt; the infinities +∞ and −∞ have meaningful order relationship with the finite values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exception signaling:&lt;/strong&gt; it avoids exceptions on operations that meaningfully produce infinities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuation of computations:&lt;/strong&gt; This is perhaps the most important feature: instead of triggering an exception, the control flow proceeds reliably. &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Floating-Point Formats For You!</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:46:31 +0000</pubDate>
      <link>https://dev.to/martinlicht/floating-point-formats-for-you-1ek1</link>
      <guid>https://dev.to/martinlicht/floating-point-formats-for-you-1ek1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Most programming languages have at least one data type that represents &lt;strong&gt;integral numbers&lt;/strong&gt; within a specific range. For example, Python has a built-in data type &lt;code&gt;int&lt;/code&gt; that can hold the integers &lt;code&gt;-2147483648&lt;/code&gt; through &lt;code&gt;2147483647&lt;/code&gt;, corresponding to 4 bytes. The C programming language has several integer types for signed and unsigned integers. For example, the &lt;code&gt;unsigned int&lt;/code&gt; type can hold integers from &lt;code&gt;0&lt;/code&gt; to at least &lt;code&gt;4294967295&lt;/code&gt; (corresponding to at least 4 bytes), and the &lt;code&gt;signed short&lt;/code&gt; type holds at least the integers &lt;code&gt;-32768&lt;/code&gt; through &lt;code&gt;32767&lt;/code&gt; (corresponding to at least 2 bytes).&lt;/p&gt;

&lt;p&gt;Closer to the machine level, calculating with integers is among the fundamental capabilities of any CPU. Any CPUS's instruction set includes commands to perform basic arithmetic with integers, offering different variants depending on the size and signedness of the integers. For example, the x86 architecture includes instructions to multiply two signed 4-byte integers or add two unsigned 2-byte integers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, not all numerical values are integers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Science and engineering handle real numbers that are not integers. One famous example of such a number is the circle number pi, whose first few digits are &lt;code&gt;3.141592653589793...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;How do we represent, say, irrational numbers such as pi or fractional numbers such as &lt;code&gt;0.0000456&lt;/code&gt; or &lt;code&gt;12345.6789&lt;/code&gt; on a computer? Different ideas have been attempted since the invention of digital computers.&lt;br&gt;
Whilst number representations such as fixed point or binary-coded decimals (BCD) have been in use in earlier times,&lt;br&gt;
these have been largely supplanted by floating-point number formats over the last fifty years.&lt;br&gt;
The industry has adopted &lt;strong&gt;floating-point numbers&lt;/strong&gt; as standard practice in hardware and software,&lt;br&gt;
especially to accommodate the computational needs of scientists and engineers.&lt;br&gt;
On the one hand, floating-point numbers are supported as data types in most programming languages.&lt;br&gt;
On the other hand, most modern CPUs support instructions with numbers encoded in floating-point formats.&lt;br&gt;
These calculations are typically handled by the processor's &lt;strong&gt;floating-point unit&lt;/strong&gt; (FPU).&lt;/p&gt;
&lt;h2&gt;
  
  
  From scientific notation to floating-point numbers
&lt;/h2&gt;

&lt;p&gt;Most real numbers cannot be represented on computers since computers can only store a finite number of bits. In fact, one can mathematically prove that there are real numbers whose digit sequences cannot be generated by any computer program. At best, the vast majority of numbers in our calculations can only be approximated on a computer. The &lt;strong&gt;floating-point number representation&lt;/strong&gt; stands out among the many possibilities for representing (or approximating) non-integer numbers. Its origins predate the advent of digital computers: it is directly inspired by the &lt;strong&gt;scientific notation&lt;/strong&gt; in science and engineering.&lt;/p&gt;

&lt;p&gt;Given the central role of &lt;strong&gt;scientific notation&lt;/strong&gt;, the floating-point number representation should be primarily understood as a number format for computational methods in science and engineering. Other number representations (e.g., fixed-point formats) may be more suitable for different domains, such as computational finance or machine learning.&lt;/p&gt;

&lt;p&gt;Let us illustrate the scientific notation with an example from physics. The fine structure constant, which is a fundamental constant of particle physics, has a measured approximate value of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f = 0.0072973525 = 7.2973525 x 10^(-3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scientific notation rescales numbers by factoring out some power of ten (positive or negative), leaving a decimal number with only one leading digit. We call that decimal number the &lt;strong&gt;mantissa&lt;/strong&gt; or &lt;strong&gt;significand&lt;/strong&gt;. The power of ten determines the &lt;strong&gt;exponent&lt;/strong&gt;. In this example, &lt;code&gt;7.2973525&lt;/code&gt; is the &lt;strong&gt;mantissa&lt;/strong&gt; and &lt;code&gt;-3&lt;/code&gt; is the &lt;strong&gt;exponent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The main advantages of the scientific notation are two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can quickly determine the order of magnitude for this number. Scientific calculations can involve numbers across several orders of magnitude.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can easily control the precision. When operating within a particular order of magnitude, we generally only need to know the scientific value up to a certain precision, that is, a relative error. The more fractional digits we keep in our mantissa, the more precision we maintain. Practical calculations discard excess precision and only maintain a certain number of fractional digits in the mantissa.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, the numerical value of the Avogrado constant in physics and chemistry is roughly equal to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = 6.0221 x 10^(23)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With four fractional digits in the mantissa, we read that the relative error between the written value and the exact value is at most &lt;code&gt;10^(-4)&lt;/code&gt;, meaning that the written value lies within the percentage of a percentage of the actual value (which is &lt;code&gt;6.02214076 x 10^(23)&lt;/code&gt;). Increasing the length of the mantissa reduces the relative errors of our number representation, provided our measured data warrant that accuracy. Apparently, a trade-off between precision and practical effort needs to be balanced by CPU manufacturers and domain experts.&lt;/p&gt;

&lt;p&gt;In summary, up to a certain precision determined by the length of the mantissa, the scientific notation rewrites any real number by splitting it into a mantissa and some power of ten. More concisely, we can use the &lt;strong&gt;exponential notation&lt;/strong&gt; or &lt;strong&gt;E notation&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f = 7.2973525e-3
A = 6.0221e23
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scientific notation has evolved over centuries in science and engineering. It represents numbers in compact form and enables scientists to balance between computational effort and practically relevant precision. We can represent numbers over numerous orders of magnitudes, from the very large to the very small. With that in mind, the scientific notation is the natural candidate for implementing fractional calculations on a computer.&lt;/p&gt;

&lt;p&gt;This has led to the name &lt;em&gt;floating-point number&lt;/em&gt;: given any number, we can choose a suitable exponent such that the decimal point is shifted right behind the leading digit. The term &lt;em&gt;floating-point&lt;/em&gt; reflects how the decimal point "floats" to the desired position after the leading digit, always dynamically adjusted with the exponent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary floating-point numbers
&lt;/h2&gt;

&lt;p&gt;The scientific notation is not restricted to the decimal system: any radix system supports the scientific notation. On computers, it seems most natural to work with the binary system. For example, the fine-structure constant and the Avogrado constant above read in binary roughly like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f = 1.110111100001111010100001001010101101110101111 x 2^(-8)
A = 1.111111100001 x 2^(78)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our floating-point formats are conceptually based on this &lt;strong&gt;binary scientific notation&lt;/strong&gt;. The key idea is this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Floating-point numbers implement scientific notation in binary on computers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are a few things that we keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Any floating-point format will have &lt;code&gt;M&lt;/code&gt; bits to represent a binary mantissa and &lt;code&gt;N&lt;/code&gt; to represent the binary exponent. The number of bits &lt;code&gt;M&lt;/code&gt; and &lt;code&gt;N&lt;/code&gt; are fixed for each particular floating-point format. Effectively, there are only finitely many possible mantissae, and the exponent will be constrained to a finite number of possible values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;leading non-fractional digit&lt;/strong&gt; of any binary mantissa is always &lt;code&gt;1&lt;/code&gt;. In order to save memory, many floating-point formats store only the fractional digits of the mantissa explicitly in memory. This is known as &lt;strong&gt;hidden bit convention&lt;/strong&gt;, as we will discuss soon.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An issue we have swept under the rug so far is how to represent the number zero. This is not trivial. An engineer doing computations by hand would write down zero &lt;code&gt;0&lt;/code&gt; or something like &lt;code&gt;0.00000 x 10^0&lt;/code&gt;. However, we cannot represent the zero in a binary floating-point format that adheres to the hidden bit convention, which always implicitly assumes a leading digit &lt;code&gt;1&lt;/code&gt;. If we were to explicitly save the first non-fractional digit, then this would cost us an additional bit that virtually never carries any interesting information. Furthermore, we would have numerous representations of zero sharing the same exponent: &lt;code&gt;0.000 x 2^3&lt;/code&gt;,  &lt;code&gt;0.000 x 2^0&lt;/code&gt;, or  &lt;code&gt;0.000 x 2^(-7)&lt;/code&gt;. In summary, storing the explicit leading digit comes with many disadvantages. Instead, the mainstream formats reserve specific bit patterns for exceptional cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The base used in the scientific notation and floating-point formats is also called the &lt;strong&gt;radix&lt;/strong&gt;. We focus on binary floating-point numbers with radix &lt;code&gt;2&lt;/code&gt;. Of course, we could theoretically use any radix: the radix 10 representation is commonly used when humans calculate by hand. CPUs natively supporting floating-point calculations in some decimal encoding have appeared throughout the years.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Binary floating-point numbers, more details
&lt;/h2&gt;

&lt;p&gt;IEEE 754 is a technical standard that defines several floating-point number formats. The most important of these formats are natively supported by CPU instruction sets across different hardware vendors. More specifically, the floating-point number formats represent numbers as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; bit for the sign, the &lt;strong&gt;sign bit&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;M&lt;/code&gt; bits for the mantissa, the &lt;strong&gt;mantissa bits&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt; bits for the exponent, the &lt;strong&gt;exponent bits&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each format is characterized by how many bytes any floating-point number occupies in memory and how many bits are allocated for the mantissa and the exponent. Each such floating-point number format represents a trade-off between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the exponent range, which determines the possible orders of magnitudes&lt;/li&gt;
&lt;li&gt;the mantissa size, which governs the precision&lt;/li&gt;
&lt;li&gt;the total size, which impacts processing time and memory footprint&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some key clarifications are in order here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Most formats use an &lt;strong&gt;implicit leading bit&lt;/strong&gt;. Then the &lt;code&gt;M&lt;/code&gt; bits of the mantissa only represent the fractional part of the mantissa (in binary expansion), while the leading digit &lt;code&gt;1&lt;/code&gt; is not stored explicitly. This is also known as the &lt;strong&gt;hidden-bit convention&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The exponent is not stored as an &lt;code&gt;N&lt;/code&gt;-bit signed integer. This might be counterintuitive. Instead, we subtract a &lt;strong&gt;bias&lt;/strong&gt; from the exponent and store the result, which is non-negative, as an &lt;code&gt;N&lt;/code&gt;-bit unsigned integer. The bias is typically &lt;code&gt;2^(N-1)-1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The sign bit is stored at the very beginning. Next come the exponent bits, which are stored &lt;strong&gt;before&lt;/strong&gt; the mantissa bits. This has some advantages in particular situations. For example, with that convention, we can sort floating-point numbers as if they were signed integers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the exponent bits are set to either the largest or the smallest possible values of the exponent range, alternative interpretations are applied to the entire bit sequence. The exponent bits store an unsigned integer from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;2^N-1&lt;/code&gt;. Subject to the typical exponent bias, the nominal exponent range is from &lt;code&gt;-2^(N-1)+1&lt;/code&gt; up to &lt;code&gt;2^N-1 - 2^(N-1)+1 = 2^N-2^(N-1) = 2^(N-1)&lt;/code&gt;. Excluding the extremal exponents, which are reserved for encoding exceptional cases, the effective exponent range goes from the lowest effective value &lt;code&gt;-2^(N-1)+2&lt;/code&gt; up to highest effective value &lt;code&gt;2^(N-1)-1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Numerous floating-point formats are in use. Different CPU architectures and programming languages support different such formats. They generally differ by how many bits they afford for the mantissa and the exponent.&lt;/p&gt;

&lt;p&gt;The most important floating-point number formats are &lt;strong&gt;IEEE 754 single precision&lt;/strong&gt; and &lt;strong&gt;IEEE 754 double precision&lt;/strong&gt;. Let us review them in more detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IEEE 754 single precision&lt;/strong&gt; occupies 32 bits of memory. This corresponds to the type &lt;code&gt;float&lt;/code&gt; in most implementations of the C programming language. A single-precision floating-point number reserves 23 bits for the mantissa and 8 bits for the exponent.&lt;/p&gt;

&lt;p&gt;The exponent occupies 8 bits, representing unsigned integers from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;255&lt;/code&gt;. Accordingly, the exponent bias in single precision is &lt;code&gt;127 = 2^7&lt;/code&gt;, and the nominal exponent range goes from &lt;code&gt;-127&lt;/code&gt; to &lt;code&gt;128&lt;/code&gt;. However, the smallest and largest exponents are reserved for representing exceptional values. The actual exponent range, therefore, only goes from &lt;code&gt;-126&lt;/code&gt; to &lt;code&gt;127&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The mantissa bits encode an unsigned integer within the range of &lt;code&gt;0&lt;/code&gt; through &lt;code&gt;8388607&lt;/code&gt;, representing the fractional binary digits. If we divide that by &lt;code&gt;8388608 = 2^23&lt;/code&gt; and add &lt;code&gt;1&lt;/code&gt;, then we recover the true mantissa of the number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IEEE 754 double precision&lt;/strong&gt; occupies 64 bits of memory and corresponds to the type &lt;code&gt;double&lt;/code&gt; in most implementations of the C programming language. A double-precision floating-point number reserves 52 bits for the mantissa and 11 bits for the exponent.&lt;/p&gt;

&lt;p&gt;The exponent occupies 11 bits, representing unsigned integers from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;2047 = 2^11 - 1&lt;/code&gt;. Accordingly, the exponent bias in double-precision is &lt;code&gt;1023 = 2^10-1&lt;/code&gt;, and the nominal exponent range goes from &lt;code&gt;-1023&lt;/code&gt; to &lt;code&gt;1024&lt;/code&gt;. Again, the smallest and largest exponents are reserved for representing exceptional values, so the actual exponent range only goes from &lt;code&gt;-1022&lt;/code&gt; to &lt;code&gt;1023&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The mantissa bits encode an unsigned integer within the range of &lt;code&gt;0&lt;/code&gt; through &lt;code&gt;2^52-1&lt;/code&gt;, representing the fractional binary digits. If we divide that by &lt;code&gt;2^52&lt;/code&gt; and add &lt;code&gt;1&lt;/code&gt;, then we recover the true mantissa of the number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common floating-point formats
&lt;/h2&gt;

&lt;p&gt;Common folklore says that the single-precision type (&lt;code&gt;float&lt;/code&gt;) is processed faster by floating-point units than the double-precision type (&lt;code&gt;double&lt;/code&gt;) whilst only taking half the size. However, though single-precision is adequate for many computations, scientific applications may require double-precision calculations. Several other floating-point formats are summarized in the following table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Total bits&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Mantissa bits&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Exponent bits&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Bias&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Exponent range&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Half (binary16)&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;−14 to +15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Single (binary32)&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;127&lt;/td&gt;
&lt;td&gt;−126 to +127&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Double (binary64)&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;1023&lt;/td&gt;
&lt;td&gt;−1022 to +1023&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Quadruple (binary128)&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;td&gt;112&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;16383&lt;/td&gt;
&lt;td&gt;−16382 to +16383&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Octuple (binary256)&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;236&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;16383&lt;/td&gt;
&lt;td&gt;−16382 to +16383&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bfloat16 (Brain Float)&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;127&lt;/td&gt;
&lt;td&gt;−126 to +127&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;x86 Extended Precision (80-bit)&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;64(or 63)&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;16383&lt;/td&gt;
&lt;td&gt;−16382 to +16383&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The 80-bit is technically the odd man out here: not following the hidden bit convention, it stores the leading digit. It is a legacy format stemming from the early days of Intel floating-point math.&lt;/p&gt;

&lt;p&gt;Single and double precision are natively supported in mainstream CPUs. By contrast, the half-, quad-, and octo-precision types lack widespread hardware support and are emulated via software at best. Half-precision types are more relevant for GPU computing, and might be accessed indirectly via libraries that control GPU-side calculations. Extended precision types beyond &lt;code&gt;double&lt;/code&gt; have found resonance in specialized scientific applications.&lt;/p&gt;

&lt;p&gt;When the first C compiler was introduced in 1972, the data types &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt; were already part of the language. The terminology suggests that single-precision was considered the standard floating-point type back then. With ANSI C in 1989, double-precision became the default type for floating-point literals, echoing that double-precision is the norm nowadays. Floating-point formats besides single and double precision are not natively supported in C but may be provided via compiler extensions (e.g., &lt;code&gt;_Float16&lt;/code&gt; or &lt;code&gt;_Float128&lt;/code&gt;). The C type &lt;code&gt;long double&lt;/code&gt; is implementation-defined: in the Windows ecosystem, it is equivalent to &lt;code&gt;double&lt;/code&gt;; contingent on hardware support, it corresponds to the 80-bit standard on x86 and to the 128bit standard on RISC-V processors.&lt;/p&gt;

&lt;p&gt;To get a feel for these floating-point formats, we take a look at approximate decimal precisions and ranges for some IEEE 754 formats:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Mantissa dec.&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;max value&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;min value&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Half (binary16)&lt;/td&gt;
&lt;td&gt;3.31&lt;/td&gt;
&lt;td&gt;65504&lt;/td&gt;
&lt;td&gt;5.96e-8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Single (binary32)&lt;/td&gt;
&lt;td&gt;7.22&lt;/td&gt;
&lt;td&gt;3.40e38&lt;/td&gt;
&lt;td&gt;1.40e-45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Double (binary64)&lt;/td&gt;
&lt;td&gt;15.95&lt;/td&gt;
&lt;td&gt;1.80e308&lt;/td&gt;
&lt;td&gt;4.94e-324&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Quadruple (binary128)&lt;/td&gt;
&lt;td&gt;34.02&lt;/td&gt;
&lt;td&gt;1.19e932&lt;/td&gt;
&lt;td&gt;6.48e-4966&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IEEE 754 Octuple (binary245)&lt;/td&gt;
&lt;td&gt;71.34&lt;/td&gt;
&lt;td&gt;1.61e78913&lt;/td&gt;
&lt;td&gt;2.25e-78984&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Normalized floating-point numbers and special cases
&lt;/h2&gt;

&lt;p&gt;When the exponent bits assume the extremal values of the exponent range, either the highest or lowest possible exponent, then the bits of the floating-point number is not interpreted in the usual way. Instead, that situation encodes special values.&lt;/p&gt;

&lt;p&gt;First, we need the notion of &lt;strong&gt;normalized floating-point number&lt;/strong&gt;. A normalized floating-point number for a floating-point format with &lt;code&gt;N&lt;/code&gt; exponent bits is simply a number where the exponent bit part is at least &lt;code&gt;1&lt;/code&gt; and at most &lt;code&gt;2^N-2&lt;/code&gt;, that is, it is any exponent except the largest and the smallest. Floating-point numbers whose bit patterns satisfy that condition, the normalized floating-point numbers, are interpreted in the standard manner.&lt;/p&gt;

&lt;p&gt;Floating-point numbers whose exponent assumes one of the two extremal values are &lt;strong&gt;not normalized&lt;/strong&gt;. The IEEE 754 standard reserves special interpretations for such floating-point numbers, where the bits now take on a different meaning.&lt;/p&gt;

&lt;h3&gt;
  
  
  Infinity
&lt;/h3&gt;

&lt;p&gt;If all exponent bits are set to &lt;code&gt;1&lt;/code&gt; and all mantissa bits are set to &lt;code&gt;0&lt;/code&gt;, then the floating-point number is interpreted as an infinity. This is either &lt;code&gt;+inf&lt;/code&gt; if the sign bit is not set or &lt;code&gt;-inf&lt;/code&gt; if the sign bit is set.&lt;/p&gt;

&lt;p&gt;These infinities are introduced to handle overflows: when the result of a floating-point computation becomes so large in magnitude that they cannot be represented by a normalized floating-point number, then it is stored as a positive or negative infinity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Not-a-number
&lt;/h3&gt;

&lt;p&gt;If all exponent bits are set to &lt;code&gt;1&lt;/code&gt; and not all mantissa bits are set to &lt;code&gt;0&lt;/code&gt;, then the floating-point number is interpreted as &lt;strong&gt;not-a-number&lt;/strong&gt;, or &lt;strong&gt;NaN&lt;/strong&gt;. This encodes an invalid computational result, e.g., the division of infinities results in a &lt;code&gt;nan&lt;/code&gt;, and the square root of a negative number yields &lt;code&gt;nan&lt;/code&gt;. For now, we shall not be further concerned with the technical intricacies of &lt;code&gt;not-a-number&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zero
&lt;/h3&gt;

&lt;p&gt;The number zero cannot be represented as a normalized floating-point number: we remember that any normalized floating-point number must have &lt;code&gt;1&lt;/code&gt; as the leading digit of its mantissa, following the hidden bit convention. This obviously excludes the number zero.&lt;/p&gt;

&lt;p&gt;Hence, zero is treated as a special case in the floating-point format. By convention, a zero is a floating-point number where both the exponent bits and the mantissa bits are all zero. The sign bit can be either zero or one, which is why we have two possible zeroes: either the &lt;strong&gt;positive zero&lt;/strong&gt; or the &lt;strong&gt;negative zero&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These two zeroes are examples of denormalized numbers, which we shall discuss now.&lt;/p&gt;

&lt;h3&gt;
  
  
  Denormalized numbers
&lt;/h3&gt;

&lt;p&gt;We recall once again that normalized floating-point number formats generally adhere to the hidden bit convention: the leading digit in their mantissa is implicitly &lt;code&gt;1&lt;/code&gt; and not stored explicitly in memory. Only the fractional part of the mantissa is stored explicitly. But in the special case when the exponent bits are all zero, the mantissa and exponent bits are interpreted differently, and we call such floating-point numbers &lt;strong&gt;denormalized&lt;/strong&gt; or &lt;strong&gt;subnormal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Denormalized numbers represent values extremely close to zero and are implemented as follows. First, the exponent is now interpreted as &lt;code&gt;-2^(N-1)+2&lt;/code&gt;. If we were to follow the usual convention of normalized numbers, then the exponent would be the negative bias &lt;code&gt;-2^(N-1)+1&lt;/code&gt; instead. Second, the mantissa is now interpreted with a leading &lt;code&gt;0&lt;/code&gt; instead of a leading &lt;code&gt;1&lt;/code&gt;. Lastly, the sign bit is interpreted as usual.&lt;/p&gt;

&lt;p&gt;Effectively, we interpret the mantissa bit as an unsigned integer &lt;code&gt;x&lt;/code&gt; with &lt;code&gt;M&lt;/code&gt; bits, ranging from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;2^M-1&lt;/code&gt;, and the denormalized floating-point number represents &lt;code&gt;x/2^(M) * 2^(-2^(N-1)+2)&lt;/code&gt;. This is strictly less than the smallest normalized floating-point number, which is &lt;code&gt;2^(-2^(N-1)+2)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What is the purpose of denormalized numbers?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Representation of the number zero, as mentioned above.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handling of underflows: If the computation result is too small to be represented by a normalized number, then it is stored as a denormalized number. Denormalized numbers are designed to enable a gradual transition to zero when computational results become very small, avoiding an abrupt flush to zero.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, &lt;strong&gt;computation with denormalized numbers&lt;/strong&gt; may incur a performance penalty. The background is that the floating-point units are designed and optimized to handle normalized floating-point numbers, which are generally considered the standard case; denormalized numbers are expected to be the rare exception. Most FPUs do not support subnormal numbers in hardware but emulate them via microcode or software, which saves transistors on the chip. Even if the hardware natively supports subnormal calculations, it usually comes with a performance penalty.&lt;/p&gt;

&lt;p&gt;This is why many CPUs support optional switches that guarantee that subnormal numbers are automatically treated as zero in computations or flushed to zero immediately, thus avoiding subnormal calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish
&lt;/h2&gt;

&lt;p&gt;This article has explored the very basics of floating-point numbers and their bit representations. Future articles will address select topics in more depth.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to tell your compiler that you have implemented printf-like functions</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:44:56 +0000</pubDate>
      <link>https://dev.to/martinlicht/how-to-tell-your-compiler-that-you-have-implemented-printf-like-functions-2bfp</link>
      <guid>https://dev.to/martinlicht/how-to-tell-your-compiler-that-you-have-implemented-printf-like-functions-2bfp</guid>
      <description>&lt;p&gt;Functions such as &lt;code&gt;printf&lt;/code&gt; have been a hallmark of C. These variadic functions accept a variable number of arguments as input and convert them into a character sequence according to a format string. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// ... possibly recalculate the variable temperature.&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Room temperature at %u&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An experienced programmer will recognize the subtle bug in the above example. The program intends to print a line of the form &lt;code&gt;Room temperature at 20&lt;/code&gt; or whatever the value of the variable &lt;code&gt;temperature&lt;/code&gt; is. That seems to work well when the argument is a positive number: the format string &lt;code&gt;Room temperature at %u&lt;/code&gt; should indicate that printf should fetch an integer from the stack (or register) and print its value.&lt;/p&gt;

&lt;p&gt;But here is the bug: the format specifier &lt;code&gt;%u&lt;/code&gt; tells printf to fetch an &lt;strong&gt;unsigned&lt;/strong&gt; integer even though the argument provided is a &lt;strong&gt;signed&lt;/strong&gt; integer. That will not cause difficulties if the signed integer is positive, at least on mainstream CPU targets: the positive integers within the range of &lt;code&gt;signed int&lt;/code&gt; have the same bit representations when represented in &lt;code&gt;unsigned int&lt;/code&gt;, and the printed characters will be the same.&lt;/p&gt;

&lt;p&gt;However, if the variable &lt;code&gt;temperature&lt;/code&gt; contains a negative value, then the interpretation as &lt;code&gt;unsigned int&lt;/code&gt; will be a large positive integer. And that is exactly what printf will display.&lt;/p&gt;

&lt;p&gt;The code is still legal C. Hence, such bugs generally cannot be detected at compile-time. However, there are ways and means to mitigate the situation: we can enable a wide range of compiler flags when using GCC or Clang, which will trigger warnings in many situations where format strings are misused by the programmer.&lt;/p&gt;

&lt;p&gt;For example, the above lines of code will trigger a warning if we enable the flag &lt;code&gt;-Wformat-signedness&lt;/code&gt;. In other words, GCC and Clang offer static code analysis functionality to specifically analyze the usage of the functions such as &lt;code&gt;printf&lt;/code&gt;, &lt;code&gt;snprintf&lt;/code&gt;, or &lt;code&gt;vprintf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://martinlicht.hashnode.dev/warning-options-for-printf-incantations" rel="noopener noreferrer"&gt;A previous article&lt;/a&gt; has discussed these options in greater detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing your own printf-style functions
&lt;/h2&gt;

&lt;p&gt;Variadic functions are part of the C language, and we can easily write such functions ourselves. There are circumstances where we would like, more specifically, a variadic function that mimics the behavior of &lt;code&gt;printf&lt;/code&gt;. For example, the following function will behave like printf but &lt;em&gt;prepend&lt;/em&gt; the prefix &lt;code&gt;[LOG]&lt;/code&gt; before each output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printf_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;va_list&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;va_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;fputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[LOG] "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;va_end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us break down the code. The function contains one named parameter: the format string &lt;code&gt;fmt&lt;/code&gt; of type &lt;code&gt;const char*&lt;/code&gt;. The ellipsis ... marks the function as variadic, so it can take any number of further optional arguments.&lt;/p&gt;

&lt;p&gt;How do we access these additional parameters? In our case, we want to forward these to some printf-like function. First, we declare a variable &lt;code&gt;params&lt;/code&gt; whose type is &lt;code&gt;va_list&lt;/code&gt;: this type can hold information about variadic arguments and is implementation-defined. We do not need to know its further details.&lt;/p&gt;

&lt;p&gt;The call &lt;code&gt;va_start(params,fmt)&lt;/code&gt; invokes a macro: it indicates that the variadic parameters start after the parameter &lt;code&gt;fmt&lt;/code&gt; and stores that information in &lt;code&gt;params&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The call &lt;code&gt;va_end(params)&lt;/code&gt; is complementary to the invocation of &lt;code&gt;va_start&lt;/code&gt;: it is a macro that indicates that we are done with processing the variadic argument list.&lt;/p&gt;

&lt;p&gt;The action takes place in the middle: after the output of our logging prefix, we forward the variadic argument list to &lt;code&gt;vprintf&lt;/code&gt;. The behavior of this function is identical to that of printf except that it directly receives the &lt;code&gt;va_list&lt;/code&gt; variable instead of the separate arguments.&lt;/p&gt;

&lt;p&gt;Clearly, such functions are useful in practice. However, unlike the C standard library functions such as &lt;code&gt;printf&lt;/code&gt;, these user-declared functions do not enjoy the extensive warning options facilitated by GCC or Clang. That is a major drawback because we could accidentally write, say,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of pi is ca. %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14159&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but receive no warning about such misuse. Here, the function will print gibberish because it interprets the bits of the double value &lt;code&gt;3.14159&lt;/code&gt; as an &lt;code&gt;int&lt;/code&gt;, as indicated by &lt;code&gt;%d&lt;/code&gt; in the format string.&lt;/p&gt;

&lt;p&gt;Fortunately, there is a remedy for that situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The format attribute
&lt;/h2&gt;

&lt;p&gt;GCC and Clang offer the format attribute to indicate that a variadic function should behave like one of the functions in the standard library and, most crucially, trigger the same warning behavior whenever the format string does not match the provided arguments.&lt;/p&gt;

&lt;p&gt;We explain the attribute with our example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printf_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="n"&gt;__attribute__&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;va_list&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;va_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;fputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[LOG] "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;va_end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The attribute receives three arguments. The first argument tells the compiler that &lt;code&gt;printf_log&lt;/code&gt; receives arguments in the same relation with each other as the ones given to &lt;code&gt;printf&lt;/code&gt;. The second argument then specifies which of the parameters of &lt;code&gt;printf_log&lt;/code&gt; receive the format string. In our case, that is the first (&lt;code&gt;1&lt;/code&gt;) parameter. Lastly, we provide the argument with the position of the first parameter to be processed as the parameter list.&lt;/p&gt;

&lt;p&gt;With that declaration, the compiler will know how to interpret the format string. It will issue the respective warnings whenever it detects some mismatch.&lt;/p&gt;

&lt;p&gt;The attribute must be visible at the call site to enable the warnings, so we typically include this attribute at the declaration. The header declaration of our example will normally look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printf_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="n"&gt;__attribute__&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, we are ready to go!&lt;/p&gt;

</description>
      <category>c</category>
      <category>coding</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Warning options for printf incantations</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:44:14 +0000</pubDate>
      <link>https://dev.to/martinlicht/warning-options-for-printf-incantations-3fj2</link>
      <guid>https://dev.to/martinlicht/warning-options-for-printf-incantations-3fj2</guid>
      <description>&lt;p&gt;The &lt;code&gt;printf&lt;/code&gt; function in C is known for its technical syntax and error proneness. This is shared by its numerous variants, such as &lt;code&gt;snprintf&lt;/code&gt; or &lt;code&gt;vsprintf&lt;/code&gt;. This article will discuss how your C compiler, GCC or Clang, can help you mitigate these difficulties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter the printf dungeon
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;printf&lt;/code&gt; function accepts a format string and a variable number of optional arguments. The printf format string compactly encodes how to print the values in optional arguments of the function. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%8s: %10d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;%8s: %10d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;% 8s: %10d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Income"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1234567&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Costs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Balance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1230567&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will print the following text, with the particular arrangement of white space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Income:   1234567
    Costs:     -4000
  Balance:   1230567
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each line in this example begins with a string of at least 8 characters, right-aligned with leading space, followed by a colon and a space, and finally, a right-aligned decimal integer occupying at least 10 character positions.&lt;/p&gt;

&lt;p&gt;As this example illustrates, the format string describes both the number and the types of the arguments passed to printf, as well as how each value should be formatted. After the format string, the corresponding values to be printed are passed as variadic arguments. Intuitively, these arguments must match the number and types expected by the format string.&lt;/p&gt;

&lt;p&gt;Understanding how variadic functions are implemented is crucial here. The optional arguments in a variadic function call are pushed onto the stack (or passed via registers, depending on the calling convention) without any accompanying metadata. That is, the function receives no explicit information about how many arguments there are or what types they have. This metadata must be provided by other means. In the case of &lt;code&gt;printf&lt;/code&gt; and related functions, the format string serves precisely that role: it encodes the number and types of the arguments in a compact and symbolic form.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let us reflect upon just how low-level this feature is: the printf function receives a pointer to the format string. Based on that character sequence, it performs read and perhaps write operations throughout the stack.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The printf lower levels
&lt;/h2&gt;

&lt;p&gt;The printf function, however, has no means of verifying that the optional arguments actually match the description in the format string. This may cause incorrect output by misinterpreting the stack. Even worse, stack corruption is possible if the format string contains write-back instructions. Getting the &lt;code&gt;printf&lt;/code&gt; string correct requires careful attention to detail.&lt;/p&gt;

&lt;p&gt;For instance, take a look at the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The temperature is %u degrees outside!%n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the very first glance, the code's purpose seems to print an integer (&lt;code&gt;-10&lt;/code&gt;) on a single line. However, this will not work as expected. On the one hand, the bit pattern of the negative integer -10, which uses the two's complement representation, will be interpreted as an unsigned integer (&lt;code&gt;%u&lt;/code&gt; instead of &lt;code&gt;%d&lt;/code&gt;). As a result, an incomprehensibly larger positive number is printed instead. On the other hand, the author probably meant to write a line break &lt;code&gt;\n&lt;/code&gt; at the end but wrote an accidental &lt;code&gt;%n&lt;/code&gt; instead. That format specifier will tell printf to assume that one more argument is provided: a pointer to a memory location where it is supposed to write the number of characters printed so far. Obviously, no such pointer has been pushed onto the stack, so printf will write the value into some completely wrong place and accidentally corrupt the stack. This is clearly a source of bugs and potential security vulnerabilities.&lt;/p&gt;

&lt;p&gt;That being said, the above code will compile just fine. Nothing in the language standard prevents us from misusing printf when the format string does not match the arguments, leading to pathological examples such as the one above. The capabilities of C are not expressive enough to diagnose the mismatch between the format string and the arguments at compile-time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Warning options to the rescue
&lt;/h2&gt;

&lt;p&gt;While ill-formed format strings are technically allowed by the C standard, compilers offer warnings that help detect such issues. This gets us fairly close to detecting such bugs at compile-time. These warning features are enabled and controlled via numerous option flags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-Wformat&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wno-format&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat=0&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat=1&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat=2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These compiler flags broadly enable (or disable) the different warning levels for format functions at different levels. You can either switch them off completely (&lt;code&gt;-Wno-format&lt;/code&gt; or &lt;code&gt;-Wformat=0&lt;/code&gt;), enable the most important warnings (&lt;code&gt;-Wformat&lt;/code&gt; or &lt;code&gt;-Wformat=1&lt;/code&gt;), or enable even stricter warnings (&lt;code&gt;-Wformat=2&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;For example, with &lt;code&gt;-Wformat&lt;/code&gt; we receive a warning about the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pi is ca. %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14159&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us have a look at some specialized warning options in more detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-Wformat-contains-nul&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This flag will have warnings triggered if the format string contains null bytes, that is, if the string terminates earlier than expected due to an unintended &lt;code&gt;\0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-Wformat-extra-args&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Typically, printf ignores any excess arguments passed in the variable argument list which are not referenced in the format string. Such mismatches are often unintentional. When this flag is enabled, a warning will be triggered in any such situation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World! #%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14159&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-Wformat-signedness&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When this warning flag is set, then the compiler will warn you whenever signedness indicated in the format specifier does not match the argument's type. For example, we may use &lt;code&gt;%d&lt;/code&gt; but pass an unsigned argument or use &lt;code&gt;%u&lt;/code&gt; and pass a signed argument. The output will then confuse negative signed numbers and very large unsigned integers.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;numeric_limits&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Variable u has value %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;signed&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Variable s has value %u&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-Wno-format-zero-length&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat-zero-length&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Enabling this flag will have the compiler emit a warning whenever the format string has length zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-Wformat-nonliteral&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat-security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the format flag is not a literal, then several bugs or security vulnerabilities can be introduced. For example, code of the form&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fmt_str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_format_string&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmt_str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;may lead to undefined behavior, stack memory leakage, or memory corruption if &lt;code&gt;fmt_str&lt;/code&gt; includes some format specifiers. In particular, these may cause printf to expose secrets on the stack. For example, if the format specifier &lt;code&gt;%n&lt;/code&gt; appears in the string, this can potentially corrupt memory on the stack and poses a security risk.&lt;/p&gt;

&lt;p&gt;The flag &lt;code&gt;-Wformat-nonliteral&lt;/code&gt; makes the compiler issue warnings whenever the format string is not a literal string and hence cannot be checked for such a mismatch of format specifiers and arguments.&lt;br&gt;
At the time of this writing, the flag &lt;code&gt;-Wformat-security&lt;/code&gt; enables a subset of the checks enabled by &lt;code&gt;-Wformat-nonliteral&lt;/code&gt;. At the time of this writing, the official documentation of GCC indicates that the former flag will enable some additional checks on its own in future releases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-Wformat-truncation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat-truncation=level&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat-overflow&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;-Wformat-overflow=level&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-Wformat-truncation&lt;/code&gt; and &lt;code&gt;-Wformat-overflow&lt;/code&gt; warnings are relevant for functions such as&lt;code&gt;snprintf&lt;/code&gt; and &lt;code&gt;sprintf&lt;/code&gt;, which involve writing formatted output to memory buffers. These flags address subtle bugs due to output truncation or buffer overflows. The latter are a source of security vulnerabilities.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;-Wformat-truncation&lt;/code&gt; is set, then a warning will be triggered when the compiler can determine that a call to an output function such as &lt;code&gt;snprintf&lt;/code&gt; or &lt;code&gt;vsnprintf&lt;/code&gt; will truncate the output because the buffer is too small to contain the formatted string, including the null terminator. Such truncations are often unintentional.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;snprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;"Result: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;123456&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;-Wformat-overflow&lt;/code&gt; is set, then a warning will be triggered when the compiler can determine that the output will exceed the size of the buffer. Such a buffer overflow compromises memory safety. This pertains to functions such as &lt;code&gt;sprintf&lt;/code&gt; or &lt;code&gt;vsprintf&lt;/code&gt;, which do not limit the number of characters into the buffer.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is a very long string: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These flags can be set with a certain &lt;strong&gt;level&lt;/strong&gt; that determines the effort and thoroughness with which the compiler will try to detect truncation or overflow errors. Generally speaking, switching on optimization improves the detection of such errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Treasure chest
&lt;/h2&gt;

&lt;p&gt;For completeness, I also mention the warning flag &lt;code&gt;-Wformat-y2k&lt;/code&gt;, which does not concern format string as in &lt;code&gt;printf&lt;/code&gt; but a different type of format strings, namely the type accepted by &lt;code&gt;strftime&lt;/code&gt;. The latter function is supposed to print dates in human-readable form. The warning flag &lt;code&gt;-Wformat-y2k&lt;/code&gt; warns whenever a year is only printed as a two-digit year instead of the full four-digit year. Obviously, that is a whole different class of format strings.&lt;/p&gt;

</description>
      <category>c</category>
      <category>codequality</category>
      <category>programming</category>
      <category>tooling</category>
    </item>
    <item>
      <title>/* DISABLES CODE */: When Clang warns about unreachable code</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:41:56 +0000</pubDate>
      <link>https://dev.to/martinlicht/-disables-code-when-clang-warns-about-unreachable-code-47ml</link>
      <guid>https://dev.to/martinlicht/-disables-code-when-clang-warns-about-unreachable-code-47ml</guid>
      <description>&lt;p&gt;I would like to write about unreachable code in C and C++ and about the pertinent compiler warnings in this context.&lt;/p&gt;

&lt;p&gt;Sometimes, entire blocks of our C or C++ source codes are unintentionally cut off from the control flow. Consider the following demonstrative (albeit somewhat contrived) example written in modern C.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;report_condition&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;check_complicated_condition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// author forgot to put b here.&lt;/span&gt;
        &lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Condition holds!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Condition fails!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Never executed.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such a function may be written for debugging: at each call, it reports whether the condition of interest is satisfied. However, it very much seems that the author forgot to check the actual variable b, perhaps because the function &lt;code&gt;check_complicated_condition&lt;/code&gt; was not available during earlier stages of coding. Therefore, the control flow will never enter the else-branch, which we can safely assume to be a mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler warnings
&lt;/h2&gt;

&lt;p&gt;As diligent C and C++ programmers, we naturally switch on as many compiler warnings as possible. The Clang compiler offers us the &lt;code&gt;-Wunreachable-code&lt;/code&gt; warning flag for situations like the above. The compiler will warn us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;warning: code will never be executed &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-Wunreachable-code&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This compiler flag is available to us with Clang. The GCC also had this feature for a long time, but the flag was removed because the necessary code analysis was too complex. In particular, the control flow also depends on the optimization level, complicating the matter further.&lt;/p&gt;

&lt;p&gt;Nota bene, the compiler flag &lt;code&gt;-Wunreachable-code&lt;/code&gt; is not yet included in &lt;code&gt;-Wall&lt;/code&gt; and &lt;code&gt;-Wextra&lt;/code&gt; but is easily missed: we must explicitly enable it as an option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Application in debugging
&lt;/h2&gt;

&lt;p&gt;Even if we want to have as many compiler flags switched on as possible, this can be troublesome during debugging. For example, the following debugging statements can accumulate during a longer code fix session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Code block used occasionally during debugging,&lt;/span&gt;
    &lt;span class="c1"&gt;// but intentionally inactive most of the time.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clang will warn us that the code block will never be executed. However, the above example is so common in practice that it warrants an explicit opt-out. One notices that the specific warning emitted by Clang is something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;note: silence by adding parentheses to mark code as explicitly dead
      |     &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      |        ^
      |        /&lt;span class="k"&gt;*&lt;/span&gt; DISABLES CODE &lt;span class="k"&gt;*&lt;/span&gt;/ &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It took me a while to understand how this is supposed to work, which prompted me to write this blog entry. It is enough to set brackets to mark the code as explicitly dead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bracketed comment, on the other hand, is purely decorative. It is probably good practice to mark dead code accordingly, as in,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; /&lt;span class="k"&gt;*&lt;/span&gt; DISABLES CODE &lt;span class="k"&gt;*&lt;/span&gt;/ &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    // ...
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or with any other macro. This allows us to find explicitly inaccessible code more quickly in the code base, e.g., when grepping this specific comment string within the code base.&lt;/p&gt;

&lt;p&gt;This being one of the more arcane features of your C and C++ compiler, I hope you found this article helpful for your future efforts in writing clean code and easing your debugging. &lt;/p&gt;

</description>
      <category>c</category>
      <category>codequality</category>
      <category>cpp</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Removing duplicates from a C++ container, using forward iterators</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:40:59 +0000</pubDate>
      <link>https://dev.to/martinlicht/removing-duplicates-from-a-c-container-using-forward-iterators-no1</link>
      <guid>https://dev.to/martinlicht/removing-duplicates-from-a-c-container-using-forward-iterators-no1</guid>
      <description>&lt;p&gt;We further discuss how to remove duplicates from a C++ container. A previous &lt;a href="https://martinlicht.hashnode.dev/how-to-remove-duplicates-from-a-c-container" rel="noopener noreferrer"&gt;article&lt;/a&gt; discusses the removal of duplicates from random access containers. But not every container supports random access; more precisely, not every container supports random access iterators.&lt;/p&gt;

&lt;p&gt;Let us discuss removing duplicates when the container interface is the most basic one: only supporting forward iterators. Moreover, we will not require that the elements support comparisons.&lt;/p&gt;

&lt;p&gt;A very basic and naive algorithm works in-place and runs in quadratic time. That is strictly worse than the in-place algorithms that run in log-linear time, assuming the container is random access and the elements can be sorted.&lt;/p&gt;

&lt;p&gt;However, this surprisingly is not the end of the whole story: if we are willing to tolerate additional memory consumption, then we achieve much better time complexity for these algorithms.&lt;/p&gt;

&lt;h2&gt;
  
  
  A classical approach
&lt;/h2&gt;

&lt;p&gt;Our first approach uses the &lt;code&gt;remove_if&lt;/code&gt; algorithm in the Standard Template Library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;ForwardIterator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(;&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;runner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;remove_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some explanations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The function assumes as a precondition that &lt;code&gt;begin&lt;/code&gt; will equals &lt;code&gt;end&lt;/code&gt; after a finite number of increments (possibly none). This precondition remains true throughout each iteration of the loop body. At the end, we return &lt;code&gt;begin&lt;/code&gt;, which will equal &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At each loop iteration, we let &lt;code&gt;runner&lt;/code&gt; be the successor of &lt;code&gt;begin&lt;/code&gt;. By the loop invariant, &lt;code&gt;runner&lt;/code&gt; will equal &lt;code&gt;end&lt;/code&gt; after a finite number of iterator increments, possibly none.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The call to &lt;code&gt;remove_if&lt;/code&gt; will target all the elements between &lt;code&gt;runner&lt;/code&gt; and up to (excluding) &lt;code&gt;end&lt;/code&gt; which equal &lt;code&gt;v&lt;/code&gt;. Internally, &lt;code&gt;remove_if&lt;/code&gt; uses move assignments to achieve the following outcome: if there are, say, &lt;code&gt;k&lt;/code&gt; elements within the range that equal &lt;code&gt;v&lt;/code&gt;, then the last &lt;code&gt;k&lt;/code&gt; elements before &lt;code&gt;end&lt;/code&gt; are in a moved-from state, and none of the preceding members equals &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function &lt;code&gt;remove_if&lt;/code&gt; returns an iterator to the new logical end of the range, which is between &lt;code&gt;runner&lt;/code&gt; and (excluding) &lt;code&gt;end&lt;/code&gt;. As we assign &lt;code&gt;end&lt;/code&gt;, notice that the loop invariant is maintained.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we save the return value in a variable &lt;code&gt;last&lt;/code&gt;, then the range &lt;code&gt;[begin, last)&lt;/code&gt; will contain the original elements of the original but with all duplicates removed. All members within the range &lt;code&gt;[last,end)&lt;/code&gt; will be in a moved-from state.&lt;/p&gt;

&lt;p&gt;Let us combine the above code with some example main function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// output the remaining items&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the removed items still exist but in unspecified state &lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, there is a caveat to this implementation of &lt;code&gt;remove_duplicates&lt;/code&gt;, in comparison to the variant that uses random access iterators: the runtime generally grows quadratically in the number of elements. This is realized, for example, when the original range contains &lt;code&gt;N&lt;/code&gt; different elements with no duplicates at all: then the run time will be quadratic in &lt;code&gt;N&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Generally speaking, the run time of this algorithm will be shorter the &lt;strong&gt;more&lt;/strong&gt; duplicates there are. While the quadratic worst case is realized if there are no duplicates at all, the algorithm will only take a linear number of steps if the range contains only the same type of element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use a Hash table
&lt;/h2&gt;

&lt;p&gt;This was pretty elementary. But is there a way to improve the run time? The underlying idea is that we run over the entire range once and remove elements that have been encountered at previous positions. What causes the run time to grow beyond linear is that each step, checking whether the element has already been encountered requires more than constant time.&lt;/p&gt;

&lt;p&gt;In our attempt to ameliorate this situation, let us have a look at the following implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;set&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;ForwardIterator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;remove_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// `contains` needs C++20&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The C++ standard mandates that the number of comparisons invoked by &lt;code&gt;remove_if&lt;/code&gt; is exactly the distance &lt;code&gt;end - begin&lt;/code&gt;. That means that no more than &lt;code&gt;N&lt;/code&gt; comparisons are made within a range of &lt;code&gt;N&lt;/code&gt; elements, and so the comparison is called on any element exactly once. This excludes the possibility of double removals, though this would not be any serious concern in the most straight-forward implementation of &lt;code&gt;remove_if&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Notice that the &lt;code&gt;insert&lt;/code&gt; and &lt;code&gt;contains&lt;/code&gt; methods have run time logarithmic in the number of elements in the set. On a side note, &lt;code&gt;contains&lt;/code&gt; is only available from C++20 onward.&lt;/p&gt;

&lt;p&gt;How does compare to implementation using &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;unique&lt;/code&gt;, as discussed in a previous gist? Both approaches lead to time complexity &lt;code&gt;N log N&lt;/code&gt;. But using sorting can be implemented as an in-place algorithm.&lt;/p&gt;

&lt;p&gt;Of course, &lt;code&gt;std::set&lt;/code&gt; requires that the type of the elements supports comparisons. But we would like to even ditch that requirement. Alternatively, we can use an unordered set. The container &lt;code&gt;std::unordered_set&lt;/code&gt; has been available since the advent of C++11.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unordered_set&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;ForwardIterator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unordered_set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;remove_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;ForwardIterator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unordered set data structure is a bit of mixed bag in practice. The C++ standard requires that the operations &lt;code&gt;contains&lt;/code&gt; and &lt;code&gt;insert&lt;/code&gt; run in asymptotic constant time. This is implemented using a hash table. In real life, outside of the platonic realm of the C++ standard, &lt;a href="https://medium.com/plaxis/embarrassingly-poor-performance-of-regular-point-sets-with-std-unordered-set-418cb413974b" rel="noopener noreferrer"&gt;it has been reported&lt;/a&gt; that some implementations do not satisfy the official run time requirements due to sub-optimal hash functions, so the real life performance may be considerably worse.&lt;/p&gt;

&lt;p&gt;Hence, if we allow for additional memory on a linear order in the worst case, then we can hope to achieve average linear time complexity. In fact, some might consider this to be even better than the much more idiomatic combination of sort-unique.&lt;/p&gt;

&lt;h2&gt;
  
  
  See also
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/container/list/sort" rel="noopener noreferrer"&gt;unordered_set&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/container/set/contains" rel="noopener noreferrer"&gt;contains&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>cpp</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to remove duplicates from a C++ container</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Sat, 14 Mar 2026 19:40:18 +0000</pubDate>
      <link>https://dev.to/martinlicht/how-to-remove-duplicates-from-a-c-container-hmc</link>
      <guid>https://dev.to/martinlicht/how-to-remove-duplicates-from-a-c-container-hmc</guid>
      <description>&lt;p&gt;&lt;a href="https://gist.github.com/martinlicht/3a40e27cf0a0e5051b18effcde1191b1" rel="noopener noreferrer"&gt;&lt;em&gt;Article based on a GitHub Gist&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We discuss how to remove duplicate elements from C++ Standard Template Library (STL) containers such as &lt;code&gt;std::vector&lt;/code&gt;. Albeit an everyday task, this functionality is not part of the STL. Every C++ programmer sooner or later needs to write this functionality on their own. Writing this up has actually shown me a few surprises.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let us start with the canonical C++ solution to this problem. We use a combination of &lt;code&gt;std::sort()&lt;/code&gt; and &lt;code&gt;std::unique()&lt;/code&gt;, followed by a resizing of the vector. We can write this up with a template for the &lt;code&gt;std::vector&lt;/code&gt; container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// sort the vector &lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// remove consecutive duplicates &lt;/span&gt;

    &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;erase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// erase the remaining duplicates, which are now at the end of the vector&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; // for the output&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// output the modified vector &lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us briefly review the steps in &lt;code&gt;remove_duplicates&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We first sort the elements of the container. Here we see that this already requires a comparison function (either &lt;code&gt;operator&amp;lt;&lt;/code&gt; or &lt;code&gt;std::less&lt;/code&gt;, depending on your C++ version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function &lt;code&gt;std::unique&lt;/code&gt; moves &lt;strong&gt;consecutive&lt;/strong&gt; duplicates to the end of the container. The previous sorting step ensures that all duplicate elements in the container build a successive sequence. &lt;code&gt;std::unique&lt;/code&gt; returns an iterator to the first element of those duplicates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lastly, since all the superfluous duplicates are located at the end of the container, we can simply erase them.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The function &lt;code&gt;std::unique&lt;/code&gt; was introduced precisely for this purpose. The biggest restriction of this general approach is that we need to sort the elements of the container, which requires a comparison. But that restriction has a good reason!&lt;/p&gt;

&lt;p&gt;Namely, the asymptotic time and space complexity of this algorithm is dominated by the complexity of the sorting function, depending on the specific sorting algorithm. In most implementations, such as when using &lt;code&gt;heap sort&lt;/code&gt;, the algorithm works in-place and its runtime typically grows by &lt;code&gt;N log(N)&lt;/code&gt; in the number &lt;code&gt;N&lt;/code&gt; of elements. The function &lt;code&gt;unique&lt;/code&gt; can be implemented in linear time and in-place, so it really the sorting algorithm that matters here.&lt;/p&gt;

&lt;p&gt;Hence, we can expect that removing duplicates can be implemented as an in-place algorithm with with &lt;code&gt;N log(N)&lt;/code&gt; time complexity. However, the specific behavior will depend on the algorithm used inside &lt;code&gt;std::sort&lt;/code&gt;. The C++ standard indeed mandates an asymptotic runtime of &lt;code&gt;N log N&lt;/code&gt; but the required memory is left unspecified.&lt;/p&gt;

&lt;p&gt;Let us complement this with a minor variation of &lt;code&gt;remove_duplicates&lt;/code&gt;: instead of calling &lt;code&gt;erase&lt;/code&gt;, we just resize the vector. That has the same outcome for all practical purposes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// sort the vector &lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// remove consecutive duplicates &lt;/span&gt;

    &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// erase the remaining duplicates, which are now at the end of the vector&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the iterator interface for random access containers
&lt;/h2&gt;

&lt;p&gt;The preceding version of &lt;code&gt;remove_duplicates&lt;/code&gt; takes the container as the argument. This is very much the type of function interface that you encounter in most programming languages - with the exception of the C++ STL, of course! The STL algorithms use iterators all over the place.&lt;/p&gt;

&lt;p&gt;So let us rewrite the function using iterators. We want to provide random access iterators as arguments: one for the beginning of the sequence, and one for the end.&lt;/p&gt;

&lt;p&gt;However, there is a severe constraint: without access to the full container, we cannot remove the remaining duplicates in the manner that we did above. As a remedy, we return the iterator to the logical end of the sequence, similar as &lt;code&gt;std::duplicate&lt;/code&gt;. Rather an anticlimax!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iterator&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;RandomAccessIterator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;RandomAccessIterator&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;RandomAccessIterator&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RandomAccessIterator&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// sort all elements in the range&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// remove consecutive duplicates&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// example using std::vector &lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;new_end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;erase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;new_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// example using array&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;new_end_arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Output the result; since arrays do not support erase, just use new_end_arr&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;new_end_arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we are still restricted to containers that allow random access. What about, say, the &lt;code&gt;std::list&lt;/code&gt;, which is implemented as a doubly-linked list and allows merely biderectional iterators but is not a random access container?&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing duplicates in a bidirectional container
&lt;/h2&gt;

&lt;p&gt;The above version of &lt;code&gt;remove_duplicates&lt;/code&gt; utilizes the algorithms &lt;code&gt;std::sort&lt;/code&gt; and &lt;code&gt;std::unique&lt;/code&gt;. These are only available for containers that support random access iterators, such as &lt;code&gt;std::vector&lt;/code&gt; or classical arrays. However, a different approach is needed for other containers, such as &lt;code&gt;std::list&lt;/code&gt;, which support only bidirectional iterators.&lt;/p&gt;

&lt;p&gt;The background is that the &lt;code&gt;std::sort&lt;/code&gt; algorithms only perform with random access iterators: internally it uses an algorithm such as quick sort or merge sort, or some derivation of these, which only works well with random access. However, a bidirectional container still enables a different implementation of sorting algorithms.&lt;/p&gt;

&lt;p&gt;This is how we implement this functionality for linked lists. The complete code looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;list&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lst&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Example using a list&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;remove_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notably, this approach relies on two member functions of &lt;code&gt;std::list&lt;/code&gt;, namely &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;unique&lt;/code&gt;. While sorting works completely analogously to the case of &lt;code&gt;std::vector&lt;/code&gt; and other random access containers, the class method &lt;code&gt;std::unique&lt;/code&gt; has strikingly different behavior: this method now really erases all duplicate elements, instead of just moving them to the end of the container.&lt;/p&gt;

&lt;h2&gt;
  
  
  See also
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/algorithm/sort" rel="noopener noreferrer"&gt;std::sort&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/algorithm/unique" rel="noopener noreferrer"&gt;std::unique&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/container/list/sort" rel="noopener noreferrer"&gt;std::list::sort&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.cppreference.com/w/cpp/container/list/unique" rel="noopener noreferrer"&gt;std::list::unique&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Local functions in standard C++ (kind of...)</title>
      <dc:creator>Martin Licht</dc:creator>
      <pubDate>Mon, 17 Mar 2025 12:14:20 +0000</pubDate>
      <link>https://dev.to/martinlicht/local-functions-in-standard-c-kind-of-hd6</link>
      <guid>https://dev.to/martinlicht/local-functions-in-standard-c-kind-of-hd6</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Local functions are not allowed in C++, but static public member functions of local classes are.&lt;/p&gt;

&lt;p&gt;We occasionally need to define little auxiliary functions that are only used in a specific code segment, perhaps only once. A textbook example occurs when we use the C &lt;code&gt;qsort&lt;/code&gt; function, which requires a pointer to a separate comparison function. A standard code example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;compare_ints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="n"&gt;qsort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;compare_ints&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That we need to introduce the auxiliary function at the global scope of the translation unit is somewhat unfortunate. In the above example, we have added &lt;code&gt;static&lt;/code&gt; to prevent the function from being externally visible, that is, to other translation units, but it nevertheless remains visible at the file scope, even though such functions are typically only used in some specific situations.&lt;/p&gt;

&lt;p&gt;Can we avoid this namespace pollution?&lt;/p&gt;

&lt;p&gt;Some programming languages, such as JavaScript, allow the definition of &lt;strong&gt;local function&lt;/strong&gt; within code segments. In other words, these languages permit the nested definition of functions. This is not available in standard C but exists as a language extension in some compilers. &lt;br&gt;
For example, consider the following code segment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;compare_ints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Not allowed in standard C or C++&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;qsort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;compare_ints&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;```&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;While&lt;/span&gt; &lt;span class="n"&gt;GCC&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="n"&gt;such&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;language&lt;/span&gt; &lt;span class="n"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html), &lt;/span&gt;
&lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;above&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;never&lt;/span&gt; &lt;span class="n"&gt;been&lt;/span&gt; &lt;span class="n"&gt;permissible&lt;/span&gt; &lt;span class="n"&gt;standard&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;historical&lt;/span&gt; &lt;span class="n"&gt;rationale&lt;/span&gt; &lt;span class="n"&gt;might&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;been&lt;/span&gt; &lt;span class="n"&gt;lost&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Perhaps&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;relic&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="n"&gt;when&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt; &lt;span class="n"&gt;had&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;declared&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;beginning&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;adding&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="n"&gt;was&lt;/span&gt; &lt;span class="n"&gt;deemed&lt;/span&gt; &lt;span class="n"&gt;too&lt;/span&gt; &lt;span class="n"&gt;complicated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="n"&gt;perhaps&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;presence&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="n"&gt;would&lt;/span&gt; &lt;span class="n"&gt;complicate&lt;/span&gt; &lt;span class="n"&gt;compiler&lt;/span&gt; &lt;span class="n"&gt;design&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="n"&gt;famously&lt;/span&gt; &lt;span class="n"&gt;prides&lt;/span&gt; &lt;span class="n"&gt;itself&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;being&lt;/span&gt; &lt;span class="n"&gt;implementable&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;rather&lt;/span&gt; &lt;span class="n"&gt;tiny&lt;/span&gt; &lt;span class="n"&gt;compilers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;However&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;existence&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;such&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt; &lt;span class="n"&gt;extensions&lt;/span&gt; &lt;span class="n"&gt;shows&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;there&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;been&lt;/span&gt; &lt;span class="n"&gt;persistent&lt;/span&gt; &lt;span class="n"&gt;interest&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;concept&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 

&lt;span class="n"&gt;We&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;focus&lt;/span&gt; &lt;span class="n"&gt;too&lt;/span&gt; &lt;span class="n"&gt;much&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Instead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;discuss&lt;/span&gt; &lt;span class="n"&gt;how&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;facilitates&lt;/span&gt; &lt;span class="n"&gt;de&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;facto&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;standard&lt;/span&gt; &lt;span class="n"&gt;conforming&lt;/span&gt; &lt;span class="n"&gt;manner&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;almost&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;syntactic&lt;/span&gt; &lt;span class="n"&gt;overhead&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Importantly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;these&lt;/span&gt; &lt;span class="n"&gt;de&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;facto&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="n"&gt;advantage&lt;/span&gt; &lt;span class="n"&gt;over&lt;/span&gt; &lt;span class="n"&gt;lambdas&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;they&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;pointers&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 

&lt;span class="cp"&gt;## Local Type Definitions in C and C++
&lt;/span&gt;
&lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="n"&gt;lesser&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;known&lt;/span&gt; &lt;span class="n"&gt;feature&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;both&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;these&lt;/span&gt; &lt;span class="n"&gt;languages&lt;/span&gt; &lt;span class="n"&gt;allow&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="n"&gt;definitions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;applies&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;typedef&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;endraw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;following&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;perfectly&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="err"&gt;```&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;RED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GREEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BLUE&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Point: (%d, %d)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same is true for C++, where we can easily define classes in the local scope of our functions. For example, the following is permitted in C++:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Point: ("&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;", "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;")&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Static Member Functions in Local Classes
&lt;/h2&gt;

&lt;p&gt;Such local classes in C++ have only some minor restrictions in comparison to classes defined within namespaces. &lt;br&gt;
The most significant one seems to be that they cannot have static data members.&lt;br&gt;
That restriction seems tolerable because we can replace static data members by local static variables with minimal notational effort.&lt;/p&gt;

&lt;p&gt;The key feature, though, is the possibility of &lt;strong&gt;static member functions&lt;/strong&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;sayHelloClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hello from a static member function!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;sayHelloClass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For all intents and purposes, the static member function of the local class satisfies the same functionality as a local function would.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using a Local Struct for &lt;code&gt;qsort&lt;/code&gt; in C
&lt;/h2&gt;

&lt;p&gt;Coming back to our original problem of providing a function pointer to the C standard &lt;code&gt;qsort&lt;/code&gt; function without an auxiliary function clogging the namespace of the translation unit, we find the following solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sort_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Comparator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;int_b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;qsort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Comparator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="n"&gt;sort_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Comparator::compare&lt;/code&gt; is assigned to the function pointer argument of C's &lt;code&gt;qsort&lt;/code&gt; function. There is no global namespace pollution because the static member function is only visible where the local class is visible: within the function &lt;code&gt;sort_array&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The above example shows how local functions can effectively be defined within C++ without the use of lambdas, the most important application being the interface with C functions that expect function pointers. The only syntactical overhead is the definition of a local auxiliary class. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using a Local Struct for &lt;code&gt;std::sort&lt;/code&gt; in C++
&lt;/h2&gt;

&lt;p&gt;For the sake of the comparison, let us have a look at the C++ standard sort function.&lt;br&gt;
This function, too, accepts a function pointer for the comparison function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sort_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Comparator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;Comparator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;sort_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, we can use a lambda with little effort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this last example seems to be the modern C++ way of solving the problem, at least according to the enthusiasts of modern C++, lambdas cannot be used as function pointers, which forecloses the possibility of using them as auxiliary functions that interface with C libraries. &lt;/p&gt;

&lt;h2&gt;
  
  
  Local functions in C or C++ ... ever?
&lt;/h2&gt;

&lt;p&gt;Will C++ ever introduce true local functions? &lt;br&gt;
This seems to be unlikely for several reasons. &lt;br&gt;
C has left out local functions for decades and probably will continue to do in order to keep the language simple. &lt;br&gt;
C++ will probably not take such a big step that alienates it from C at the core foundations of the language. Moreover, if one stays within the C++ ecosystem, then lambdas seem to be a perfectly workable alternative. &lt;/p&gt;

&lt;p&gt;The likely applications of the static-member-function-of-local-class trick are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interfacing with C libraries that expect function pointers, where lambdas are not permitted&lt;/li&gt;
&lt;li&gt;Very unlikely but possible: highly performance-critical sections with numerous small auxiliary functions, where the overhead of lambdas is too much&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main point is that even without language extensions, we have a standard way of defining local auxiliary functions within the code. &lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
  </channel>
</rss>
