<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C on Cheat Sheet</title><link>/tags/c/</link><description>Recent content in C on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/c/index.xml" rel="self" type="application/rss+xml"/><item><title>File I/O</title><link>/linux/system-programming/file-io/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/file-io/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 4 (File I/O: The Universal I/O Model) and Chapter 5 (File I/O: Further Details).
See &lt;a href="../../programming/go/std/io/"&gt;Go io package&lt;/a&gt; for equivalent abstractions (&lt;code&gt;io.Reader&lt;/code&gt;, &lt;code&gt;io.Writer&lt;/code&gt;, &lt;code&gt;io.Seeker&lt;/code&gt;) in Go.&lt;/p&gt;
&lt;h2 id="core-function-signatures"&gt;Core Function Signatures&lt;a class="anchor" href="#core-function-signatures"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All I/O operations on regular files, devices, pipes, sockets, and terminals use these core system calls.&lt;/p&gt;
&lt;h3 id="1-open"&gt;1. open&lt;a class="anchor" href="#1-open"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Opens an existing file or creates a new file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;#include&lt;/span&gt; &lt;span style="color:#57606a"&gt;&amp;lt;fcntl.h&amp;gt;&lt;/span&gt;&lt;span style="color:#57606a"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#6639ba"&gt;open&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;const&lt;/span&gt; &lt;span style="color:#cf222e"&gt;char&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;pathname&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; flags&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#1f2328"&gt;...&lt;/span&gt; &lt;span style="color:#57606a"&gt;/* mode_t mode */&lt;/span&gt;&lt;span style="color:#1f2328"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Return:&lt;/strong&gt; New file descriptor (non-negative integer) on success; &lt;code&gt;-1&lt;/code&gt; on error (&lt;code&gt;errno&lt;/code&gt; set)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameters:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;pathname&amp;gt;&lt;/code&gt;: Path to the target file&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;flags&amp;gt;&lt;/code&gt;: Bitwise OR of access mode and status flags&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;mode&amp;gt;&lt;/code&gt;: Permissions for newly created file (&lt;code&gt;0644&lt;/code&gt;, &lt;code&gt;0600&lt;/code&gt;, etc.), required only when &lt;code&gt;O_CREAT&lt;/code&gt; is specified&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="access-modes-specify-exactly-one"&gt;Access Modes (Specify exactly one)&lt;a class="anchor" href="#access-modes-specify-exactly-one"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;O_RDONLY&lt;/code&gt;: Open for read-only&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_WRONLY&lt;/code&gt;: Open for write-only&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_RDWR&lt;/code&gt;: Open for reading and writing&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="common-creation-and-status-flags"&gt;Common Creation and Status Flags&lt;a class="anchor" href="#common-creation-and-status-flags"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;O_CREAT&lt;/code&gt;: Create file if it does not exist&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_EXCL&lt;/code&gt;: Used with &lt;code&gt;O_CREAT&lt;/code&gt;; fail atomically if file already exists (&lt;code&gt;EEXIST&lt;/code&gt;). Prevents Time-of-Check to Time-of-Use (TOCTOU) race conditions when creating lock files (&lt;code&gt;app.lock&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_TRUNC&lt;/code&gt;: Truncate existing file length to zero&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_APPEND&lt;/code&gt;: Atomically updates file offset to end of file before each write. Prevents race conditions between multiple processes appending to the same file (eliminates &lt;code&gt;lseek(SEEK_END)&lt;/code&gt; race conditions)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_NONBLOCK&lt;/code&gt;: Open in non-blocking mode (useful for FIFOs, pipes, and sockets)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_CLOEXEC&lt;/code&gt;: Enable close-on-exec flag atomically. Prevents file descriptor leakage across &lt;code&gt;exec&lt;/code&gt; calls to child programs (stored in the process-private FD table)&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id="2-read"&gt;2. read&lt;a class="anchor" href="#2-read"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Reads data from an open file descriptor into a memory buffer.&lt;/p&gt;</description></item><item><title>Pointers and Arrays</title><link>/programming/c/pointer/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/pointer/</guid><description>&lt;h1 id="pointers-and-arrays"&gt;Pointers and Arrays&lt;a class="anchor" href="#pointers-and-arrays"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="pointer-and-array"&gt;Pointer and Array&lt;a class="anchor" href="#pointer-and-array"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="pointer-decay"&gt;Pointer Decay&lt;a class="anchor" href="#pointer-decay"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In C, an array expression in most contexts automatically converts (&amp;ldquo;decays&amp;rdquo;) into a pointer to its first element.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When an array is passed as an argument to a function, what is actually passed is a pointer to its first element.&lt;/li&gt;
&lt;li&gt;The following function parameter declarations have identical signatures:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;void&lt;/span&gt; &lt;span style="color:#6639ba"&gt;f&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; arr&lt;span style="color:#1f2328"&gt;[]);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;void&lt;/span&gt; &lt;span style="color:#6639ba"&gt;f&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; arr&lt;span style="color:#1f2328"&gt;[&lt;/span&gt;&lt;span style="color:#0550ae"&gt;10&lt;/span&gt;&lt;span style="color:#1f2328"&gt;]);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;void&lt;/span&gt; &lt;span style="color:#6639ba"&gt;f&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;arr&lt;span style="color:#1f2328"&gt;);&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Adjusted signature
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="multidimensional-array-parameters"&gt;Multidimensional Array Parameters&lt;a class="anchor" href="#multidimensional-array-parameters"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When passing a multidimensional array to a function, all dimensions except the first must be explicitly specified:&lt;/p&gt;</description></item><item><title>Syntax Basics</title><link>/programming/c/syntax-basics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/syntax-basics/</guid><description>&lt;h1 id="syntax-basics"&gt;Syntax Basics&lt;a class="anchor" href="#syntax-basics"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="declaration-vs-definition"&gt;Declaration vs. Definition&lt;a class="anchor" href="#declaration-vs-definition"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C, declaration and definition are distinct compiler operations governing name resolution and memory allocation.&lt;/p&gt;
&lt;h3 id="declaration"&gt;Declaration&lt;a class="anchor" href="#declaration"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A declaration informs the compiler of the name and type of an entity without allocating storage.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Introduces an identifier to the compiler so it can perform type checking.&lt;/li&gt;
&lt;li&gt;An entity can be declared multiple times across translation units.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;extern&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; x&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Variable x is defined elsewhere; no storage allocated here
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#6639ba"&gt;Func&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; a&lt;span style="color:#1f2328"&gt;);&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Function prototype declaration; no code generated
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="definition"&gt;Definition&lt;a class="anchor" href="#definition"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A definition provides the actual storage (for variables) or executable code (for functions).&lt;/p&gt;</description></item><item><title>static</title><link>/programming/c/static/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/static/</guid><description>&lt;h1 id="static"&gt;&lt;code&gt;static&lt;/code&gt;&lt;a class="anchor" href="#static"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Depending on where it appears, &lt;code&gt;static&lt;/code&gt; in C controls either &lt;strong&gt;cross-file visibility (linkage)&lt;/strong&gt; or &lt;strong&gt;variable lifetime and memory placement&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id="1-file-scope-static-internal-linkage"&gt;1. File-Scope &lt;code&gt;static&lt;/code&gt;: Internal Linkage&lt;a class="anchor" href="#1-file-scope-static-internal-linkage"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Applying &lt;code&gt;static&lt;/code&gt; to a global variable or function at file scope gives it &lt;strong&gt;internal linkage&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Visible only within the current translation unit (&lt;code&gt;.c&lt;/code&gt; file); &lt;code&gt;extern&lt;/code&gt; declarations in other files cannot link against it.&lt;/li&gt;
&lt;li&gt;Used to encapsulate module-private state and helper functions.&lt;/li&gt;
&lt;li&gt;Stored in the static data segment (&lt;code&gt;.data&lt;/code&gt; / &lt;code&gt;.bss&lt;/code&gt;) for the entire duration of the program, just like ordinary globals.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;static&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; internal_count &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;0&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Accessible only within this .c file
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;static&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#6639ba"&gt;Helper&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; x&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Private helper hidden from other translation units
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; x &lt;span style="color:#0550ae"&gt;*&lt;/span&gt; &lt;span style="color:#0550ae"&gt;2&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id="2-local-static-static-storage-duration-in-the-data-segment"&gt;2. Local &lt;code&gt;static&lt;/code&gt;: Static Storage Duration in the Data Segment&lt;a class="anchor" href="#2-local-static-static-storage-duration-in-the-data-segment"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;static&lt;/code&gt; variable declared inside a function or block is not destroyed when the function returns; &lt;strong&gt;its value persists until the program terminates&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Bit Manipulation</title><link>/programming/c/bit-manipulation/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/bit-manipulation/</guid><description>&lt;h1 id="bit-manipulation"&gt;Bit Manipulation&lt;a class="anchor" href="#bit-manipulation"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;link rel="stylesheet" href="../../katex/katex.min.css" /&gt;&lt;script defer src="../../katex/katex.min.js"&gt;&lt;/script&gt;&lt;script defer src="../../katex/auto-render.min.js" onload="renderMathInElement(document.body, {&amp;#34;delimiters&amp;#34;:[{&amp;#34;left&amp;#34;:&amp;#34;$$&amp;#34;,&amp;#34;right&amp;#34;:&amp;#34;$$&amp;#34;,&amp;#34;display&amp;#34;:true},{&amp;#34;left&amp;#34;:&amp;#34;$&amp;#34;,&amp;#34;right&amp;#34;:&amp;#34;$&amp;#34;,&amp;#34;display&amp;#34;:false},{&amp;#34;left&amp;#34;:&amp;#34;\\(&amp;#34;,&amp;#34;right&amp;#34;:&amp;#34;\\)&amp;#34;,&amp;#34;display&amp;#34;:false},{&amp;#34;left&amp;#34;:&amp;#34;\\[&amp;#34;,&amp;#34;right&amp;#34;:&amp;#34;\\]&amp;#34;,&amp;#34;display&amp;#34;:true}]});"&gt;&lt;/script&gt;
&lt;p&gt;Based on &lt;em&gt;The C Programming Language (TCPL), 2nd ed.&lt;/em&gt; Section 2.9 (Bitwise Operators), p. 50.&lt;/p&gt;
&lt;h2 id="bit-counting-population-count"&gt;Bit Counting (Population Count)&lt;a class="anchor" href="#bit-counting-population-count"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Counting the number of 1-bits in an unsigned integer.&lt;/p&gt;
&lt;h3 id="version-1-linear-bit-shift"&gt;Version 1: Linear Bit Shift&lt;a class="anchor" href="#version-1-linear-bit-shift"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Shifts bits rightward one by one and masks the lowest bit with &lt;code&gt;01&lt;/code&gt; (octal literal for 1):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#6639ba"&gt;BitCountLinear&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;unsigned&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; x&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; b &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;0&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;for&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(;&lt;/span&gt; x &lt;span style="color:#0550ae"&gt;!=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;0&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; x &lt;span style="color:#0550ae"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;1&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;if&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;x &lt;span style="color:#0550ae"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color:#0550ae"&gt;01&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; b&lt;span style="color:#0550ae"&gt;++&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; b&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; $O(N)$ where $N$ is the number of bits in the integer type (eg. up to 32 iterations).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="version-2-brian-kernighans-algorithm-x--x---1"&gt;Version 2: Brian Kernighan&amp;rsquo;s Algorithm (&lt;code&gt;x &amp;amp;= (x - 1)&lt;/code&gt;)&lt;a class="anchor" href="#version-2-brian-kernighans-algorithm-x--x---1"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Clears the rightmost 1-bit on each iteration:&lt;/p&gt;</description></item><item><title>Process</title><link>/linux/system-programming/process/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/process/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 6 (Processes), Chapter 24 (Process Creation), Chapter 25 (Process Termination), Chapter 26 (Monitoring Child Processes), and Chapter 27 (Program Execution).&lt;/p&gt;
&lt;p&gt;The Linux process lifecycle is governed by the combination of four core system calls:&lt;/p&gt;
&lt;span class="book-katex" &gt;\( 
\mathbf{fork()} \longrightarrow \mathbf{execve()} \longrightarrow \mathbf{exit()} / \mathbf{\_exit()} \longrightarrow \mathbf{waitpid()}
 \)&lt;/span&gt;
&lt;hr&gt;
&lt;h2 id="1-process-virtual-memory-layout-tlpi-ch-6"&gt;1. Process Virtual Memory Layout (TLPI Ch 6)&lt;a class="anchor" href="#1-process-virtual-memory-layout-tlpi-ch-6"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The kernel allocates an independent virtual address space for each process, structured from low to high addresses as follows:&lt;/p&gt;</description></item><item><title>C vs C++</title><link>/programming/c/c-vs-cpp/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/c-vs-cpp/</guid><description>&lt;h1 id="c-vs-c"&gt;C vs C++&lt;a class="anchor" href="#c-vs-c"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="type-and-tag-namespaces"&gt;Type and Tag Namespaces&lt;a class="anchor" href="#type-and-tag-namespaces"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C and C++, user-defined types are placed into different compiler namespaces, affecting whether type keywords can be omitted.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Type&lt;/th&gt;
 &lt;th style="text-align: left"&gt;C Syntax&lt;/th&gt;
 &lt;th style="text-align: left"&gt;C++ Syntax&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;struct&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;struct A a;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;A a;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;union&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;union U u;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;U u;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;enum&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;enum Color c;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;Color c;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;class&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Not supported&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;MyClass obj;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="c-separate-tag-and-identifier-namespaces"&gt;C: Separate Tag and Identifier Namespaces&lt;a class="anchor" href="#c-separate-tag-and-identifier-namespaces"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In C, names of &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, and &lt;code&gt;enum&lt;/code&gt; reside in a dedicated &lt;strong&gt;tag namespace&lt;/strong&gt;, distinct from ordinary identifiers.&lt;/li&gt;
&lt;li&gt;Without a &lt;code&gt;typedef&lt;/code&gt;, variable declarations must include the defining keyword:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; Node &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; val&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; Node n&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// &amp;#39;struct&amp;#39; keyword required in C
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="c-unified-type-namespace"&gt;C++: Unified Type Namespace&lt;a class="anchor" href="#c-unified-type-namespace"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In C++, names introduced by &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, or &lt;code&gt;enum&lt;/code&gt; automatically enter the &lt;strong&gt;type namespace&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The type name can be used directly without a &lt;code&gt;typedef&lt;/code&gt; or prefix keyword:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; &lt;span style="color:#1f2328"&gt;Node&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; val&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Node n&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Valid in C++ without typedef
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="string-literal-typing"&gt;String Literal Typing&lt;a class="anchor" href="#string-literal-typing"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Language&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Literal Type&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;code&gt;char *s = &amp;quot;hello&amp;quot;;&lt;/code&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Modification Attempt&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;C&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;char[]&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Valid (Legacy compatibility)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime crash (Segmentation fault)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;C++&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;const char[]&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile Error&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Prevented at compile time&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;C:&lt;/strong&gt; String literals are stored in read-only memory, but their formal type is non-const &lt;code&gt;char[]&lt;/code&gt;. Modifying a literal triggers undefined behavior at runtime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;C++:&lt;/strong&gt; String literals have type &lt;code&gt;const char[]&lt;/code&gt; (decaying to &lt;code&gt;const char *&lt;/code&gt;). The compiler prevents modifications and invalid assignments at compile time (enforcing low-level const).&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="references-vs-pointers"&gt;References vs. Pointers&lt;a class="anchor" href="#references-vs-pointers"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++ introduces references (&lt;code&gt;&amp;amp;&lt;/code&gt;), which do not exist in C.&lt;/p&gt;</description></item><item><title>Shared Libraries</title><link>/linux/system-programming/shared-libraries/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/shared-libraries/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 41 (Fundamentals of Shared Libraries) and Chapter 42 (Advanced Features of Shared Libraries).&lt;/p&gt;
&lt;h1 id="shared-libraries"&gt;Shared Libraries&lt;a class="anchor" href="#shared-libraries"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;hr&gt;
&lt;h2 id="1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;1. Header vs Static vs Shared Library (TLPI Ch 41)&lt;a class="anchor" href="#1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C libraries (eg. &lt;code&gt;glibc&lt;/code&gt;) separate compile-time declarations from link-time and runtime binaries.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Artifact&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Extension &amp;amp; Example Path&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Role&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Phase&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Header&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.h&lt;/code&gt; (&lt;code&gt;/usr/include/dlfcn.h&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Plain-text function signatures, types, and macros&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile time (&lt;code&gt;gcc -c&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Static Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.a&lt;/code&gt; (&lt;code&gt;libc.a&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Archive of &lt;code&gt;.o&lt;/code&gt; object files copied directly into the executable&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Link time (&lt;code&gt;gcc -static&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Shared Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.so&lt;/code&gt; (&lt;code&gt;/lib/x86_64-linux-gnu/libc.so.6&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Precompiled Position-Independent machine code loaded once into memory and shared across processes&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime (&lt;code&gt;ld.so&lt;/code&gt; dynamic linker)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="building-and-inspecting-shared-libraries"&gt;Building and Inspecting Shared Libraries&lt;a class="anchor" href="#building-and-inspecting-shared-libraries"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ gcc -fPIC -shared -Wall -o libfoo.so foo.c
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ ldd &amp;lt;binary&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ nm -D libfoo.so&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-fPIC&lt;/code&gt;: Generate Position-Independent Code so the library can be mapped at any virtual address&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-shared&lt;/code&gt;: Produce a shared object (&lt;code&gt;.so&lt;/code&gt;) instead of an executable requiring &lt;code&gt;main&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ldd&lt;/code&gt;: List shared libraries required by a binary and their resolved paths&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nm -D&lt;/code&gt;: Inspect exported dynamic symbols in a &lt;code&gt;.so&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;2. Dynamic Loading API: &lt;code&gt;dlopen&lt;/code&gt; &amp;amp; &lt;code&gt;dlsym&lt;/code&gt; (TLPI Ch 42)&lt;a class="anchor" href="#2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;glibc&lt;/code&gt; API for loading shared libraries into memory and resolving symbol addresses dynamically &lt;strong&gt;at runtime&lt;/strong&gt; rather than at program startup (&lt;code&gt;man 3 dlopen&lt;/code&gt;, &lt;code&gt;man 3 dlsym&lt;/code&gt;). The &lt;strong&gt;&lt;code&gt;RTLD_&lt;/code&gt;&lt;/strong&gt; prefix stands for &lt;strong&gt;R&lt;/strong&gt;un-&lt;strong&gt;T&lt;/strong&gt;ime &lt;strong&gt;L&lt;/strong&gt;ink-&lt;strong&gt;D&lt;/strong&gt;ynamic.&lt;/p&gt;</description></item><item><title>stdatomic</title><link>/programming/c/stdatomic/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/stdatomic/</guid><description>&lt;h1 id="stdatomic"&gt;&lt;code&gt;stdatomic&lt;/code&gt;&lt;a class="anchor" href="#stdatomic"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The C11 standard (&lt;code&gt;&amp;lt;stdatomic.h&amp;gt;&lt;/code&gt;) provides hardware-level atomic memory operations and a formal memory model directly in the language without platform-specific inline assembly.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-why-atomic-operations-are-needed"&gt;1. Why Atomic Operations Are Needed&lt;a class="anchor" href="#1-why-atomic-operations-are-needed"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Concurrent reads and writes to ordinary variables across multiple threads or &lt;code&gt;mmap&lt;/code&gt; shared-memory processes introduce two fundamental hazards:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lost Update&lt;/strong&gt;: An increment such as &lt;code&gt;total += 100&lt;/code&gt; compiles into three separate machine steps: 1) memory &lt;code&gt;Load&lt;/code&gt;, 2) register &lt;code&gt;Add&lt;/code&gt;, and 3) memory &lt;code&gt;Store&lt;/code&gt;. If two threads read &lt;code&gt;1000&lt;/code&gt; at the same time, both store &lt;code&gt;1100&lt;/code&gt;, losing one increment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Torn Read&lt;/strong&gt;: If one thread reads a 64-bit variable while another thread is halfway through updating it, the reader can observe a corrupted value combining the old upper 32 bits with the new lower 32 bits.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Atomic operations use CPU hardware instructions (eg. x86 &lt;code&gt;LOCK XADD&lt;/code&gt;, ARM &lt;code&gt;LDADD&lt;/code&gt;) to execute the access as a &lt;strong&gt;single indivisible step&lt;/strong&gt;, preventing both Lost Updates and Torn Reads.&lt;/p&gt;</description></item><item><title>Comparison</title><link>/programming/comparison/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/comparison/</guid><description>&lt;h1 id="comparison"&gt;Comparison&lt;a class="anchor" href="#comparison"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;h2 id="generics"&gt;Generics&lt;a class="anchor" href="#generics"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Feature&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Go&lt;/th&gt;
 &lt;th style="text-align: left"&gt;C++&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Paradigm&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Parametric Polymorphism&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Metaprogramming&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Interface-based (Type Sets)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Concepts (C++20), SFINAE&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Specialization&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Not Supported&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Supported&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Metaprogramming&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Not Supported&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Turing-complete&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;div class="book-tabs" &gt;
&lt;input type="radio" class="toggle" name="tabs-6" id="tabs-6-0" checked="checked" /&gt;&lt;label for="tabs-6-0"&gt;Go&lt;/label&gt;&lt;div class="book-tabs-content markdown-inner"&gt;
&lt;p&gt;Go generics focus on simplicity and clarity. They use type parameters constrained by interfaces.&lt;/p&gt;
&lt;h3 id="generic-function"&gt;Generic Function&lt;a class="anchor" href="#generic-function"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;eg.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// T is constrained by &amp;#39;any&amp;#39;&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;func&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;Print&lt;/span&gt;&lt;span style="color:#1f2328"&gt;[&lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;any&lt;/span&gt;&lt;span style="color:#1f2328"&gt;](&lt;/span&gt;&lt;span style="color:#1f2328"&gt;s&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;[]&lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;{&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;for&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;_&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;v&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0550ae"&gt;:=&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;range&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;s&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;{&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;fmt&lt;/span&gt;&lt;span style="color:#1f2328"&gt;.&lt;/span&gt;&lt;span style="color:#6639ba"&gt;Print&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#1f2328"&gt;v&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34; &amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// T is constrained by &amp;#39;comparable&amp;#39; (supports == and !=)&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;func&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;Find&lt;/span&gt;&lt;span style="color:#1f2328"&gt;[&lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;comparable&lt;/span&gt;&lt;span style="color:#1f2328"&gt;](&lt;/span&gt;&lt;span style="color:#1f2328"&gt;s&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;[]&lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;val&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;T&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;{&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;for&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;i&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;v&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0550ae"&gt;:=&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;range&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;s&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;{&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;if&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;v&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0550ae"&gt;==&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;val&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;{&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;return&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;i&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#cf222e"&gt;return&lt;/span&gt;&lt;span style="color:#fff"&gt; &lt;/span&gt;&lt;span style="color:#0550ae"&gt;-&lt;/span&gt;&lt;span style="color:#0550ae"&gt;1&lt;/span&gt;&lt;span style="color:#fff"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="data-structure"&gt;Data Structure&lt;a class="anchor" href="#data-structure"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Syntax: &lt;code&gt;type STRUCT_NAME[&amp;lt;type parameter&amp;gt; &amp;lt;type constraint&amp;gt;] struct {}&lt;/code&gt;&lt;/p&gt;</description></item></channel></rss>