Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Similar documents
UNIT- 3 Introduction to C++

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language

CSCI 171 Chapter Outlines

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Computers Programming Course 5. Iulian Năstac

INTRODUCTION 1 AND REVIEW

Fundamental of Programming (C)

CS201 - Introduction to Programming Glossary By

Short Notes of CS201

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

UNIT-2 Introduction to C++

Full file at

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

APPENDIX A : Example Standard <--Prev page Next page -->

UEE1302 (1102) F10: Introduction to Computers and Programming


>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

A Fast Review of C Essentials Part I

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Objectives. In this chapter, you will:

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

6.096 Introduction to C++ January (IAP) 2009

EL6483: Brief Overview of C Programming Language

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Fundamentals of Programming

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Flow Control. CSC215 Lecture

Chapter 2 Basic Elements of C++

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

The New C Standard (Excerpted material)

Programming in C++ 4. The lexical basis of C++

CS Programming In C

C Formatting Guidelines

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Chapter 2: Introduction to C++

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Syntax and Variables

Tokens, Expressions and Control Structures

Pace University. Fundamental Concepts of CS121 1

Typescript on LLVM Language Reference Manual

C Language Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Part I Part 1 Expressions

The C++ Language. Arizona State University 1

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Information Science 1

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

LECTURE 02 INTRODUCTION TO C++

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Procedures, Parameters, Values and Variables. Steven R. Bagley

EE 382 Style Guide. March 2, 2018

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Creating a C++ Program

Full file at C How to Program, 6/e Multiple Choice Test Bank

Unit 3. Operators. School of Science and Technology INTRODUCTION

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

QUIZ: What value is stored in a after this

The New C Standard (Excerpted material)

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Programming and Data Structures

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

High Performance Computing in C and C++

Variables and Operators 2/20/01 Lecture #

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 3: Operators, Expressions and Type Conversion

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

C Language, Token, Keywords, Constant, variable

BITG 1233: Introduction to C++

C Programming Language (Chapter 2 of K&R) Variables and Constants

Formatting & Style Examples

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Operators. Java operators are classified into three categories:

from Appendix B: Some C Essentials

ARG! Language Reference Manual

Transcription:

C/C++ Notes C/C++ Coding Style Guidelines -0 Ray Mitchell

C/C++ Notes 0 0 0 0 NOTE G. C/C++ Coding Style Guidelines. Introduction The C and C++ languages are free form, placing no significance on the column or line where a token is located. This means that in the extreme a program could be written either all on one line with token separators only where required, or with each token on a separate line with blank lines in between. Such variations in programming style can be compared to accents in a spoken language. As an accent gets stronger the meaning of the conversation becomes less understandable, eventually becoming gibberish. This document illustrates some of the most commonly accepted conventions used in writing C/C++ programs, providing the consistent guidelines needed to write accentless code.. Implementation Files and Header Files Implementation files typically have a.c or.cpp extension and will minimally contain any needed non-inline function definitions and external variable definitions, although they often also contain many of same types of things contained in header files. Header files typically have a.h or no extension and are included in other files via a #include directive. They typically contain items like macro definitions, inline function definitions, function prototypes, external variable referencing declarations, templates, typedefs, and type definitions for classes, structures, unions, and enumerations. They also contain #include directives when necessary to support other items in the file. Header files must never contain function definitions, external variable definitions, using directives or statements, or run time code that is not part of a macro or an inline function. In applications where multiple implementation files include many of the same header files it is sometimes acceptable to place all of these #include directives in a single header file and include it instead. Standard header files are typically supplied with the compiler, while rd-party library vendors provide custom header files to support their products. In addition, programmers are always free to create their own custom header files to meet their needs. Header files must always be logically organized, includable in any order, and each must implement an Include Guard to prevent the multiple inclusion of its contents in another file, even if the header is included multiple times.. File Organization The suggested order for some common file items is as follows, with a blank line placed between each group. The most important consideration is that a something that relies on something be placed after it. Except for item, which is always required, not all files will contain all of these while some files may contain items not shown:. A block comment containing information about the contents and functionality of the file, the developer, the revision history, and anything that might be helpful in understanding and maintaining it;. #include directives;. using statements/directives (C++);. #define directives (C);. typedefs and custom data type definitions;. const variable declarations (C++);. External variable declarations;. Function prototypes;. Function definitions in some meaningful order, with a block comment before each describing its syntax and purpose. -0 Ray Mitchell

C/C++ Notes Appendix G 0 0 0 0 NOTE G.. Comments Comments should be used wherever there might be a question about the workings or purpose of an algorithm, statement, macro definition, or anything that would not be obvious to a programmer unfamiliar with the code. It should never be necessary to reverse-engineer a program. Comments should explain algorithmic operations, not semantics. For example, an appropriate comment for the statement distance = rate * time; might be position of projectile, while something like multiply rate by time and assign to distance says nothing useful. With a wise choice of identifiers code can often be made somewhat self-documenting, thereby reducing the need for as many comments. The format of a comment is normally determined by its length. Comments occupying more than one line, such as a file title block or algorithm description, should be in block comment form. Except for partial-line comments, comments should be placed prior to and aligned with the code they comment as follows. Depending upon the compiler, C++ style comments could cause compatibility issues in C/C0 code. A C style block comment (also acceptable in C++): /* * Use a block comment whenever comments that occupy more than one line are needed. Note * the opening / is aligned with the code being commented, all asterisks are aligned with each * other, and that all comment text is left aligned. */ for (nextvalue = getchar(); nextvalue!= EOF; nextvalue = getchar()) An equivalent C++ style block comment (also acceptable in C since C): // // Use a block comment whenever comments that occupy more than one line are needed. Note // the opening / is aligned with the code being commented, all // are aligned with each other, and // that all comment text is left aligned. // for (nextvalue = getchar(); nextvalue!= EOF; nextvalue = getchar()) C and C++ style full-line comments: /* This is a full-line C style comment. */ // This is a full-line C++ style comment. for (nextvalue = getchar(); nextvalue!= EOF; nextvalue = getchar()) It is possible to over comment a program to the point of making it unreadable. This is especially true when code lines and comment lines are intermixed. Many comments can be reduced to the point of being small enough to fit to the right of the code being commented while still conveying meaning. To be most readable code should reside on the left side of a page with comments on the right, opposite the code they comment. Whenever possible all such comments should start in the same column as each other, as far to the right as possible. Deciding on the appropriate column is a compromise and there will usually be exceptions in any given program. Using tabs instead of spaces when positioning such comments reduces the effect of making minor code changes but may not be interpreted correctly by the printer. The following illustrates these partial-line comments: while ((nextvalue = getchar())!= EOF) /* while source file has characters */ if (nextvalue == '.') /* got sentence terminator */ break; /* don't read any more characters */ // must continue reading sentence ++charcount; // update characters read in sentence -0 Ray Mitchell

C/C++ Notes 0 0 0 0 NOTE G. The following examples use exactly the same code but with the comments (if any) placed differently. Which programmer would you hire or be most willing to maintain code for? WRONG NO COMMENTS while ((nextvalue = getchar())!= EOF) if (nextvalue == '.') break; ++charcount; WRONG CLUTTERED & HARD TO READ while ((nextvalue = getchar())!= EOF)/* while source file has characters */ if (nextvalue == '.')/* got sentence terminator */ break; /* don't read any more characters */ /* must continue reading sentence */ ++charcount; /* update characters read in sentence */ CORRECT FULL LINE COMMENTS /* while source file has characters */ while ((nextvalue = getchar())!= EOF) /* got sentence terminator */ if (nextvalue == '.') /* don't read any more characters */ break; /* must continue reading sentence */ /* update characters read in sentence */ ++charcount; CORRECT PARTIAL LINE COMMENTS while ((nextvalue = getchar())!= EOF) /* while source file has characters */ if (nextvalue == '.') /* got sentence terminator */ break; /* don't read any more characters */ /* must continue reading sentence */ ++charcount; /* update characters read in sentence */ -0 Ray Mitchell

C/C++ Notes Appendix G 0 0 0 0 NOTE G.. Literals A magic number is defined as any literal (numeric, character, or string) embedded in a program s code or comments. They usually make programs cryptic and difficult to maintain because their meaning is not obvious and, if they must be changed, the likelihood of missing one or changing the wrong one is high. There are a few cases, however, where magic numbers are acceptable, including: 0 & as array indices or loop start/end values, as an increment/decrement value, as way to double/halve a value or as a divisor to check for odd/even, coefficients in some mathematical formulae, some informational strings, cases dictated by common sense. In general, however, the #define directive (in C), const-qualified variables (in C++), or enumerated data (C and C++) should be used to associate meaningful names with literals. This permits values to be changed everywhere in a program by making a change in only one place. Avoid, however, the pitfall of choosing names that reflect the value they represent, such as #define SIX. This is as bad as directly coding itself. Would you ever define SIX to be anything other than? Because an implementation may define the macro NULL as a pointer type, it must never be used in other than pointer contexts.. Naming Conventions. Choose meaningful names that reflect usage whenever possible and practical.. Function & function-like macro names should be verbs or ask questions; all other names should be nouns or state facts.. To avoid conflicts with internal names, leading or trailing underscores should be avoided in application programs.. Object-like macro, const-qualified variable, and typedef names should be in upper case.. Function, function-like macro, and class/struct/union/enum tag names should be in Pascal case (i.e., PascalCaseName).. All other names should be in camel case, (i.e., camelcasename).. Compound Statements (Block Statements) A compound (block) statement is defined as zero or more statements enclosed in curly braces. Although initializer lists are also enclosed in braces they are not compound statements. Compound statements are a required part of function, structure, class, union, and enumeration definitions as well as switch statements, but they are optional with if,, for, while, and do statements unless more than one statement is to be associated with those statements. Here are some examples: Optional Compound Statement Not Used Optional Compound Statement Is Used Compound Statement Required for (...) for (...) for (...) velocity = ; acceleration = ; velocity = ; velocity = ; height = ; acceleration = ; acceleration = ; -0 Ray Mitchell

C/C++ Notes 0 0 0 0 NOTE G.. Placement of Braces A point of contention among some C/C++ programmers is the placement of the braces used for compound statements and initializer lists. The two most popular formats are shown on the left and right below and are known as the brace under and brace after (K&R) styles, respectively. Choose one and use it exclusively and consistently. Do not intermix the two or use a different variation. The braces are sometimes omitted if only one statement is needed with if,, for, while, or do: int SomeFunction(void) int SomeFunction (void) (Both forms are the same!) declarations/statements declarations/statements if (...) if (...) declarations/statements declarations/statements if (...) declarations/statements if (...) declarations/statements declarations/statements declarations/statements for or while (...) for or while (...) declarations/statements declarations/statements do do declarations/statements declarations/statements while (...); while(...); switch (...) switch (...) declarations declarations case... : case... : statements statements break; break; struct or class or union or enum tag struct or class or union or enum tag declarations declarations ; ; double factors[] = double factors[] = initializer, initializer,... initializer, initializer,... ; ; 0-0 Ray Mitchell

C/C++ Notes Appendix G 0 0 0 0 NOTE G. struct/class/union/enum type definitions and initialized variable declarations may be placed entirely on the same line if they will fit, but this is not appropriate for any of the other constructs: struct or class or union or enum tag declaration, declaration, ; double factors[] = initializer, initializer,... ;. Indenting Indenting is used strictly to enhance a program s human readability and is totally ignored by the compiler. An indent signifies the association of one or more statements with a previous, less indented construct, as in the following examples: if (...) for (...) int main(void) while (..) x = ; int ch = 'A'; printf(...); x = ; ++d; putchar(ch); if (...) return EXIT_SUCCESS; z = y; do y = ; int t = ; x = y + ; while (...); ++v; The rules for how and when to indent are simple:. Braces must be aligned according to the placement formats previously discussed.. All declarations and other statements associated with a construct must be indented equally from the left edge of that construct.. The width used for all indents must be consistent throughout any program. One final consideration is the width of each indent. A default tab stop is spaces wide on some systems, but this is too wide for most programs because the code ends up going off the right edge of the screen/paper or wrapping around. Because of this an indent width of or spaces (or at least / inch) is recommended instead. Widths smaller than this tend to be hard to discern while larger values run out of room more quickly. Actual spaces should be used instead of hard tab characters because the system printer often has no way of knowing the editor's tab setting. Thus, it often assumes that it is and produces a program listing whose indenting does not match that seen within the editor itself.. Multiple Statements on One Line Do not put more than one statement on a line. Similarly, put the body of an if,, for, while, do, or case on the next line. Chained assignments such as x = y = z = 0; are permissible as long as all operands are logically related. Multiple variables of the same type may be declared on the same line, comma separated, unless a comment is required. The following examples illustrate some acceptable and non-acceptable formats: -0 Ray Mitchell

C/C++ Notes 0 0 0 0 NOTE G. Do Don't x = ; x = ; y = printf(...); Delete(); y = printf(...); Delete(); for (idx = 0; idx < MAX; ++idx) for (idx = 0; idx < MAX; ++idx) printf("%d", idx); printf("%d", idx); if (y < x) if (y < x) printf("%d\n", x); printf("%d\n", x); printf("%d\n", y); printf("%d\n", y); case 'A': case 'A': putchar('a'); break; (See below) putchar('a'); break; A common exception is where multiple switch cases are almost identical. Readability may actually be enhanced by grouping them as: case 'A': putchar('a'); break; case 'B': putchar('b'); break; case 'C': putchar('c'); break; case 'D': putchar('d'); break;. The if Convention If an if follows an, separated by only whitespace, the if should be placed on the line with the, thereby forming the standard C/C++ if construct. Do Don't if (a > b) if (a > b) ++a; ++a; if (a < x) ++b; if (a < x) ++b;. Functions Each function definition should be preceded by a block comment that describes its syntax and what it does. A function's return type must always be specified and never defaulted to type int by omitting it. Functions not returning a value must use the keyword void. Placing a return type on its own line in column is common. C functions with no parameters must use void as the parameter list. Although C++ functions with no parameters may also use void for this purpose, it is more common to leave their parameter lists empty. Either the definition of a function or its prototype must visible to the caller before the function is called. Explicitly declaring a function as external by using the keyword extern is obsolete. Prototypes for user functions needed by more than one file and all library functions, such as those in the standard library, must be kept in header files that are included by any files needing them. Do not duplicate the prototype in each file. In multi-file programs all functions not shared with other files should be declared static to limit their scope. -0 Ray Mitchell

C/C++ Notes Appendix G 0 0 0 NOTE G.. External Variables Referencing declarations of external variables used by more than one file must be kept in header files that are included by any files needing them. Do not by duplicate the declaration in each file. In multi-file programs, all external variables (except const-qualified external variables in C++) not shared with other files should be declared static to limit their scope.. Automatic Variables Automatic variables should be declared as close to the point of use as is practical and, if initialization is required, also initialized as close to the point of use as is practical. A program that initializes automatic variables when declared but doesn't use them until much later is difficult to read. The use of extremely large automatic aggregate objects (i.e., arrays, structures, classes) can cause compiler/run-time problems due to the amount of stack space required, and initializing such objects can add significant run time overhead. For similar reasons functions should avoid the arbitrary passing/returning of aggregates. Instead, pass/return pointers or references to them. Making local objects static can sometimes solve stack size issues but can also result in more overall memory usage and can prevent the implementation of multi-threaded applications.. Operators All ternary and binary operators except class/structure/union member operators should be separated from their operands by spaces. Some judgment is called for in the case of complex expressions, which may be clearer if the inner operators are not surrounded by spaces and the outer ones are, for example: if (x=y && <t ++p) Spaces should be placed after, but not before, all commas and semicolons. They should also be placed after keywords that are followed by expressions in parentheses, with the exception of sizeof and return. On the other hand, macros with arguments must not and function calls should not have a space between the name and the left parenthesis. Unary operators should not be separated from their single operand. Compound assignments and pre/post increment/decrement expressions should always be used instead of their equivalent longer forms. The shorter expressions are evaluated as is and are not expanded to the longer forms. Parentheses around the entire right side of such expressions are not needed, as the following illustrates: Do Don't x += x = x + x++ or ++x x = x + or x += x >>= x = x >> x /= + y x = x / ( + y) or x /= ( + y) -0 Ray Mitchell

C/C++ Notes NOTE G.. Portability Beware of making assumptions about:. The number of bits in a data type;. Byte ordering (big endian vs. little endian);. Bit-field ordering (left-to-right vs. right-to-left);. The size of different pointer types;. The compatibility of pointers with arithmetic types;. The internal representation of negative integer numbers;. The internal representation of floating point numbers;. The operation of a right shift on negative integer numbers (arithmetic vs. logical);. The padding of structures/classes/unions;. The machine's character set (ASCII, EBCDIC, etc.);. The evaluation of expressions having side effects when used as macro arguments;. The order of evaluation of expressions not involving sequence point operators, including expressions used as function arguments. -0 Ray Mitchell