Embedded C Coding Standard Embedded C Coding Standard
Total Page:16
File Type:pdf, Size:1020Kb
Keeps Bugs Out EmbeddedEmbedded CC CodingCoding StandardStandard by Michael Barr ® Edition: BARR-C: 2018 | barrgroup.com BARR-C:2018 Embedded C Coding Standard Embedded C Coding Standard ii Embedded C Coding Standard Embedded C Coding Standard by Michael Barr Edition: BARR-C:2018 | barrgroup.com iii Embedded C Coding Standard Embedded C Coding Standard by Michael Barr Copyright © 2018 Integrated Embedded, LLC (dba Barr Group). All rights reserved. Published by: Barr Group 20251 Century Blvd, Suite 330 Germantown, MD 20874 This book may be purchased in print and electronic editions. A free online edition is also available. For more information see https://barrgroup.com/coding-standard. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Compliance with the coding standard rules in this book neither ensures against software defects nor legal liability. Product safety and security are your responsibility. Barr Group, the Barr Group logo, The Embedded Systems Experts, and BARR-C are trademarks or registered trademarks of Integrated Embedded, LLC. Any other trademarks used in this book are property of their respective owners. ISBN-13: 978-1-72112-798-6 ISBN-10: 1-72112-798-4 iv Embedded C Coding Standard Document License By obtaining Barr Group’s copyrighted “Embedded C Coding Standard” (the “Document”), you are agreeing to be bound by the terms of this Document License (“Agreement”). 1. RIGHTS GRANTED. For good and valuable consideration, the receipt, adequacy, and sufficiency of which is hereby acknowledged, Barr Group grants you a license to use the Document as follows: You may publish the Document for your own internal use and for the use of your internal staff in conducting your business only. By obtaining a copy of the Document, you expressly agree to the terms of this Agreement. 2. RIGHTS RESERVED. Except as expressly permitted herein, no rights in or to the Document are granted to you. Barr Group (on behalf of itself and its Affiliates) retains all other rights in and to the Document, anywhere in the world. Specifically, you acknowledge and agree: (i) that Barr Group holds the copyright for the Document; (ii) that the Document and any works derived therefrom may not be published or shared except as permitted in Section 1 above; (iii) that you shall take appropriate measures regarding limitations on publishing and sharing, including the incorporation of appropriate copyright markings and notations on limitations of publishing and sharing, as described in this Agreement. If you fail to include these required copyright markings and notations, you agree that such copies are unauthorized copies of the copyright material and that these unauthorized copies infringe Barr Group’s copyright of the Document. If you prepare any new works, translations or derivative works from the Document, you hereby agree that your rights for these new works, translations, or derivative works are limited to your private (e.g., company internal) use. 3. DISCLAIMERS AND LIMITATION ON LIABILITY. You agree to hold harmless, defend, and indemnify Barr Group, its owners and officers, employees, and subcontractors, to the full extent permitted by law, for any claims brought in relation to, or use of, the Document. You bear full responsibility for determining whether the contents of the Document and its derivations are safe and appropriate for the purposes of your uses. In addition: (a) THE DOCUMENT IS PROVIDED “AS IS” WITHOUT REPRESENTATION OR WARRANTY OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, BARR GROUP EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, STATUTORY, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED, TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT WITH REGARD TO THE DOCUMENT. (b) In no event shall Barr Group be liable for any damages whatsoever (including, but not limited to, special, incidental, consequential, or indirect damages for personal injury, any and all other third party claims, loss of business profits, business interruption, loss of business information, or any other pecuniary loss, attorneys’ fees and litigation costs) resulting from or related to this Agreement or the use of the Document (whether or not Barr Group knew or should have known of the possibility of any such damages) or any third party claim that the Document as delivered to you hereunder, infringes any copyright or trademark right or misappropriates any trade secret of any third party. 4. GOVERNING LAW; JURISDICTION; VENUE. This Agreement shall be construed in accordance with the laws of the State of Maryland without giving effect to its conflicts of laws rules. Any dispute arising out of, connected with or relating to this Agreement, shall be brought in either the state court of general jurisdiction in Montgomery County or the U.S. District Court for the District of Maryland, which the parties agree shall be the venue and exclusive forum in which to adjudicate any case or controversy arising from or relating to this Agreement. Each party irrevocably submits to the jurisdiction of such courts and waives any objections to either the jurisdiction of or venue in such courts. In the event Barr Group prevails in any dispute arising out of, connected with, or relating to this Agreement, you shall pay Barr Group any and all costs associated with such dispute, including, without limitation, reasonable attorneys’ fees. 5. SEVERABILITY; CONSIDERATION. Every part of this Agreement shall be considered severable. If for any reason any party of this Agreement is held to be invalid, that determination shall not impair or otherwise affect the enforceability of the other parts of this Agreement. By obtaining the Document, you acknowledge and agree that the consideration underlying this Agreement is the mutual promises and covenants herein and your use of, and access to, the Document subject to the terms and conditions of this Agreement. v Embedded C Coding Standard vi Embedded C Coding Standard Contents Introduction ........................................................................................................................ 1 Purpose of the Standard ................................................................................................ 1 Guiding Principles ......................................................................................................... 2 MISRA C ......................................................................................................................... 4 C++ vs. C ......................................................................................................................... 5 Enforcement Guidelines ................................................................................................ 5 Deviation Procedure ...................................................................................................... 6 Customization ................................................................................................................ 7 Acknowledgements ....................................................................................................... 7 1 General Rules .............................................................................................................. 8 1.1 Which C? ............................................................................................................. 8 1.2 Line Widths ......................................................................................................... 9 1.3 Braces ................................................................................................................. 10 1.4 Parentheses........................................................................................................ 11 1.5 Common Abbreviations ................................................................................... 12 1.6 Casts ................................................................................................................... 13 1.7 Keywords to Avoid .......................................................................................... 14 1.8 Keywords to Frequent ...................................................................................... 15 2 Comment Rules ........................................................................................................ 17 2.1 Acceptable Formats .......................................................................................... 17 2.2 Locations and Content ..................................................................................... 18 vii Embedded C Coding Standard 3 White Space Rules .................................................................................................... 21 3.1 Spaces ................................................................................................................ 21 3.2 Alignment.......................................................................................................... 23 3.3 Blank Lines ........................................................................................................ 24 3.4 Indentation ........................................................................................................ 25 3.5 Tabs .................................................................................................................... 27 3.6 Non-Printing