Coding Standards for C

Similar documents
Coding Standards for C

Coding Standards for Java

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

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

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

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

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

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

C++ Programming Style Guide

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Documentation Requirements Computer Science 2334 Spring 2016

Coding Standard for ECE3090 August 24, 2005

Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

C Formatting Guidelines

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

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

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

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

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

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

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

Java Programming Style Guide

C Coding standard. Raphael kena Poss. July Introduction 2. 3 File system layout 2

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

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

Review of the C Programming Language for Principles of Operating Systems

Introduction to C. CS2023 Winter 2004

C Language Coding Standard

Basics of Java Programming

Introduction to C Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

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

C Coding Standards. Alan Bridger UK Astronomy Technology Centre Mick Brooks, Jim Pisano National Radio Astronomy Observatory

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

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

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

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

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

CSE 374 Programming Concepts & Tools

Chapter 2: Introduction to C++

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

EE 382 Style Guide. March 2, 2018

CS112 Lecture: Working with Numbers

Programming Style Guide v1.1

Lexical Considerations

Full file at

C Language, Token, Keywords, Constant, variable

CSE 333 Autumn 2014 Midterm Key

Introduction to C An overview of the programming language C, syntax, data types and input/output

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

C: How to Program. Week /Mar/05

Fundamentals of Programming

LECTURE 02 INTRODUCTION TO C++

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

INTRODUCTION 1 AND REVIEW

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Lexical Considerations

Program Organization and Comments

Review of the C Programming Language

CS 251 Intermediate Programming Coding Standards

Chapter 2 - Introduction to C Programming

IT 374 C# and Applications/ IT695 C# Data Structures

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++

1 Lexical Considerations

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

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

Fundamental of Programming (C)

Syntax and Variables

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CSCI 262 C++ Style Guide

CSCI 171 Chapter Outlines

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Formatting & Style Examples

Supplement D: Expanded Guidelines on Programming Style and Documentation

C CODING STANDARDS AND PRACTICES ENTC 489 EMBEDDED REAL TIME SOFTWARE DEVELOPMENT REVISION 1.0 CODESTD.DOC

DEPARTMENT OF MATHS, MJ COLLEGE

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS 351 Design of Large Programs Coding Standards

UNIT- 3 Introduction to C++

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Expanded Guidelines on Programming Style and Documentation

Chapter 2, Part III Arithmetic Operators and Decision Making

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

Programming for Engineers Introduction to C

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Programming in C. What is C?... What is C?

JME Language Reference Manual

Programming. C++ Basics

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Getting started with Java

The C++ Language. Arizona State University 1

BTE2313. Chapter 2: Introduction to C++ Programming

Transcription:

Why have coding standards? Coding Standards for C Version 6.1 CS480s11 It is a known fact that 80% of the lifetime cost of a piece of software goes to maintenance. Therefore it makes sense for all programs within an organization to be as consistent as possible. Code conventions also improve the readability of the software. This document specifies the coding standards for all Computer Science courses at Pacific University that use the C programming language. It is important for you to adhere to these standards in order to receive full credit on your assignments. This document is divided into four main sections: Naming Conventions Formatting Comments Printing Naming Conventions Constants A constant is to be mnemonically defined using all capital letters and underscores such as MAX_NAME_CHARS. Further, your program is to contain no "magic constants." That is, all magic constants must be #defined to make program modification easier and program readabilty better. In the case below, 100 is a magic constant and if used in several places throughout a program, can create problems if 100 is to be modified for any reason. Poor Program Style input = fopen ("scores.dat", "r");... for (indx = 0; indx < 100; indx++)... 1

Correct Program Style #define MAX_SCORES 100 #define FILE_SCORES "scores.dat" pscoresfile = fopen (FILE_SCORES, "r");... for (indx = 0; indx < MAX_SCORES; ++indx)... Notice: Constants like 0 and 1 are usually acceptable unless they represent values such as true and false in which case they should be #defined. Variable Names 1) A variable name is defined in all lowercase letters unless the variable name contains multiple names such as studentrecord. After the first word, each subsequent word has the first letter capitalized with the remainder of the word made up of lowercase letters. 2) Variable names are to be mnemonic unless the variable is being used in a for loop in which case the names i, j, k, l, m, n are acceptable names to be used. If however the nested loop is being used in conjunction with a two-dimensional array, then the names row and col should be used. 3) Global variables must begin with g so that a name such as ghashtable denotes a global variable. Any variable that is global to only one file should be declared as static and use the gl (gee-el) prefix. 4) Funtions that are visible outside a module must have function names that begin with the name (or prefix) of the module in which they are found. Functions in a module that are not visible outside that module do not need to have the module name as part of the function name and the function must be declared as static. 5) To aid in identifying the type of a variable, we will use the following prefixes. Type Indicator is a Text Prefix Variable Name Example boolean b bflag pointer p char *pname handle h void **hwindow null terminated string sz char pszfilename structure s Home sperson function module name stkpush globals g char gnumfiles global to only one file gl int glnumentries 2

Poor Program Style int L (char *n) int i; for (i = 0; *(n + i)!= '\0'; ++i) return i; Good Program Style int strlength (char *pszstr) int count; for (count = 0; '\0'!= *(pszstr + count); ++count) return count; Struct Names Struct definitions will follow the regular variable naming conventions except the first letter of the struct must be capitalized. Further, struct definitions are to exist in a header file (.h file) associated with the.c source file associated with the project. Poor Program Style for Structs typedef struct t int d; int h; int m; int s; t; 3

Good Program Style for Structs typedef struct Time int days; int hours; int minutes; int seconds; Time; Implementation Example The first file is a.h file that contains the definitions of the library (module). The second file is a.c (NOT.cpp) file that contains the actual implementation of the functions included in the library definition. The.c file includes the.h file at the top of the file. Rational Example File name: rational.h Author: Joe Bloggs Date: 8/31/09 Class: CS300 Assignment: Rational Purpose: To define the header file for the rational module #ifndef RATIONAL_H #define RATIONAL_H typedef struct Rational int numerator; int denominator; Rational; extern void rationalprint (Rational); extern void rationalset (Rational *, int, int); extern int rationalisequal (Rational, Rational); extern Rational rationalmultiply (Rational, Rational); #endif 4

File name: rational.c Author: Joe Bloggs Date: 8/31/09 Class: CS300 Assignment: Rational Purpose: This is the implementation file for each of the rational functions associated with the rational module. #include <stdio.h> #include "../header/rational.h" Function: rationalprint Description: Outputs a fraction in the form numerator / denominator to the screen Parameters: srational - a fraction to be printed Returned: None void rationalprint (Rational srational) printf ("%d / %d", srational.numerator, srational.denominator); Function: rationalset Description: Initializes a fraction to the values of the numerator and denominator passed in. Parameters: srational - a fraction numerator - numerator initialization value denominator - denominator initialization value Returned: None void rationalset (Rational *psrational, int numerator, int denominator) psrational->numerator = numerator; psrational->denominator = denominator; 5

Function: rationalisequal Description: Compares two fractions returning a value of true if the numerators and denominators of both fractions are the same. Parameters: srational1 - first fraction used in comparison srational2 - second fraction used in comparison Returned: true if objects are equal; else, false int rationalisequal (Rational srational1, Rational srational2) return ((srational1.numerator == srational2.numerator) && (srational1.denominator == srational2.denominator)); Function: rationalmultiply Description: Multiples the numerators and denominators of two fractions. Parameters: srational1 - first rational number srational2 - second rational number Returned: A fraction that contains the result of the multiplication. Rational rationalmultiply (Rational srational1, Rational srational2) Rational sfraction; rationalset (&sfraction, 0, 0); sfraction.numerator = srational1.numerator * srational2.numerator; sfraction.denominator = srational1.denominator * srational2.denominator; return sfraction; Formatting Indentation Two spaces must be used as the unit of indentation per tab. Every IDE (Integrated Development Environment) such as Visual Studio or Eclipse includes an option for changing the number of spaces in a tab. These can usually be found in the preferences section. 6

Line Length Lines must be no longer than 78 characters. Anything longer than 80 characters is normally not handled well in many terminals and tools. Wrapping Lines If an expression cannot fit on a single line then break it: After a comma Before an operator Make sure that the new line is aligned with the beginning of the expression at the same level on the previous line. Spaces All arithmetic and logical operators must have exactly one space before and after the operator. The only exceptions are: Unary operators The period No spaces before the comma and only one space after the comma Blank Lines Use blank lines to separate distinct pieces of code. For example, one blank line before and after a while loop helps the code reader. The important thing to remember is that blank lines must be used consistently. Braces Any curly braces that you use in your program (e.g. surrounding structs, functions) must appear on their own lines. Any code within the braces must be indented relative to the braces. typedef struct Rational int numerator; int denominator; Rational; 7

Comments Comments should be used to explain the purpose of the code fragment they are grouped with. Comments should state what the code is doing, while the code itself shows how you are doing it. Use comments sparingly and only comment code segments that are not obvious. Giving your variables meaningful names will improve the readability of your code and reduce the need for comments. File Header The main purpose of a file header is to explain the purpose of the module as briefly as possible. You must include the following sections in your module header: File name Your name Date Class Title Assignment Title Purpose File name: main.c Author: Joe Bloggs Date: 8/31/09 Class: CS300 Assignment: Rational Purpose: This program is the driver to test the rational module. #include <stdio.h> #include "../header/rational.h" int main(int argc, char* argv[]) Rational srational1, srational2; rationalset (&srational1, 1, 2); rationalset (&srational2, 2, 4); rationalprint (srational1); printf ("\n"); rationalprint (srational2); printf ("\n"); printf ("%i\n", rationalisequal ( srational1, srational2)); printf ("\n"); rationalprint (rationalmultiply (srational1, srational2)); printf ("\n"); return 0; 8

Declaration Comments Variables must be declared so that comments are not necessary to explain the variable's meaning. You must also group together variables that are related. int seconds; int minutes; int hours; char *pszfirstname; char *pszlastname; Function Header In the same way that a program header is used to describe the purpose of the program, the function header is used to describe the purpose of the function. Each function header must include the following: Function name Description Parameters Returned Notice that each parameter exists on a single line and the line up. Function: rationalset Description: Initializes a fraction to the values of the numerator and denominator passed in. Parameters: srational numerator - a fraction - numerator initialization value denominator - denominator initialization value Returned: None void rationalset (Rational *psrational, int numerator, int denominator) psrational->numerator = numerator; psrational->denominator = denominator; 9

Sidebar and In-line Comments A sidebar comment appears on the same line as the single statement it is describing. The comment must be brief and not exceed that line. Only document statements that are not obvious. The following documentation is not necessary for experienced C programmers but shows what a sidebar comment looks like. value <<= 1; /* multiply value by 2 */ In-line comments appear on their own lines and precede the segment of code they describe. You should use in-line comments to describe complex code that is not limited to a single statement. You must use blank lines to separate the comments from the segments of code they are describing. The comment below would not be placed in an actual program as it is obvious what the code is doing; however, the example illustrates what an in-line comment is to look like. /* If the file exists, open the input file for reading */ if ((pinfile = fopen (FILE_SCORES, "r"))!= NULL)... Although using comments helps in describing your code, you must always make sure that your variables have meaningful names to make the code more understandable. Printing When printing your code you must use a fixed width font. Courier and Courier New are examples of fixed width fonts. You must also make sure that your lines do not wrap nor do they get cut off when printing. All printing is to be done in Portrait and the printing order for the files is as follows: 1) the program file containing main 2) header (.h) / implementation (.c) pairs for each module Note: Each module is to have a separate.h and.c file. The final output you will turn in is to be printed in color since comments, keywords, strings, etc are highlighted for easy reading. 10