# ansi c

Low-level programming langauge originally part of Unix.

- Page: https://oh4.co/site/ansi-c.html
- Under: [programming languages](programming-languages.md)
- Last change: 7EAB0D (2026-11-13)

C is the <em>lingua franca</em> of the software world as so much is written in it. Any serious programmer should have significant familiarity with C.

C is worth learning as an aid to thinking about problems at the hardware and operating system levels - though it is still an abstract machine on top of the real hardware.

<h4>Style</h4>

<p>ANSI C89 compliant code is preferred for portability. Here are a few standard conventions:</p>

<h5>General</h5>

<ul>
  <li>use <code>/* */</code> comment pairs, never <code>//</code></li>
  <li>no tabs, expand to spaces. 2 spaces per indentation level</li>
  <li>no whitespace after the keywords <code>if</code>, <code>for</code>, and <code>while</code>.</li>
  <li>no braces around single-line blocks (e.g., <code>if</code>, <code>for</code>, and <code>while</code> bodies)</li>
  <li>integer valued functions return -1 on error, 0 or positive on success</li>
  <li><code>enum</code> and <code>#define</code>ed constants should be <code>UPPERCASE</code></li>
  <li><code>struct</code> tags are <code>Underscore_Capitalized</code>, with matching typedefs</li>
  <li>function and variable names are compact like <code>krninit</code> when possible, <code>underscore_lowercased</code> when clarity demands</li>
  <li>automatic variables (local variables inside a function) are never initialized at declaration</li>
  <li>don't write <code>!strcmp</code> (or <code>!memcmp</code>, etc.); always explicitly compare the result fo string or memory comparison with zero</li>
  <li>include files should not include other files (prevents circular dependencies and keeps compilation fast)</li>
</ul>

<h5>Comments</h5>

<ul>
  <li>Code should be readable and require few comments. Functions should have a
  line or two comment above explaining their purpose (even if redundant, as the
    purpose may not always be clear to others).</li>
  <li>Every field of a struct should have a corresponding comment</li>
  <li>Low-level code should be verbosely commented and when possible include
  links to reference materials. This can prevent a lot of wasteful churn
  between code, reference materials, and search engines when reading.</li>
</ul>

<h5>Libraries</h5>

<p>The C language does not provide a library system for larger-scale code
reuse. However, one powerful - the single header file library - has emerged as
an excellent way to integrate with and manage dependencies. Single header file
libraries are our preferred method for sharing and reusing code across
projects.</p>

<ul>
  <li><a href="https://github.com/nothings/stb" target="_blank">Sean Barrett's stb libraries</a></li>
  <li><a href="https://github.com/mattiasgustavsson/libs" target="_blank">Mattias Gustavsson's single-header libraries</a></li>
  <li><a href="https://github.com/nothings/single_file_libs#multithreading" target="_blank">A curated collection of single-header libraries</a></li>
  <li><a href="https://github.com/RandyGaul/cute_headers" target="_blank">cute headers</a></li>
</ul>

## Links

- [tinycc git repo](https://repo.or.cz/w/tinycc.git)
- [plan9 style guide](https://plan9.io/magic/man2html/6/style)

## Linked from

- [web](web.md)
