qmk_firmware/coding_conventions_c.html

37 lines
102 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Coding Conventions (C) | QMK Firmware</title>
<meta name="description" content="Documentation for QMK Firmware">
<meta name="generator" content="VitePress v1.1.3">
<link rel="preload stylesheet" href="/assets/style.No8zk9aC.css" as="style">
<script type="module" src="/assets/app.B7PFkdfJ.js"></script>
<link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
<link rel="modulepreload" href="/assets/chunks/theme.CkLEqEBY.js">
<link rel="modulepreload" href="/assets/chunks/framework.B9AX-CPi.js">
<link rel="modulepreload" href="/assets/coding_conventions_c.md.Bi7EH0Zk.lean.js">
<script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
<script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
</head>
<body>
<div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar has-sidebar top" data-v-ae24b3ad data-v-ccf7ddec><div class="wrapper" data-v-ccf7ddec><div class="container" data-v-ccf7ddec><div class="title" data-v-ccf7ddec><div class="VPNavBarTitle has-sidebar" data-v-ccf7ddec data-v-ab179fa1><a class="title" href="/" data-v-ab179fa1><!--[--><!--]--><!--[--><!--[--><!--[--><img class="VPImage dark logo" src="/qmk-logo-dark.svg" alt data-v-8426fc1a><!--]--><!--[--><img class="VPImage light logo" src="/qmk-logo-light.svg" alt data-v-8426fc1a><!--]--><!--]--><!--]--><span data-v-ab179fa1>QMK Firmware</span><!--[--><!--]--></a></div></div><div class="content" data-v-ccf7ddec><div class="content-body" data-v-ccf7ddec><!--[--><!--]--><div class="VPNavBarSearch search" data-v-ccf7ddec><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-ccf7ddec data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-7f418b0f data-v-9c663999><!--[--><span data-v-9c663999>Home</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-ccf7ddec data-v-e6aabb21><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="Switch to dark theme" aria-checked="false" data-v-e6aabb21 data-v-d1f28634 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-d1f28634></span><span class="vpi-moon moon" data-v-d1f28634></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-ccf7ddec data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://reddit.com/r/olkb" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-eee4e7cb><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="50px" height="50px"><path d="M 29 3 C 28.0625 3 27.164063 3.382813 26.5 4 C 25.835938 4.617188 25.363281 5.433594 25 6.40625 C 24.355469 8.140625 24.085938 10.394531 24.03125 13.03125 C 19.234375 13.179688 14.820313 14.421875 11.28125 16.46875 C 10.214844 15.46875 8.855469 14.96875 7.5 14.96875 C 6.089844 14.96875 4.675781 15.511719 3.59375 16.59375 C 1.425781 18.761719 1.425781 22.238281 3.59375 24.40625 L 3.84375 24.65625 C 3.3125 26.035156 3 27.488281 3 29 C 3 33.527344 5.566406 37.585938 9.5625 40.4375 C 13.558594 43.289063 19.007813 45 25 45 C 30.992188 45 36.441406 43.289063 40.4375 40.4375 C 44.433594 37.585938 47 33.527344 47 29 C 47 27.488281 46.6875 26.035156 46.15625 24.65625 L 46.40625 24.40625 C 48.574219 22.238281 48.574219 18.761719 46.40625 16.59375 C 45.324219 15.511719 43.910156 14.96875 42.5 14.96875 C 41.144531 14.96875 39.785156 15.46875 38.71875 16.46875 C 35.195313 14.433594 30.800781 13.191406 26.03125 13.03125 C 26.09375 10.546875 26.363281 8.46875 26.875 7.09375 C 27.164063 6.316406 27.527344 5.757813 27.875 5.4375 C 28.222656 5.117188 28.539063 5 29 5 C 29.460938 5 29.683594 5.125 30.03125 5.40625 C 30.378906 5.6875 30.785156 6.148438 31.3125 6.6875 C 32.253906 7.652344 33.695313 8.714844 36.09375 8.9375 C 36.539063 11.238281 38.574219 13 41 13 C 43.75 13 46 10.75 46 8 C 46 5.25 43.75 3 41 3 C 38.605469 3 36.574219 4.710938 36.09375 6.96875 C 34.3125 6.796875 33.527344 6.109375 32.75 5.3125 C 32.300781 4.851563 31.886719 4
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">enum</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> foo_state {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FOO_BAR,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FOO_BAZ,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Returns a value */</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> foo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (some_condition) {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> FOO_BAR;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h1 id="auto-formatting-with-clang-format" tabindex="-1">Auto-formatting with clang-format <a class="header-anchor" href="#auto-formatting-with-clang-format" aria-label="Permalink to &quot;Auto-formatting with clang-format&quot;"></a></h1><p><a href="https://clang.llvm.org/docs/ClangFormat.html" target="_blank" rel="noreferrer">Clang-format</a> is part of LLVM and can automatically format your code for you, because ain&#39;t nobody got time to do it manually. We supply a configuration file for it that applies most of the coding conventions listed above. It will only change whitespace and newlines, so you will still have to remember to include optional braces yourself.</p><p>Use the <a href="https://llvm.org/builds/" target="_blank" rel="noreferrer">full LLVM installer</a> to get clang-format on Windows, or use <code>sudo apt install clang-format</code> on Ubuntu.</p><p>If you run it from the command-line, pass <code>-style=file</code> as an option and it will automatically find the .clang-format configuration file in the QMK root directory.</p><p>If you use VSCode, the standard C/C++ plugin supports clang-format, alternatively there is a <a href="https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat" target="_blank" rel="noreferrer">separate extension</a> for it.</p><p>Some things (like LAYOUT macros) are destroyed by clang-format, so either don&#39;t run it on those files, or wrap the sensitive code in <code>// clang-format off</code> and <code>// clang-format on</code>.</p></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-09de1c0f><!--[--><!--]--><!----><nav class="prev-next" data-v-09de1c0f><div class="pager" data-v-09de1c0f><a class="VPLink link pager-link prev" href="/arm_debugging" data-v-09de1c0f><!--[--><span class="desc" data-v-09de1c0f>Previous page</span><span class="title" data-v-09de1c0f>ARM Debugging Guide</span><!--]--></a></div><div class="pager" data-v-09de1c0f><a class="VPLink link pager-link next" href="/compatible_microcontrollers" data-v-09de1c0f><!--[--><span class="desc" data-v-09de1c0f>Next page</span><span class="title" data-v-09de1c0f>Compatible Microcontrollers</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
<script>window.__VP_HASH_MAP__=JSON.parse("{\"changelog_20190830.md\":\"6XixyP8L\",\"changelog_20200229.md\":\"BmeRJene\",\"changelog_20200530.md\":\"q1Nx4Bsf\",\"changelog_20200829.md\":\"DPwMxUVb\",\"changelog_20201128.md\":\"cyEKi6md\",\"changelog_20210227.md\":\"D-qZNAtE\",\"changelog_20210529.md\":\"CMCDBsST\",\"changelog_20210828.md\":\"Bz_Cqhkz\",\"changelog_20211127.md\":\"CKqQnO6y\",\"changelog_20220226.md\":\"CyVoLONs\",\"changelog_20220528.md\":\"CihUA7fi\",\"changelog_20220827.md\":\"Df1nzx8O\",\"changelog_20221126.md\":\"Coqj0YNT\",\"changelog_20230226.md\":\"kWJusvXj\",\"changelog_20230528.md\":\"BQOXA7rR\",\"changelog_20230827.md\":\"CysqTdAP\",\"changelog_20231126.md\":\"D6JlaCSH\",\"changelog_20240225.md\":\"DmbHo-aA\",\"changelog_20240526.md\":\"ChevS3SM\",\"changelog_20240825.md\":\"Bim1xI09\",\"capabilities.md\":\"C8eGasTO\",\"capabilities_inc.md\":\"Pt72L4b9\",\"api_development_environment.md\":\"Co1Lkuhl\",\"api_development_overview.md\":\"iUK6GGbG\",\"api_docs.md\":\"DrPevC-R\",\"api_overview.md\":\"B3sDKDJg\",\"arm_debugging.md\":\"BiRXU6nl\",\"breaking_changes.md\":\"BIpM1JDF\",\"breaking_changes_history.md\":\"B_yw4mvF\",\"breaking_changes_instructions.md\":\"DUu8Tdu8\",\"chibios_upgrade_instructions.md\":\"BkS-xCQh\",\"cli.md\":\"Ct8-gh67\",\"cli_commands.md\":\"CijNW8-t\",\"cli_configuration.md\":\"DT4O5Qrb\",\"cli_development.md\":\"C1RMbrqW\",\"cli_tab_complete.md\":\"CT9NBEM3\",\"coding_conventions_c.md\":\"Bi7EH0Zk\",\"coding_conventions_python.md\":\"C45woQX8\",\"compatible_microcontrollers.md\":\"C3RSssel\",\"config_options.md\":\"BroSRrlf\",\"configurator_architecture.md\":\"ClRUnOlV\",\"configurator_default_keymaps.md\":\"Btb4Y_-z\",\"configurator_step_by_step.md\":\"Bhhrr-KZ\",\"configurator_troubleshooting.md\":\"CyTH_MO_\",\"contributing.md\":\"Z2KawZJM\",\"custom_matrix.md\":\"C5Wxxs18\",\"custom_quantum_functions.md\":\"CchHkQ2j\",\"data_driven_config.md\":\"CemhrDnh\",\"documentation_best_practices.md\":\"D_2t5SCy\",\"documentation_templates.md\":\"DNSTMFQT\",\"driver_installation_zadig.md\":\"CzMMyItw\",\"drivers_adc.md\":\"CdK8AtnR\",\"drivers_apa102.md\":\"D1i8N4Yl\",\"drivers_audio.md\":\"FGaarcoq\",\"drivers_aw20216s.md\":\"DFO5qaXQ\",\"drivers_eeprom.md\":\"DjbSpQAl\",\"drivers_flash.md\":\"TpToebZV\",\"drivers_gpio.md\":\"_T-XR1i8\",\"drivers_i2c.md\":\"CZ3yUqUc\",\"drivers_is31fl3218.md\":\"Bw4QYvxz\",\"drivers_is31fl3236.md\":\"kw_4WomB\",\"drivers_is31fl3729.md\":\"B05FYaEa\",\"drivers_is31fl3731.md\":\"EbaJGcwl\",\"drivers_is31fl3733.md\":\"CxiWN3qf\",\"drivers_is31fl3736.md\":\"CNsB35Bn\",\"drivers_is31fl3737.md\":\"dFrKnsKN\",\"drivers_is31fl3741.md\":\"DCbdJxE4\",\"drivers_is31fl3742a.md\":\"Bh9Q6Zt4\",\"drivers_is31fl3743a.md\":\"W_EODxfi\",\"drivers_is31fl3745.md\":\"Dd_riPpV\",\"drivers_is31fl3746a.md\":\"DaWS07Rz\",\"drivers_serial.md\":\"B9cdhvZY\",\"drivers_snled27351.md\":\"C202OTO_\",\"drivers_spi.md\":\"KJwJ1bDT\",\"drivers_uart.md\":\"D5bf2Ez7\",\"drivers_ws2812.md\":\"_cnUpPJK\",\"easy_maker.md\":\"B1e0p_KP\",\"faq_build.md\":\"CdMV-bmr\",\"faq_debug.md\":\"fagccKfw\",\"faq_general.md\":\"BS8dICfT\",\"faq_keymap.md\":\"CmJjc7AB\",\"faq_misc.md\":\"pugCgT23\",\"feature_advanced_keycodes.md\":\"Dc44pi1G\",\"feature_converters.md\":\"nbsy9vmL\",\"feature_debounce_type.md\":\"BcX3faCS\",\"feature_eeprom.md\":\"CrKIb9by\",\"feature_layers.md\":\"mZMmAI4u\",\"feature_layouts.md\":\"CC3GUG9e\",\"feature_macros.md\":\"bo5m05vP\",\"feature_userspace.md\":\"DzgyEZm9\",\"features_audio.md\":\"B4yww0JU\",\"features_auto_shift.md\":\"gbN4UbG7\",\"features_autocorrect.md\":\"BgqtSxc-\",\"features_backlight.md\":\"BtYuf8hb\",\"features_bluetooth.md\":\"DB3PFlcy\",\"features_bootmagic.md\":\"DBdRU139\",\"features_caps_word.md\":\"Cz8-Ua5R\",\"features_combo.md\":\"nQwtfYKJ\",\"features_command.md\":\"DDU6fJSP\",\"features_digitizer.md\":\"qNC7cDmj\",\"features_dip_switch.md\":\"DQ7jEipz\",\"features_dynamic_macros.md\":\"CmY3EYfK\",\"features_encoders.md\":\"Cw1N3hGV\",\"features_grave_esc.md\":\"RGWiZPw2\",\"features_haptic_feedback.md\":\"gCZCxZph\",\"features_hd4
</body>
</html>