blob: 7353c0d1056fd3f2d7443594ff1f3213f20959e5 [file] [log] [blame]
<!--
Documentation/_templates/layout.html
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. The
ASF licenses this file to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
-->
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C Coding Standard &mdash; NuttX latest documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/sphinx_tabs/semantic-ui-2.4.1/segment.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/sphinx_tabs/semantic-ui-2.4.1/menu.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/sphinx_tabs/semantic-ui-2.4.1/tab.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/sphinx_tabs/tabs.css" type="text/css" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Documentation" href="documentation.html" />
<link rel="prev" title="Making Changes Using Git" href="making-changes.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> NuttX
<img src="../_static/NuttX.png" class="logo" alt="Logo"/>
</a>
<!-- this version selector is quite ugly, should be probably replaced by something
more modern -->
<div class="version-selector">
<select onchange="javascript:location.href = this.value;">
<option value="../../latest" selected="selected">latest</option>
<option value="../../10.0.0" >10.0.0</option>
<option value="../../10.0.1" >10.0.1</option>
<option value="../../10.1.0" >10.1.0</option>
<option value="../../10.2.0" >10.2.0</option>
<option value="../../10.3.0" >10.3.0</option>
</select>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Table of Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Home</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/inviolables.html">The Inviolable Principles of NuttX</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/index.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../components/index.html">OS Components</a></li>
<li class="toctree-l1"><a class="reference internal" href="../applications/index.html">Applications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../boards/index.html">Supported Boards</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guides/index.html">Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="../releases/index.html">Releases</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Contributing</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="workflow.html">Development Workflow</a></li>
<li class="toctree-l2"><a class="reference internal" href="making-changes.html">Making Changes Using Git</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">C Coding Standard</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#general-conventions">General Conventions</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#file-organization">File Organization</a></li>
<li class="toctree-l4"><a class="reference internal" href="#lines">Lines</a></li>
<li class="toctree-l4"><a class="reference internal" href="#comments">Comments</a></li>
<li class="toctree-l4"><a class="reference internal" href="#braces">Braces</a></li>
<li class="toctree-l4"><a class="reference internal" href="#indentation">Indentation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#parentheses">Parentheses</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#data-and-type-definitions">Data and Type Definitions</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#one-definition-declaration-per-line">One Definition/Declaration Per Line</a></li>
<li class="toctree-l4"><a class="reference internal" href="#global-variables">Global Variables</a></li>
<li class="toctree-l4"><a class="reference internal" href="#parameters-and-local-variables">Parameters and Local Variables</a></li>
<li class="toctree-l4"><a class="reference internal" href="#type-definitions">Type Definitions</a></li>
<li class="toctree-l4"><a class="reference internal" href="#structures">Structures</a></li>
<li class="toctree-l4"><a class="reference internal" href="#unions">Unions</a></li>
<li class="toctree-l4"><a class="reference internal" href="#enumerations">Enumerations</a></li>
<li class="toctree-l4"><a class="reference internal" href="#c-pre-processor-macros">C Pre-processor Macros</a></li>
<li class="toctree-l4"><a class="reference internal" href="#pointer-variables">Pointer Variables</a></li>
<li class="toctree-l4"><a class="reference internal" href="#initializers">Initializers</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#functions">Functions</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#function-headers">Function Headers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#function-naming">Function Naming</a></li>
<li class="toctree-l4"><a class="reference internal" href="#parameter-lists">Parameter Lists</a></li>
<li class="toctree-l4"><a class="reference internal" href="#function-body">Function Body</a></li>
<li class="toctree-l4"><a class="reference internal" href="#returned-values">Returned Values</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#statements">Statements</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#one-statement-per-line">One Statement Per Line</a></li>
<li class="toctree-l4"><a class="reference internal" href="#casts">Casts</a></li>
<li class="toctree-l4"><a class="reference internal" href="#operators">Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="#if-then-else-statement"><code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">then</span> <span class="pre">else</span></code> Statement</a></li>
<li class="toctree-l4"><a class="reference internal" href="#switch-statement"><code class="docutils literal notranslate"><span class="pre">switch</span></code> Statement</a></li>
<li class="toctree-l4"><a class="reference internal" href="#while-statement"><code class="docutils literal notranslate"><span class="pre">while</span></code> Statement</a></li>
<li class="toctree-l4"><a class="reference internal" href="#do-while-statement"><code class="docutils literal notranslate"><span class="pre">do</span> <span class="pre">while</span></code> Statement</a></li>
<li class="toctree-l4"><a class="reference internal" href="#use-of-goto">Use of <code class="docutils literal notranslate"><span class="pre">goto</span></code></a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#c">C++</a></li>
<li class="toctree-l3"><a class="reference internal" href="#appendix">Appendix</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#c-source-file-structure">C Source File Structure</a></li>
<li class="toctree-l4"><a class="reference internal" href="#c-header-file-structure">C Header File Structure</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="documentation.html">Documentation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">NuttX</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li><a href="index.html">Contributing</a> &raquo;</li>
<li>C Coding Standard</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/contributing/coding_style.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="c-coding-standard">
<span id="coding-standard"></span><h1>C Coding Standard<a class="headerlink" href="#c-coding-standard" title="Permalink to this headline"></a></h1>
<p>NuttX follows a specific coding style which needs to be followed at all times
a contribution to be accepted. Please read this document before working on
new code so that you can follow the style from the start. To check your code
for conformance to the coding style, you should use the <a class="reference external" href="#nxstyle">nxstyle</a>
tool included under <code class="docutils literal notranslate"><span class="pre">tools/</span></code> in the main NuttX repository.</p>
<div class="section" id="general-conventions">
<h2>General Conventions<a class="headerlink" href="#general-conventions" title="Permalink to this headline"></a></h2>
<div class="section" id="file-organization">
<h3>File Organization<a class="headerlink" href="#file-organization" title="Permalink to this headline"></a></h3>
<p><strong>File Extensions</strong> Use the <code class="docutils literal notranslate"><span class="pre">.h</span></code> extension for C header files
and <code class="docutils literal notranslate"><span class="pre">.c</span></code> for C source files.</p>
<p><strong>File header</strong>. Every C, C++, make file, or script begins with a file header.
That file header is enclosed with a <em>block comment</em> (see below). Within the
block comment, the following must appear:</p>
<blockquote>
<div><ul class="simple">
<li><p>The relative path to the file from the top-level directory.</p></li>
<li><p>An optional, one-line description of the file contents.</p></li>
<li><p>A blank line</p></li>
<li><p>A copyright notice indented two additional spaces</p></li>
<li><p>A line identifying the author and contact information with the
same indentation as the copyright notice.</p></li>
<li><p>A blank line</p></li>
<li><p>NuttX standard Apache 2.0 licensing information as provided in
the <a class="reference external" href="#appndxa">appendix</a>.</p></li>
</ul>
</div></blockquote>
<p><strong>Sample File Headers</strong>. Sample file headers are provided in an
<a class="reference external" href="#appndxa">Appendix</a> to this document. No new software may be
included in the NuttX source tree that does not have licensing
information included in the file. No new software may be included
in the NuttX source tree that does not have a Apache 2.0 license
or license (or, in the case of 3rd party file, a compatible
license such as the BSD or MIT licenses). If the file does not
follow Apache 2.0 licensing, then the appropriate license
information should be provided in the header rather than the
Apache 2.0 licensing information and a NOTE should be included in
the top-level <code class="docutils literal notranslate"><span class="pre">COPYING</span></code> file to indicate any variations from
Apache 2.0 licensing.</p>
<p><strong>Grouping</strong>. All like components in a C source or header file are
grouped together. Definitions do not appear arbitrarily through
the file, rather, like definitions are grouped together and
preceded by a <em>block comment</em> identifying the grouping.</p>
<p><strong>Block Comments</strong>. Each grouping in the file is separated with a
<em>block comment</em>. The block comment consists of:</p>
<ul class="simple">
<li><p>A line that consists of the opening C comment (<code class="docutils literal notranslate"><span class="pre">/*</span></code>) followed
by a series of asterisks extending to the length of the line
(usually to column 78).</p></li>
<li><p>The name of the grouping, starting at column 4. An asterisk
preceives the name of the grouping in column 1.</p></li>
<li><p>A line that consists of the closing C comment (<code class="docutils literal notranslate"><span class="pre">*/</span></code>) at the
end of the line (usually column 78) preceded by a series of
asterisks extending to column 1.</p></li>
</ul>
<p><strong>Examples of Block Comments</strong>. See <a class="reference external" href="#appndxa">Appendix A</a> for
examples of block comments.</p>
<p><strong>Order of Groupings</strong>. The following groupings should appear in
all C source files in the following order:</p>
<blockquote>
<div><ol class="arabic simple">
<li><p>Included Files</p></li>
<li><p>Pre-processor Definitions</p></li>
<li><p>Private Types (definitions)</p></li>
<li><p>Private Function Prototypes (declarations)</p></li>
<li><p>Private Data (definitions)</p></li>
<li><p>Public Data (definitions)</p></li>
<li><p>Private Functions (definitions)</p></li>
<li><p>Public Functions (definitions)</p></li>
</ol>
</div></blockquote>
<p>The following groupings should appear in all C header files in the
following order:</p>
<blockquote>
<div><ol class="arabic simple">
<li><p>Included Files</p></li>
<li><p>Pre-processor Definitions</p></li>
<li><p>Public Types (definitions)</p></li>
<li><p>Public Data (declarations)</p></li>
<li><p>Inline Functions (definitions)</p></li>
<li><p>Public Function Prototypes (declarations)</p></li>
</ol>
</div></blockquote>
<p><strong>Large vs. Small Files</strong>. In larger files, block comments should
be included for all groupings, even if they are empty; the empty
grouping provides important information in itself. Smaller files
may omit some of the block comments; it is awkard if the block
comments are larger than the file content!</p>
<p><strong>Header File Idempotence</strong>. C header file must protect against
multiple inclusion through the use of macros that “guard” against
multiple definitions if the header file is included multiple
times.</p>
<ul>
<li><p>Each header file must contain the following pre-processor
conditional logic near the beginning of the header file:
Between the file header and the “Included Files” block comment.
For example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#ifndef __INCLUDE_NUTTX_ARCH_H
#define __INCLUDE_NUTTX_ARCH_H
</pre></div>
</div>
<p>Notice that the definitions within of the header do not follow
the usually rules: The presence of the conditional test at the
top of the file does not cause the remaining definitions within
the file to be indented.</p>
</li>
<li><p>Then conditional compilation is closed at the fine line of the
header file with:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#endif /* __INCLUDE_NUTTX_ARCH_H */
</pre></div>
</div>
</li>
</ul>
<p><strong>Forming Guard Names</strong>. Then pre-processor macro name used in the
guard is formed from the full, relative path to the header for
from the top-level, controlled directory. That path is preceded by
<code class="docutils literal notranslate"><span class="pre">__</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> replaces each character that would otherwise be
invalid in a macro name. So, for example, <code class="docutils literal notranslate"><span class="pre">__INCLUDE_NUTTX_ARCH_H</span></code>
corresponds to the header file <code class="docutils literal notranslate"><span class="pre">include/nuttx/arch.h</span></code></p>
<p><strong>Deoxygen Information</strong>. NuttX does not use Deoxygen for
documentation and no file should contain Doxygen tags or Doxygen
style comments.</p>
<p><strong>Sample File Formats</strong>. C source and header file templates are
provided in an <a class="reference external" href="#appndxa">Appendix</a> to this document.</p>
</div>
<div class="section" id="lines">
<h3>Lines<a class="headerlink" href="#lines" title="Permalink to this headline"></a></h3>
<p><strong>Line Endings</strong>. Files should be formatted with the newline
character (<code class="docutils literal notranslate"><span class="pre">\n</span></code>) as the line ending (Unix-style line endings)
and specifically <em>not</em> the carriage return, newline sequence
(<code class="docutils literal notranslate"><span class="pre">\r\n</span></code>) used with Windows-style line endings. There should be
no extra whitespace at the end of the line. In addition, all text
files should end in a single newline (<code class="docutils literal notranslate"><span class="pre">\n</span></code>). This avoids the
<em>“No newline at end of file”</em> warning generated by certain tools.</p>
<p><strong>Line Width</strong>. Text should not extend past column 78 in the
typical C source or header file. Sometimes the nature of the
content of a file may require that the lines exceed this limit.
This often occurs in header files with naturally long definitions.
If the line width must extend 78 lines, then some wider line width
may be used in the file provided that it is used consistently.</p>
<p><strong>Line Wrapping</strong>.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">some_long_struct_name_s</span>
<span class="p">{</span>
<span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="n">flink</span><span class="p">;</span> <span class="cm">/* The forward link to the next instance of struct some_long_struct_name_s in a singly linked list */</span>
<span class="kt">int</span> <span class="n">short_name1</span><span class="p">;</span> <span class="cm">/* Short comment 1 */</span>
<span class="kt">int</span> <span class="n">short_name2</span><span class="p">;</span> <span class="cm">/* This is a very long comment describing subtle aspects of the short_name2 field */</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">some_medium_name_s</span><span class="p">);</span>
<span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">some_long_struct_name_s</span><span class="p">);</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">some_function_with_many</span> <span class="n">parameters</span><span class="p">(</span><span class="n">long_parameter_name_1</span><span class="p">,</span> <span class="n">long_parameter_name_2</span><span class="p">,</span> <span class="n">long_parameter_name_3</span><span class="p">,</span> <span class="n">long_parameter_name_4</span><span class="p">,</span> <span class="n">long_parameter_name_5</span><span class="p">,</span> <span class="n">long_parameter_name_6</span><span class="p">,</span> <span class="n">long_parameter_name_7</span><span class="p">,</span> <span class="n">long_parameter_name_8</span><span class="p">);</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">some_function_with_many</span> <span class="n">parameters</span><span class="p">(</span><span class="n">long_parameter_name_1</span><span class="p">,</span>
<span class="n">long_parameter_name_2</span><span class="p">,</span>
<span class="n">long_parameter_name_3</span>
<span class="n">long_parameter_name_4</span><span class="p">,</span>
<span class="n">long_parameter_name_5</span><span class="p">,</span>
<span class="n">long_parameter_name_6</span><span class="p">,</span>
<span class="n">long_parameter_name_7</span><span class="p">,</span>
<span class="n">long_parameter_name_8</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">some_long_struct_name_s</span>
<span class="p">{</span>
<span class="cm">/* The forward link to the next instance of struct</span>
<span class="cm"> * some_long_struct_name_s in a singly linked list.</span>
<span class="cm"> */</span>
<span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="n">flink</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">short_name1</span><span class="p">;</span> <span class="cm">/* Short comment 1. */</span>
<span class="kt">int</span> <span class="n">short_name2</span><span class="p">;</span> <span class="cm">/* This is a very long comment describing subtle</span>
<span class="cm"> * aspects of the short_name2 field. */</span>
<span class="p">};</span>
<span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="p">(</span><span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="p">)</span>
<span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">some_medium_name_s</span><span class="p">);</span>
<span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span>
<span class="p">(</span><span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_medium_name_s</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">some_medium_name_s</span><span class="p">);</span>
<span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span>
<span class="p">(</span><span class="n">FAR</span> <span class="k">struct</span> <span class="n">some_long_struct_name_s</span> <span class="o">*</span><span class="p">)</span>
<span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">some_long_struct_name_s</span><span class="p">);</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">some_function_with_many</span> <span class="n">parameters</span><span class="p">(</span><span class="n">long_parameter_name_1</span><span class="p">,</span>
<span class="n">long_parameter_name_2</span><span class="p">,</span>
<span class="n">long_parameter_name_3</span><span class="p">,</span>
<span class="n">long_parameter_name_4</span><span class="p">,</span>
<span class="n">long_parameter_name_5</span><span class="p">,</span>
<span class="n">long_parameter_name_6</span><span class="p">,</span>
<span class="n">long_parameter_name_7</span><span class="p">,</span>
<span class="n">long_parameter_name_8</span><span class="p">);</span>
</pre></div>
</div>
</div>
<p><strong>NOTE</strong>: See the discussion of <a class="reference external" href="#farnear">pointers</a> for
information about the <code class="docutils literal notranslate"><span class="pre">FAR</span></code> qualifier used above.</p>
<p><strong>Double Spacing</strong>. A single blank line may be use to separate
logical groupings as the designer feels fit. Single blank lines
are also required in certain contexts as defined in this standard.
Additional blanks lines (two or more) are prohibited.</p>
<p><strong>Columnar Organization</strong>. Similar things should be aligned on the
same column unless doing so would cause the line width to be
exceeded.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span>
<span class="n">monkey</span> <span class="o">=</span> <span class="n">oxen</span><span class="p">;</span>
<span class="n">aardvark</span> <span class="o">=</span> <span class="n">macaque</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span>
<span class="n">monkey</span> <span class="o">=</span> <span class="n">oxen</span><span class="p">;</span>
<span class="n">aardvark</span> <span class="o">=</span> <span class="n">macaque</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p><strong>Block Comments</strong> The final asterisk (<code class="docutils literal notranslate"><span class="pre">*</span></code>) should occur at
column 78 (or the line width of files with longer lines). Note
that the final comment delimiter of the block comment is an
exception an lies at column 79.</p>
</div>
<div class="section" id="comments">
<h3>Comments<a class="headerlink" href="#comments" title="Permalink to this headline"></a></h3>
<p><strong>General</strong>. Within a comment, the text must be standard English
conforming to standard English rules of grammar and spelling (US
English spelling). Of course, this is not the place to summarize
all English grammar, but as examples of common grammatic issues in
comments:</p>
<ul class="simple">
<li><p>All sentences should begin with an upper-case character and end
with either ‘.’, ‘?’, or ‘!’.</p></li>
<li><p>Sentence fragments and phrases are generally treated the same
as sentences.</p></li>
<li><p>The punctuation ‘.’ and ‘:’ is followed by two spaces; the
punctuation ‘,’ and ‘;’ is followed by a single space.</p></li>
<li><p>Text following ‘.’ or ‘:’ begins with an upper-case character;
text following ‘,’ or ‘;’ begins with a lower-case character.</p></li>
</ul>
<p><strong>Line Spacing</strong> A single blank line should precede and follow
each comment. The only exceptions are:</p>
<p>For the file header block comment that begins on line one; there
is no preceding blank line in that case.
For conditional compilation. Conditional compilation should
include the conditional logic <em>and</em> all comments associated with
the conditional logic. In this case, the blank line appears
<em>before</em> the conditional, not after it. No blank lines precede any
comments following the conditional.
With braces. No blank line separates the line containing the
opening left brace from a comment. No blank line follows a comment
that may be the final line preceding a closing right brace.
With Labels. No blank line separates the line containing the label
from a comment.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span> <span class="cm">/* set a equal to b */</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="cm">/* set b equal to c */</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">c</span><span class="p">;</span>
<span class="cm">/* Do the impossible */</span>
<span class="cp">#ifdef CONFIG_THE_IMPOSSIBLE</span>
<span class="n">the_impossible</span><span class="p">();</span>
<span class="cp">#endif</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* Only a comment */</span>
<span class="p">}</span>
<span class="nl">here</span><span class="p">:</span>
<span class="cm">/* This is the place */</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span> <span class="cm">/* Set a equal to b. */</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="cm">/* Set b equal to c. */</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">c</span><span class="p">;</span>
<span class="cp">#ifdef CONFIG_THE_IMPOSSIBLE</span>
<span class="cm">/* Do the impossible */</span>
<span class="n">the_impossible</span><span class="p">();</span>
<span class="cp">#endif</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* Only a comment */</span>
<span class="p">}</span>
<span class="nl">here</span><span class="p">:</span>
<span class="cm">/* This is the place */</span>
</pre></div>
</div>
</div>
<p><strong>Indentation</strong> Comments should, typically, be placed before the
code section to which they apply. The comment indentation should
be the same as the follow indentation rules as the following code
(if applicable).</p>
<p><strong>Short, Single line comments</strong>. Short comments must lie on a
single line. The comment delimiters must lie on the same line.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*</span>
<span class="cm"> * This is a single line comment</span>
<span class="cm"> */</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* This is a single line comment. */</span>
</pre></div>
</div>
</div>
<p><strong>Multi-line comments</strong>. If the comment is too long to fit on a
single, it must be broken into a multi-line comment. The comment
must be begin on the first line of the multi-line comment with the
opening comment delimiter (<code class="docutils literal notranslate"><span class="pre">/*</span></code>). The following lines of the
multi-line comment must be with an asterisk (<code class="docutils literal notranslate"><span class="pre">*</span></code>) aligned in the
same column as the asterisk in the preceding line. The closing
comment delimiter must lie on a separate line with the asterisk
(<code class="docutils literal notranslate"><span class="pre">*</span></code>) aligned in the same column as the asterisk in the
preceding line.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*</span>
<span class="cm"> This is the first line of a multi-line comment.</span>
<span class="cm"> This is the second line of a multi-line comment.</span>
<span class="cm"> This is the third line of a multi-line comment. */</span>
<span class="cm">/* This is the first line of another multi-line comment. */</span>
<span class="cm">/* This is the second line of another multi-line comment. */</span>
<span class="cm">/* This is the third line of another multi-line comment. */</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* This is the first line of a multi-line comment.</span>
<span class="cm"> * This is the second line of a multi-line comment.</span>
<span class="cm"> * This is the third line of a multi-line comment.</span>
<span class="cm"> */</span>
</pre></div>
</div>
</div>
<p><strong>Comments to the Right of Statements</strong>. Comments to the right of
statements in C source files are discouraged. If such comments are
used, they should be (1) very short so that they do not exceed the
line width (typically 78 characters), (2) aligned so that the
comment begins in the same column on each line.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* Make the dog be a cat */</span>
<span class="n">monkey</span> <span class="o">=</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* Make the monkey be an oxen */</span>
<span class="n">aardvark</span> <span class="o">=</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* Make the aardvark be a macaque */</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* Make the dog be a cat. */</span>
<span class="n">monkey</span> <span class="o">=</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* Make the monkey be an oxen. */</span>
<span class="n">aardvark</span> <span class="o">=</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* Make the aardvark be a macaque. */</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* Make the dog be a cat. */</span>
<span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span>
<span class="cm">/* Make the monkey be an oxen. */</span>
<span class="n">monkey</span> <span class="o">=</span> <span class="n">oxen</span><span class="p">;</span>
<span class="cm">/* Make the aardvark be a macaque. */</span>
<span class="n">aardvark</span> <span class="o">=</span> <span class="n">macaque</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p><strong>Comments to the Right of Data Definitions</strong>. Comments to the
right of a declaration with an enumeration or structure, on the
other hand, are encouraged, provided that the comments are short
and do not exceed the maximum line width (usually 78 characters).
Columnar alignment of comments is very desirable (but often cannot
be achieved without violating the line width).</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">animals_s</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">dog</span><span class="p">;</span> <span class="cm">/* This is a dog */</span>
<span class="kt">int</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This is a cat */</span>
<span class="kt">double</span> <span class="n">monkey</span><span class="p">;</span> <span class="cm">/* This is a monkey */</span>
<span class="kt">double</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* This is an oxen */</span>
<span class="kt">bool</span> <span class="n">aardvark</span><span class="p">;</span> <span class="cm">/* This is an aardvark */</span>
<span class="kt">bool</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* This is a macaque */</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">animals_s</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">dog</span><span class="p">;</span> <span class="cm">/* This is a dog. */</span>
<span class="kt">int</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This is a cat. */</span>
<span class="kt">double</span> <span class="n">monkey</span><span class="p">;</span> <span class="cm">/* This is a monkey. */</span>
<span class="kt">double</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* This is an oxen. */</span>
<span class="kt">bool</span> <span class="n">aardvark</span><span class="p">;</span> <span class="cm">/* This is an aardvark. */</span>
<span class="kt">bool</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* This is a macaque. */</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">animals_s</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">dog</span><span class="p">;</span> <span class="cm">/* This is a dog. */</span>
<span class="kt">int</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This is a cat. */</span>
<span class="kt">double</span> <span class="n">monkey</span><span class="p">;</span> <span class="cm">/* This is a monkey. */</span>
<span class="kt">double</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* This is an oxen. */</span>
<span class="kt">bool</span> <span class="n">aardvark</span><span class="p">;</span> <span class="cm">/* This is an aardvark. */</span>
<span class="kt">bool</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* This is a macaque. */</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<p><strong>Long Comments on the Right</strong>. Comments on the right of
statements or data definitions must be short and fit on the same
line without exceeding the maximum line length. If a longer
comment is needed, then it should appear above the statement of
definition rather than to the right of the definition.</p>
<p><strong>Breaking Long Comments to the Right of Statements</strong> Breaking
long comments to the right of statements is acceptable as well,
but not encouraged. In this case the comment must be begin on the
first line of the multi-line, right-hand comment with the opening
comment delimiter (/<em>). The following lines of the multi-line,
right hand comment must be with an asterisk (</em>) aligned in the
same column as the asterisk in the preceding line. The closing
comment delimiter must lie on the <em>same</em> line with the asterisk.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This assignment will convert what was at one time a lowly dog into a ferocious feline. */</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This assignment will convert what was at one time a</span>
<span class="cm"> * lowly dog into a ferocious feline. */</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* This assignment will convert what was at one time a lowly dog into a</span>
<span class="cm"> * ferocious feline.</span>
<span class="cm"> */</span>
<span class="n">dog</span> <span class="o">=</span> <span class="n">cat</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p><strong>Note</strong> that if the comment is continued on multiple lines, the
comment alignment and multi-line comment rules still apply with
one exception: The closing <code class="docutils literal notranslate"><span class="pre">*/</span></code> appears on the same line as the
final text of the comment. This exception to the rule is enforced
to keep the statements and definitions from becoming to spread
out.</p>
<p><strong>Block comments</strong>. Block comments are only used to delimit
groupings with the overall <a class="reference external" href="#fileorganization">file
organization</a> and should not be used unless
the usage is consistent with delimiting logical groupings in the
program.</p>
<p><strong>C Style Comments</strong>. C99/C11/C++ style comments (beginning with
<code class="docutils literal notranslate"><span class="pre">//</span></code>) should not be used with NuttX. NuttX generally follows C89
and all code outside of architecture specific directories must be
compatible with C89.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// This is a structure of animals</span>
<span class="k">struct</span> <span class="n">animals_s</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">dog</span><span class="p">;</span> <span class="c1">// This is a dog</span>
<span class="kt">int</span> <span class="n">cat</span><span class="p">;</span> <span class="c1">// This is a cat</span>
<span class="kt">double</span> <span class="n">monkey</span><span class="p">;</span> <span class="c1">// This is a monkey</span>
<span class="kt">double</span> <span class="n">oxen</span><span class="p">;</span> <span class="c1">// This is an oxen</span>
<span class="kt">bool</span> <span class="n">aardvark</span><span class="p">;</span> <span class="c1">// This is an aardvark</span>
<span class="kt">bool</span> <span class="n">macaque</span><span class="p">;</span> <span class="c1">// This is a macaque</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* This is a structure of animals. */</span>
<span class="k">struct</span> <span class="n">animals_s</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">dog</span><span class="p">;</span> <span class="cm">/* This is a dog. */</span>
<span class="kt">int</span> <span class="n">cat</span><span class="p">;</span> <span class="cm">/* This is a cat. */</span>
<span class="kt">double</span> <span class="n">monkey</span><span class="p">;</span> <span class="cm">/* This is a monkey. */</span>
<span class="kt">double</span> <span class="n">oxen</span><span class="p">;</span> <span class="cm">/* This is an oxen. */</span>
<span class="kt">bool</span> <span class="n">aardvark</span><span class="p">;</span> <span class="cm">/* This is an aardvark. */</span>
<span class="kt">bool</span> <span class="n">macaque</span><span class="p">;</span> <span class="cm">/* This is a macaque. */</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<p><strong>“Commenting Out” Large Code Blocks</strong>. Do not use C or C++ comments to
disable compilation of large blocks of code. Instead, use <code class="docutils literal notranslate"><span class="pre">#if</span> <span class="pre">0</span></code> to
do that. Make sure there is a comment before the <code class="docutils literal notranslate"><span class="pre">#if</span> <span class="pre">0</span></code> to explain
why the code is not compiled.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">some_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="cm">/*</span>
<span class="cm"> ... disabled code ..</span>
<span class="cm"> */</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">some_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="c1">//</span>
<span class="c1">// ... disabled code ..</span>
<span class="c1">//</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">some_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="cm">/* The following code is disabled because it is no longer needed. */</span>
<span class="cp">#if 0</span><span class="c"></span>
<span class="c"> ... disabled code ..</span>
<span class="cp">#endif</span>
<span class="p">...</span> <span class="n">compiled</span> <span class="n">code</span> <span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="braces">
<h3>Braces<a class="headerlink" href="#braces" title="Permalink to this headline"></a></h3>
<p>In general, the use of braces in the NuttX coding standard is similar to
the use of braces in the <a class="reference external" href="https://www.gnu.org/prep/standards/standards.pdf">GNU Coding
standards</a> with a
few subtle differences.</p>
<p><strong>Coding Standard:</strong></p>
<ul class="simple">
<li><p><strong>Always on Separate Lines</strong>. Braces always appear on a separate line
containing nothing else other than white space.</p></li>
<li><p><strong>Never Comments on Braces</strong>. Do not put comments on the same line as
braces.</p></li>
<li><p><strong>Compound Statements</strong>. Within this document, an opening left brace
followed by a sequence of statements, and ending with a closing right
brace is referred to as a <em>compound statement</em>.</p></li>
<li><p><strong>Nested Compound Statements</strong>. In the case where there are nested
compound statements that end with several consecutive right braces,
each closing right brace must lie on a separate line and must be
indented to match the corresponding opening brace.</p></li>
<li><p><strong>Final brace followed by a single blank line</strong>. The <em>final</em> right
brace must be followed by a blank line as per standard rules. There
are two exceptions to this rule:</p>
<ol class="arabic simple">
<li><p>In the case where there are nested several consecutive right
braces, no blank lines should be inserted except for after the
<em>final</em> right brace.</p></li>
<li><p>No blank should be used to separate the final, closing right brace
when it is followed by a <code class="docutils literal notranslate"><span class="pre">break;</span></code> statement.</p></li>
</ol>
</li>
<li><p><strong>Special Indentation Rules</strong>. Special <a class="reference external" href="#indentation">indentation
rules</a> apply to braces.</p></li>
</ul>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="p">(</span><span class="nb">true</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="cm">/* if valid */</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="cm">/* not valid */</span>
<span class="p">}</span> <span class="cm">/* end forever */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">-</span><span class="n">b</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="p">(</span><span class="nb">true</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">-</span><span class="n">b</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p><strong>Exception to Indentation Rule for Braces</strong>. The exception is braces
that following structure, enumeration, union, and function declarations.
There is no additional indentation for those braces; those braces align
with the beginning of the definition</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">enum</span> <span class="n">kinds_of_dogs_e</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">dogs_s</span> <span class="p">{</span>
<span class="p">...</span>
<span class="k">union</span> <span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="n">u</span><span class="p">;</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">cats_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">union</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="n">u</span><span class="p">;</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="kt">int</span> <span class="nf">animals</span><span class="p">(</span><span class="kt">int</span> <span class="n">animal</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">enum</span> <span class="n">kinds_of_dogs_e</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">dogs_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">union</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="n">u</span><span class="p">;</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">cats_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">union</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span> <span class="n">u</span><span class="p">;</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="kt">int</span> <span class="nf">animals</span><span class="p">(</span><span class="kt">int</span> <span class="n">animal</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="indentation">
<h3>Indentation<a class="headerlink" href="#indentation" title="Permalink to this headline"></a></h3>
<p>In general, the indentation in the NuttX coding standard is similar to
the indentation requirements of the <a class="reference external" href="https://www.gnu.org/prep/standards/standards.pdf">GNU Coding
standards</a> with a
few subtle differences.</p>
<p><strong>Indentation Unit</strong>. Indentation is in units of two spaces; Each
indentation level is twos spaces further to the right than the preceding
indentation levels. TAB characters may not be used for indentation.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dosomething</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
<span class="n">dosomething</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">dosomething</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p><strong>Use of TAB Characters</strong>. The use of TAB characters for indentation is
prohibited in C source and header files. TAB characters are, however,
used in make files, assembly language source files, Kconfig files and
some script files. When TAB characters are used in these files, spaces
may not be used for indentation. The correct TAB setting is 4 spaces
(not 8) in these cases.</p>
<p><strong>Alignment of Braces</strong>. Note that since braces must be on a separate
line (see above), this indentation by two spaces has an interesting
property:</p>
<ul class="simple">
<li><p>All C statements (and case selectors) lie on lines that are multiples
of 4 spaces (beginning with an indentation of two): 2, 6, 10, …
(4*n + 2) (for indentation level n = 0, 1, …)</p></li>
<li><p>Braces lie on a separate line also indented by multiple of 4 spaces:
4, 8, 12, … 4*n (for indentation level n = 1, 2, …)</p></li>
</ul>
<p>Thus, all code at the indentation level should align on the same column.
Similarly, opening and closing braces at the same indentation level
should also align on the same (but different) column.</p>
<p><strong>Indentation of Pre-Processor Lines</strong>. C Pre-processor commands
following any conditional computation are also indented following
basically the indentation same rules, differing in that the <code class="docutils literal notranslate"><span class="pre">#</span></code> always
remains in column 1.</p>
<p>When C pre-processor statements are indented, they should be should be
indented by 2 spaces per level-of-indentation following the <code class="docutils literal notranslate"><span class="pre">#</span></code>. C
pre-processor statements should be indented when they are enclosed
within C pre-processor conditional logic (<code class="docutils literal notranslate"><span class="pre">#if</span></code>..<code class="docutils literal notranslate"><span class="pre">#endif</span></code>). The
level of indentation increases with each level of such nested
conditional logic.</p>
<p>C pre-processor statements should always be indented in this way in the
<code class="docutils literal notranslate"><span class="pre">Pre-processor</span> <span class="pre">Definitions</span></code> <a class="reference external" href="#cfilestructure">section</a> of each
file. C pre-processor statements may be indented in the
<code class="docutils literal notranslate"><span class="pre">Public/Private</span> <span class="pre">Data</span></code> and <code class="docutils literal notranslate"><span class="pre">Public/Private</span> <span class="pre">Functions</span></code> sections of the
file. However, often the indentation of C pre-processor statements
conflicts with the indentation of the C code and makes the code more
difficult to read. In such cases, indentation of C pre-processor
statements should be omitted in those sections (only).</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifdef CONFIG_ABC</span>
<span class="cp">#define ABC_THING1 1</span>
<span class="cp">#define ABC_THING2 2</span>
<span class="cp">#define ABC_THING3 3</span>
<span class="cp">#endif</span>
<span class="cp">#ifdef CONFIG_ABC</span>
<span class="cp">#define ABC_THING1 1</span>
<span class="cp">#define ABC_THING2 2</span>
<span class="cp">#define ABC_THING3 3</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifdef CONFIG_ABC</span>
<span class="cp"># define ABC_THING1 1</span>
<span class="cp"># define ABC_THING2 2</span>
<span class="cp"># define ABC_THING3 3</span>
<span class="cp">#endif</span>
<span class="cp">#ifdef CONFIG_ABC</span>
<span class="cp"># define ABC_THING1 1</span>
<span class="cp"># define ABC_THING2 2</span>
<span class="cp"># define ABC_THING3 3</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</div>
<p><strong>Exception</strong>. Each header file includes <a class="reference external" href="#idempotence">idempotence
definitions</a> at the beginning of the header file. This
conditional compilation does <em>not</em> cause any change to the indentation.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef __INCLUDE_SOMEHEADER_H</span>
<span class="cp"># define __INCLUDE_SOMEHEADER_H</span>
<span class="p">...</span>
<span class="cp"># define THING1 1</span>
<span class="cp"># define THING2 2</span>
<span class="cp"># define THING3 3</span>
<span class="p">...</span>
<span class="cp">#endif </span><span class="cm">/* __INCLUDE_SOMEHEADER_H */</span><span class="cp"></span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef __INCLUDE_SOMEHEADER_H</span>
<span class="cp">#define __INCLUDE_SOMEHEADER_H</span>
<span class="p">...</span>
<span class="cp">#define THING1 1</span>
<span class="cp">#define THING2 2</span>
<span class="cp">#define THING3 3</span>
<span class="p">...</span>
<span class="cp">#endif </span><span class="cm">/* __INCLUDE_SOMEHEADER_H */</span><span class="cp"></span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="parentheses">
<h3>Parentheses<a class="headerlink" href="#parentheses" title="Permalink to this headline"></a></h3>
<p><strong>Coding Standard:</strong></p>
<ul class="simple">
<li><p><strong>Space after key words</strong>. Do not put a left parenthesis (<code class="docutils literal notranslate"><span class="pre">(</span></code>)
immediately after any C keywords (<code class="docutils literal notranslate"><span class="pre">for</span></code>, <code class="docutils literal notranslate"><span class="pre">switch</span></code>, <code class="docutils literal notranslate"><span class="pre">while</span></code>,
<code class="docutils literal notranslate"><span class="pre">do</span></code>, <code class="docutils literal notranslate"><span class="pre">return</span></code>, etc.). Put a space before the left parenthesis in
these cases.</p></li>
<li><p><strong>Otherwise, no space before left parentheses</strong>. Otherwise, there
should be no space before the left parentheses</p></li>
<li><p><strong>No space between function name and argument list</strong>. There should be
no space between a function name and an argument list.</p></li>
<li><p><strong>Never space before the right parentheses</strong>. There should never be
space before a right parenthesis ( <code class="docutils literal notranslate"><span class="pre">)</span></code> ).</p></li>
<li><p><strong>No parentheses around returned values</strong>. Returned values should
never be enclosed in parentheses unless the parentheses are required
to force the correct order of operations in a computed return value.</p></li>
</ul>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">do_foobar</span> <span class="p">(</span> <span class="kt">void</span> <span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">(</span> <span class="p">(</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span> <span class="p">)</span> <span class="o">||</span> <span class="p">(</span> <span class="n">ret</span> <span class="o">&lt;</span> <span class="mi">10</span> <span class="p">)</span> <span class="p">);</span> <span class="n">i</span><span class="o">++</span> <span class="p">)</span>
<span class="p">{</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">foobar</span> <span class="p">(</span> <span class="n">i</span> <span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="p">(</span> <span class="n">ret</span> <span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">do_foobar</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span> <span class="o">||</span> <span class="n">ret</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">foobar</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p><strong>NOTE:</strong> Many people do not trust their understanding of the precedence
of operators and so use lots of parentheses in expressions to force the
order of evaluation even though the parentheses may have no effect. This
will certainly avoid errors due to an unexpected order of evaluation,
but can also make the code ugly and overly complex (as in the above
example). In general, NuttX does not use unnecessary parentheses to
force order of operations. There is no particular policy in this regard.
However, you are are advised to check your C Programming Language book
if necessary and avoid unnecessary parenthesis when possible.</p>
</div>
</div>
<div class="section" id="data-and-type-definitions">
<h2>Data and Type Definitions<a class="headerlink" href="#data-and-type-definitions" title="Permalink to this headline"></a></h2>
<div class="section" id="one-definition-declaration-per-line">
<h3>One Definition/Declaration Per Line<a class="headerlink" href="#one-definition-declaration-per-line" title="Permalink to this headline"></a></h3>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">extern</span> <span class="kt">long</span> <span class="n">time</span><span class="p">,</span> <span class="n">money</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">**</span><span class="n">ach</span><span class="p">,</span> <span class="o">*</span><span class="n">bch</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">extern</span> <span class="kt">long</span> <span class="n">time</span><span class="p">;</span>
<span class="k">extern</span> <span class="kt">long</span> <span class="n">money</span><span class="p">;</span>
<span class="n">FAR</span> <span class="kt">char</span> <span class="o">**</span><span class="n">ach</span><span class="p">;</span>
<span class="n">FAR</span> <span class="kt">char</span> <span class="o">*</span><span class="n">bch</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">j</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">k</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p><strong>NOTE</strong>: See the discussion of <a class="reference external" href="#farnear">pointers</a> for information
about the <code class="docutils literal notranslate"><span class="pre">FAR</span></code> qualifier used above.</p>
</div>
<div class="section" id="global-variables">
<h3>Global Variables<a class="headerlink" href="#global-variables" title="Permalink to this headline"></a></h3>
<p><strong>Global vs. Local vs. Public vs. Private</strong> By a <em>global</em> variable it is
meant any variable defined outside of a function. The distinction is
between this kind of <em>global</em> and function <em>local</em> definition and refers
to the scope a symbol <em>within a file</em>. A related concept for all
<em>global</em> names defined within a file is the scope of the name across
different files. If the global symbol is pre-pended with the <code class="docutils literal notranslate"><span class="pre">static</span></code>
storage class then the scope of the global symbol is within the file
only. This is a somewhat different concept and within NuttX you will
find these distinguished as <em>private</em> vs. <em>public</em> global symbols.
However, within this standard, the term <em>global variable</em> will refer to
any variable that has more than local scope.</p>
<p><strong>Coding Standard:</strong></p>
<ul class="simple">
<li><p><strong>Short global variable names</strong>. Names should be terse, but generally
descriptive of what the variable is for. Try to say something with
the variable name, but don’t try to say too much. For example, the
variable name of <code class="docutils literal notranslate"><span class="pre">g_filelen</span></code> is preferable to something like
<code class="docutils literal notranslate"><span class="pre">g_lengthoffile</span></code>.</p></li>
<li><p><strong>Global variable prefix</strong>. All global variables begin with the
prefix <code class="docutils literal notranslate"><span class="pre">g_</span></code> to indicate the scope of variable.</p></li>
<li><p><strong>Module name prefix</strong> If a global variable belongs in a <em>module</em>
with a name of, say <code class="docutils literal notranslate"><span class="pre">xyz</span></code>, then that module should be included as
part of the prefix like: <code class="docutils literal notranslate"><span class="pre">g_xyz_</span></code>.</p></li>
<li><p><strong>Lowercase</strong>, Use all lower case letters.</p></li>
<li><p><strong>Minimal use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code>. Preferably there are no <code class="docutils literal notranslate"><span class="pre">_</span></code>
separators within the name. Long variable names might require some
delimitation using <code class="docutils literal notranslate"><span class="pre">_</span></code>. Long variable names, however, are
discouraged.</p></li>
<li><p><strong>Use structures</strong>. If possible, wrap all global variables within a
structure to minimize the liklihood of name collisions.</p></li>
<li><p><strong>Avoid global variables when possible</strong>. Use of global variables, in
general, is discourage unless there is no other reasonable solution.</p></li>
</ul>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">extern</span> <span class="kt">int</span> <span class="n">someint</span><span class="p">;</span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">anotherint</span><span class="p">;</span>
<span class="kt">uint32_t</span> <span class="n">dwA32BitInt</span><span class="p">;</span>
<span class="kt">uint32_t</span> <span class="n">gAGlobalVariable</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">extern</span> <span class="kt">int</span> <span class="n">g_someint</span><span class="p">;</span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">g_anotherint</span><span class="p">;</span>
<span class="kt">uint32_t</span> <span class="n">g_a32bitint</span><span class="p">;</span>
<span class="kt">uint32_t</span> <span class="n">g_aglobal</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">my_variables_s</span>
<span class="p">{</span>
<span class="kt">uint32_t</span> <span class="n">a32bitint</span><span class="p">;</span>
<span class="kt">uint32_t</span> <span class="n">aglobal</span><span class="p">;</span>
<span class="p">};</span>
<span class="k">extern</span> <span class="kt">int</span> <span class="n">g_someint</span><span class="p">;</span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">g_anotherint</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">my_variables_s</span> <span class="n">g_myvariables</span><span class="p">;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="parameters-and-local-variables">
<h3>Parameters and Local Variables<a class="headerlink" href="#parameters-and-local-variables" title="Permalink to this headline"></a></h3>
<p><strong>Coding Standard:</strong></p>
<ul class="simple">
<li><p><strong>Common naming standard</strong>. Naming for function parameters and local
variables is the same.</p></li>
<li><p><strong>Short variable names</strong>. Names should be terse, but generally
descriptive of what the variable is for. Try to say something with
the variable name, but don’t try to say too much. For example, the
variable name of <code class="docutils literal notranslate"><span class="pre">len</span></code> is preferable to something like
<code class="docutils literal notranslate"><span class="pre">lengthofiobuffer</span></code>.</p></li>
<li><p><strong>No special ornamentation</strong>. There is no special ornamentation of
the name to indication that the variable is a local variable. The
prefix <code class="docutils literal notranslate"><span class="pre">p</span></code> or <code class="docutils literal notranslate"><span class="pre">pp</span></code> may be used on names of pointers (or pointer
to pointers) if it helps to distinguish the variable from some other
local variable with a similar name. Even this convention is
discouraged when not necessary.</p></li>
<li><p><strong>Lowercase</strong> Use all lower case letters.</p></li>
<li><p><strong>Minimal use of single character variable names</strong>. Short variable
names are preferred. However, single character variable names should
be avoided. Exceptions to this include <code class="docutils literal notranslate"><span class="pre">i</span></code>, <code class="docutils literal notranslate"><span class="pre">j</span></code>, and <code class="docutils literal notranslate"><span class="pre">k</span></code> which
are reserved only for use as loop indices (part of our Fortran
legacy).</p></li>
<li><p><strong>Minimal use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code>. Preferably there are no <code class="docutils literal notranslate"><span class="pre">_</span></code>
separators within the name. Long variable names might require some
delimitation using <code class="docutils literal notranslate"><span class="pre">_</span></code>. Long variable names, however, are
discouraged.</p></li>
</ul>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">uint32_t</span> <span class="nf">somefunction</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">uint32_t</span> <span class="n">dwBValue</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">uint32_t</span> <span class="n">this_is_a_long_variable_name</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="n">a</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">this_is_a_long_variable_name</span> <span class="o">*=</span> <span class="n">dwBValue</span><span class="o">--</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">this_is_a_long_variable_name</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">uint32_t</span> <span class="nf">somefunction</span><span class="p">(</span><span class="kt">int</span> <span class="n">limit</span><span class="p">,</span> <span class="kt">uint32_t</span> <span class="n">value</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">uint32_t</span> <span class="n">ret</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="n">limit</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ret</span> <span class="o">*=</span> <span class="n">value</span><span class="o">--</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p><strong>NOTE:</strong> You will see the local variable named <code class="docutils literal notranslate"><span class="pre">ret</span></code> is frequently
used in the code base for the name of a local variable whose value will
be returned or to received the returned value from a called function.</p>
</div>
<div class="section" id="type-definitions">
<h3>Type Definitions<a class="headerlink" href="#type-definitions" title="Permalink to this headline"></a></h3>
<p><strong>Coding Standard:</strong></p>
<ul class="simple">
<li><p><strong>Short type names</strong>. Type names should be terse, but generally
descriptive of what the type is for. Try to say something with the
type name, but don’t try to say too much. For example, the type name
of <code class="docutils literal notranslate"><span class="pre">fhandle_t</span></code> is preferable to something like
<code class="docutils literal notranslate"><span class="pre">openfilehandle_t</span></code>.</p></li>
<li><p><strong>Type name suffix</strong>. All <code class="docutils literal notranslate"><span class="pre">typedef</span></code>’ed names end with the suffix
<code class="docutils literal notranslate"><span class="pre">_t</span></code>.</p></li>
<li><p><strong>Module name prefix</strong> If a type belongs in a <em>module</em> with a name
of, say <code class="docutils literal notranslate"><span class="pre">xyz</span></code>, then that module should be included as a prefix to
the type name like: <code class="docutils literal notranslate"><span class="pre">xyz_</span></code>.</p></li>
<li><p><strong>Lowercase</strong>. Use all lower case letters.</p></li>
<li><p><strong>Minimal use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code>. Preferably there are few <code class="docutils literal notranslate"><span class="pre">_</span></code>
separators within the type name. Long type names might require some
delimitation using <code class="docutils literal notranslate"><span class="pre">_</span></code>. Long type names, however, are
discouraged.</p></li>
</ul>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="kt">void</span> <span class="o">*</span><span class="n">myhandle</span><span class="p">;</span>
<span class="k">typedef</span> <span class="kt">int</span> <span class="n">myInteger</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="n">FAR</span> <span class="kt">void</span> <span class="o">*</span><span class="n">myhandle_t</span><span class="p">;</span>
<span class="k">typedef</span> <span class="kt">int</span> <span class="n">myinteger_t</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p><strong>NOTE</strong>: See the discussion of <a class="reference external" href="#farnear">pointers</a> for information
about the <code class="docutils literal notranslate"><span class="pre">FAR</span></code> qualifier used above.</p>
</div>
<div class="section" id="structures">
<h3>Structures<a class="headerlink" href="#structures" title="Permalink to this headline"></a></h3>
<p><strong>Structure Naming</strong></p>
<ul class="simple">
<li><p><strong>No un-named structures</strong>. All structures must be named, even if
they are part of a type definition. That is, a structure name must
follow the reserved word <code class="docutils literal notranslate"><span class="pre">struct</span></code> in all structure definitions.
There are two exceptions to this rule:</p>
<ol class="arabic simple">
<li><p>First for structures that are defined within another union or
structure (discouraged). In those cases, the structure name should
always be omitted.</p></li>
<li><p>Second for structures as the type of a local variable. In this
case, again, the structure name should always be omitted.</p></li>
</ol>
</li>
<li><p><strong>Structured defined with structures discouraged</strong>. Fields within a
structure may be another structure that is defined only with the
scope of the containing structure. This practice is acceptable, but
discouraged.</p></li>
<li><p><strong>No un-named structure fields</strong>. Structures may contain other
structures as fields. In this case, the structure field must be
named. C11 permits such un-named structure fields within a structure.
NuttX generally follows C89 and all code outside of architecture
specific directories must be compatible with C89.</p></li>
<li><p><strong>No structure definitions within Type Definition</strong>. The practice of
defining a structure within a type definition is discouraged. It is
preferred that the structure definition and the type definition be
separate definitions. In general, the NuttX coding style discourages
any <code class="docutils literal notranslate"><span class="pre">typdef</span></code>-ing of structures; normally the full structure name is
used as types throughout the code. The reason for this is that is
structure pointers may be forward referenced in header files without
having to include the file the provides the type definition. This
greatly reduces header file coupling.</p></li>
<li><p><strong>Short structure names</strong>. Structure names should be terse, but
generally descriptive of what the structure contains. Try to say
something with the structure name, but don’t try to say too much. For
example, the structure name of <code class="docutils literal notranslate"><span class="pre">xyz_info_s</span></code> is preferable to
something like <code class="docutils literal notranslate"><span class="pre">xyz_datainputstatusinformation_s</span></code>.</p></li>
<li><p><strong>Structure name suffix</strong>. All structure names end with the suffix
<code class="docutils literal notranslate"><span class="pre">_s</span></code>.</p></li>
<li><p><strong>Module name prefix</strong> If a structure belongs to a <em>module</em> with a
name of, say <code class="docutils literal notranslate"><span class="pre">xyz</span></code>, then that module should be included as a prefix
to the structure name like: <code class="docutils literal notranslate"><span class="pre">xyz_</span></code>.</p></li>
<li><p><strong>Lowercase</strong>. Use all lower case letters.</p></li>
<li><p><strong>Minimal use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code>. Preferably there are few <code class="docutils literal notranslate"><span class="pre">_</span></code>
separators within the structure name. Long variable names might
require some delimitation using <code class="docutils literal notranslate"><span class="pre">'_'</span></code>. Long variable names,
however, are discouraged.</p></li>
</ul>
<p><strong>Structure Field Naming</strong></p>
<ul class="simple">
<li><p><strong>Common variable naming</strong>. Structure field naming is generally the
same as for local variables.</p></li>
<li><p><strong>One definition per line</strong>. The <a class="reference external" href="#onedatperline">one definition per
line</a> rule applies to structure fields, including
bit field definitions.</p></li>
<li><p><strong>Each field should be commented</strong>. Each structure field should be
commented. Commenting should follow the <a class="reference external" href="#comments">standard
conventions</a>.</p></li>
<li><p><strong>Optional structure field prefix</strong>. It make be helpful to add a
two-letter prefix to each field name so that is is clear which
structure the field belongs to. Although a good practice, that
convention has not been used consistently in NuttX.</p></li>
<li><p><strong>Lowercase</strong>. Use all lower case letters.</p></li>
<li><p><strong>Minimal use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code>. Preferably there are few <code class="docutils literal notranslate"><span class="pre">_</span></code>
separators within the field name. Long variable names might require
some delimitation using <code class="docutils literal notranslate"><span class="pre">'_'</span></code>. Long variable names, however, are
discouraged.</p></li>
</ul>
<p><strong>Other Applicable Coding Standards</strong>. See sections related to <a class="reference external" href="#lines">line
formatting</a>, <a class="reference external" href="#braces">use of braces</a>,
<a class="reference external" href="#indentation">indentation</a>, and <a class="reference external" href="#comments">comments</a>.</p>
<p><strong>Size Optimizations</strong>. When declaring fields in structures, order the
declarations in such a way as to minimize memory waste due of data
alignment. This essentially means that that fields should be organized
by data size, not by functionality: Put all pointers togeter, all
<code class="docutils literal notranslate"><span class="pre">uint8_t</span></code>’s together, all <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code>’s together. Data types withi
well known like <code class="docutils literal notranslate"><span class="pre">uint8_t</span></code> and <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> should also be place in
either ascending or descending size order.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="k">struct</span> <span class="cm">/* Un-named structure */</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="kt">int</span> <span class="n">val1</span><span class="p">,</span> <span class="n">val2</span><span class="p">,</span> <span class="n">val3</span><span class="p">;</span> <span class="cm">/* Values 1-3 */</span>
<span class="p">...</span>
<span class="p">}</span> <span class="n">xzy_info_t</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">xyz_information</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="kt">uint8_t</span> <span class="nl">bita</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="cm">/* Bit A */</span>
<span class="nl">bitb</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="cm">/* Bit B */</span>
<span class="nl">bitc</span> <span class="p">:</span> <span class="mi">1</span><span class="p">;</span> <span class="cm">/* Bit C */</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="n">abc_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">struct</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">a</span><span class="p">;</span> <span class="cm">/* Value A */</span>
<span class="kt">int</span> <span class="n">b</span><span class="p">;</span> <span class="cm">/* Value B */</span>
<span class="kt">int</span> <span class="n">c</span><span class="p">;</span> <span class="cm">/* Value C */</span>
<span class="p">};</span> <span class="cm">/* Un-named structure field */</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">xyz_info_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="kt">int</span> <span class="n">val1</span><span class="p">;</span> <span class="cm">/* Value 1 */</span>
<span class="kt">int</span> <span class="n">val2</span><span class="p">;</span> <span class="cm">/* Value 2 */</span>
<span class="kt">int</span> <span class="n">val3</span><span class="p">;</span> <span class="cm">/* Value 3 */</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This is discouraged</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="k">struct</span> <span class="n">xyz_info_s</span> <span class="n">xzy_info_t</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p>The use of typedef’ed structures is acceptable but discouraged.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">xyz_info_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="kt">uint8_t</span> <span class="nl">bita</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="cm">/* Bit A */</span>
<span class="kt">uint8_t</span> <span class="nl">bitb</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="cm">/* Bit B */</span>
<span class="kt">uint8_t</span> <span class="nl">bitc</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="cm">/* Bit C */</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This is discouraged</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">abc_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">struct</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">a</span><span class="p">;</span> <span class="cm">/* Value A */</span>
<span class="kt">int</span> <span class="n">b</span><span class="p">;</span> <span class="cm">/* Value B */</span>
<span class="kt">int</span> <span class="n">c</span><span class="p">;</span> <span class="cm">/* Value C */</span>
<span class="p">}</span> <span class="n">abc</span><span class="p">;</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<p>The use of structures defined within other structures is acceptable provided that they define named fields.
The general practice of defining a structure within the scope of another structure, however, is still but discouraged in any case.
The following is preferred:</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">abc_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="kt">int</span> <span class="n">a</span><span class="p">;</span> <span class="cm">/* Value A */</span>
<span class="kt">int</span> <span class="n">b</span><span class="p">;</span> <span class="cm">/* Value B */</span>
<span class="kt">int</span> <span class="n">c</span><span class="p">;</span> <span class="cm">/* Value C */</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="unions">
<h3>Unions<a class="headerlink" href="#unions" title="Permalink to this headline"></a></h3>
<p><strong>Union and Field Names</strong>. Naming of unions and fields within unions
follow the same naming rules as for <a class="reference external" href="#structures">structures and structure
fields</a>. The only difference is that the suffix <code class="docutils literal notranslate"><span class="pre">_u</span></code>
is used to identify unions.</p>
<p><strong>Other Applicable Coding Standards</strong>. See sections related to <a class="reference external" href="#lines">line
formatting</a>, <a class="reference external" href="#braces">use of braces</a>,
<a class="reference external" href="#indentation">indentation</a>, and <a class="reference external" href="#comments">comments</a>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This is acceptable</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">union</span> <span class="n">xyz_union_u</span> <span class="cm">/* All unions must be named */</span>
<span class="p">{</span>
<span class="kt">uint8_t</span> <span class="n">b</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span> <span class="cm">/* Byte values. */</span>
<span class="kt">uint16_t</span> <span class="n">h</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span> <span class="cm">/* Half word values. */</span>
<span class="kt">uint32_t</span> <span class="n">w</span><span class="p">;</span> <span class="cm">/* Word Value. */</span>
<span class="p">};</span>
<span class="k">typedef</span> <span class="k">union</span> <span class="n">xyz_union_u</span> <span class="n">xzy_union_t</span><span class="p">;</span>
</pre></div>
</div>
</div>
<p>The use of typedef’ed unions is acceptable but discouraged.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is preferred</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">xyz_info_s</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">union</span>
<span class="p">{</span>
<span class="kt">uint8_t</span> <span class="n">b</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span> <span class="cm">/* Byte values. */</span>
<span class="kt">uint16_t</span> <span class="n">h</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span> <span class="cm">/* Half word values. */</span>
<span class="kt">uint32_t</span> <span class="n">w</span><span class="p">;</span> <span class="cm">/* Word Value. */</span>
<span class="p">}</span> <span class="n">u</span><span class="p">;</span> <span class="cm">/* All union fields must be named */</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
<p><strong>NOTE:</strong> Note that the union fields within structures are often named
<code class="docutils literal notranslate"><span class="pre">u</span></code>. This is another exception to the prohibition against using single
character variable and field names. The short field name <code class="docutils literal notranslate"><span class="pre">u</span></code> clearly
identifies a union field and prevents the full name of the union value
from being excessively long.</p>
</div>
<div class="section" id="enumerations">
<h3>Enumerations<a class="headerlink" href="#enumerations" title="Permalink to this headline"></a></h3>
<p><strong>Enumeration Naming</strong>. Naming of enumerations follow the same naming
rules as for <a class="reference external" href="#structures">structure</a> and <a class="reference external" href="#unions%22">union</a>
naming. The only difference is that the suffix <code class="docutils literal notranslate"><span class="pre">_e</span></code> is used to
identify an enumeration.</p>
<p><strong>Enumeration Value Naming</strong>. Enumeration values, however, following a
naming convention more similar to <a class="reference external" href="#macros">macros</a>.</p>
<ul class="simple">
<li><p><strong>Uppercase</strong>. Enumeration values are always in upper case.</p></li>
<li><p><strong>Use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code> <strong>encouraged</strong>. Unlike other naming, use of the
underscore character <code class="docutils literal notranslate"><span class="pre">_</span></code> to break up enumeration names is
encouraged.</p></li>
<li><p><strong>Prefix</strong>. Each value in the enumeration should begin with an
upper-case prefix that identifies the value as a member of the
enumeration. This prefix should, ideally, derive from the name of the
enumeration.</p></li>
<li><p><strong>No dangling commas</strong>. There should be no dangling comma on the
final value of the enumeration. The most commonly used tool chain are
tolerant of such dangling commas, but others will not.</p></li>
</ul>
<p><strong>Other Applicable Coding Standards</strong>. See sections related to <a class="reference external" href="#lines">line
formatting</a>, <a class="reference external" href="#braces">use of braces</a>,
<a class="reference external" href="#indentation">indentation</a>, and <a class="reference external" href="#comments">comments</a>.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">enum</span> <span class="n">xyz_state_e</span>
<span class="p">{</span>
<span class="n">XYZ_STATE_UNINITIALIZED</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* Uninitialized state. */</span>
<span class="n">XYZ_STATE_WAITING</span><span class="p">,</span> <span class="cm">/* Waiting for input state. */</span>
<span class="n">XYZ_STATE_BUSY</span><span class="p">,</span> <span class="cm">/* Busy processing input state. */</span>
<span class="n">XYZ_STATE_ERROR</span><span class="p">,</span> <span class="cm">/* Halted due to an error. */</span>
<span class="n">XYZ_STATE_TERMINATING</span><span class="p">,</span> <span class="cm">/* Terminating stated. */</span>
<span class="n">XYZ_STATE_TERMINATED</span> <span class="cm">/* Terminating stated. */</span>
<span class="p">};</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="c-pre-processor-macros">
<h3>C Pre-processor Macros<a class="headerlink" href="#c-pre-processor-macros" title="Permalink to this headline"></a></h3>
<p><strong>Coding Standard:</strong></p>
<p><strong>Macro Naming</strong>. Macro naming following a naming convention similar to
the naming of <a class="reference external" href="#enumerations">enumeration values</a>.</p>
<ul class="simple">
<li><p><strong>Uppercase</strong>. Macro names are always in upper case.</p></li>
<li><p><strong>Lowercase Exceptions</strong>. There are a few lower case values in NuttX
macro names. Such as a lower-case <code class="docutils literal notranslate"><span class="pre">p</span></code> for a period or decimal point
(such as <code class="docutils literal notranslate"><span class="pre">VOLTAGE_3p3V</span></code>). I have also used lower-case <code class="docutils literal notranslate"><span class="pre">v</span></code> for a
version number (such as <code class="docutils literal notranslate"><span class="pre">CONFIG_NET_IPv6</span></code>). However, these are
exceptions to the rule rather than illustrating a rule.</p></li>
<li><p><strong>Macros that could be functions</strong>. Lower-case macro names are also
acceptable if the macro is a substitute for a function name that
might be used in some other context. In that case, normal function
naming applies.</p></li>
<li><p><strong>Use of</strong> <code class="docutils literal notranslate"><span class="pre">_</span></code> <strong>encouraged</strong>. Unlike other naming, use of the
underscore character <code class="docutils literal notranslate"><span class="pre">_</span></code> to break up macro names is encouraged.</p></li>
<li><p><strong>Prefix</strong>. Each related macro value should begin with an upper-case
prefix that identifies the value as part of a set of values (and also
to minimize the likelihood of naming collisions).</p></li>
<li><p><strong>Single space after</strong> <code class="docutils literal notranslate"><span class="pre">#define</span></code>. A single space character should
separate the <code class="docutils literal notranslate"><span class="pre">#define</span></code> from the macro name. Tabs are never used.</p></li>
<li><p><strong>Normal commenting rules</strong>. Normal commenting rules apply.</p></li>
<li><p><strong>Line continuations</strong>. Macro definitions may be continued on the
next line by terminating the line with the <code class="docutils literal notranslate"><span class="pre">\</span></code> character just
before the newline character. There should be a single space before
the <code class="docutils literal notranslate"><span class="pre">\</span></code> character. Aligned <code class="docutils literal notranslate"><span class="pre">\</span></code> characters on multiple line
continuations are discouraged because they are a maintenance problem.</p></li>
<li><p><strong>Parentheses around macro argument expansions</strong>. Macros may have
argument lists. In the macros expansion, each argument should be
enclosed in parentheses.</p></li>
<li><p><strong>Real statements</strong>. If a macro functions as a statement, then the
macro expansion should be wrapped in <code class="docutils literal notranslate"><span class="pre">do</span> <span class="pre">{</span> <span class="pre">...</span> <span class="pre">}</span> <span class="pre">while</span> <span class="pre">(0)</span></code> to
assume that the macros is, indeed, a statement.</p></li>
<li><p><strong>Magic numbers are prohibited in code</strong>. Any numeric value is not
intuitively obvious, must be properly named and provided as either a
pre-processor macro or an enumeration value.</p></li>
<li><p><strong>Side effects</strong>. Be careful of side effects.</p></li>
<li><p><strong>Indentation</strong>. See the <a class="reference external" href="#indentation">Indentation of Pre-Processor
Lines</a> requirements above.</p></li>
</ul>
<p><strong>Other Applicable Coding Standards</strong>. See sections related to <a class="reference external" href="#lines">line
formatting</a>, <a class="reference external" href="#indentation">indentation</a>, and
<a class="reference external" href="#comments">comments</a>.</p>
<div class="admonition error">
<p class="admonition-title">Error</p>
<p>This is incorrect</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define max(a,b) a &gt; b ? a : b</span>
<span class="cp">#define ADD(x,y) x + y</span>
<span class="cp">#ifdef HAVE_SOMEFUNCTION</span>
<span class="kt">int</span> <span class="nf">somefunction</span><span class="p">(</span><span class="k">struct</span> <span class="n">somestruct_s</span><span class="o">*</span> <span class="n">psomething</span><span class="p">);</span>
<span class="cp">#else</span>
<span class="cp">#define SOMEFUNCTION() (0)</span>
<span class="cp">#endif</span>
<span class="cp"># define IS_A_CAT(c) ((c) == A_CAT)</span>
<span class="cp">#define LONG_MACRO(a,b) \</span>
<span class="cp"> { \</span>
<span class="cp"> int value; \</span>
<span class="cp"> value = b-1; \</span>
<span class="cp"> a = b*value; \</span>
<span class="cp"> }</span>
<span class="cp">#define DO_ASSIGN(a,b) a = b</span>
</pre></div>
</div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>This is correct</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define MAX(a,b) (((a) &gt; (b)) ? (a) : (b))</span>
<span class="cp">#define ADD(x,y) ((x) + (y))</span>
<span class="cp">#ifdef HAVE_SOMEFUNCTION</span>
<span class="kt">int</span> <span class="nf">somefunction</span><span class="p">(</span><span class="k">struct</span> <span class="n">somestruct_s</span><span class="o">*</span> <span class="n">psomething</span><span class="p">);</span>
<span class="cp">#else</span>
<span class="cp"># define somefunction(p) (0)</span>
<span class="cp">#endif</spa