Barr Group FacebookBarr Group TwitterBarr Group LinkedInBarr Group Vimeo

Rules

The following C coding rules relate to the use of individual spaces:

Rule 3.1.a.) Each of the keywords if, else, while, for, switch, and return shall always be followed by one space.

Rule 3.1.b.) Each of the assignment operators =, +=, -=, *=, /=, %=, &=, |=, ^=, ~=, and != shall always be preceded and followed by one space.

Rule 3.1.c.) Each of the binary operators +, -, *, /, %, <, <=, >, >=, ==, !=, <<, >>, &, |, ^, &&, and || shall always be preceded and followed by one space.

Rule 3.1.d.) Each of the unary operators +, -, ++, --, !, and ~, shall always be written without a space on the operand side and with one space on the other side.

Rule 3.1.e.) The pointer operators * and & shall be written with white space on each side within declarations but otherwise without a space on the operand side.

Rule 3.1.f.) The ? and : characters that comprise the ternary operator shall each always be preceded and followed by one space.

Rule 3.1.g.) The structure pointer and structure member operators (-> and ., respectively) shall always be without surrounding spaces.

Rule 3.1.h.) The left and right brackets of the array subscript operator ([ and ]) shall always be without surrounding spaces.

Rule 3.1.i.) Expressions within parentheses shall always have no spaces adjacent to the left and right parenthesis characters.

Rule 3.1.j.) The left and right parentheses of the function call operator shall always be without surrounding spaces, except that the function declaration shall feature one space between the function name and the left parenthesis to allow that one particular mention of the function name to be easily located.

Rule 3.1.k.) Each comma separating function parameters shall always be followed by one space.

Rule 3.1.l.) Each semicolon separating the elements of a for statement shall always be followed by one space.

Rule 3.1.m.) Each semicolon shall follow the statement it terminates without a preceding space.

Reasoning

The placement of white space is as important as the placement of the text of a program. Good use of white space reduces eyestrain and increases the ability of the author and reviewers of the code to spot potential bugs.

Exceptions

None.

Enforcement

These rules shall be enforced by an automated tool such as a code beautifier.

Comments:

"without a space on the operand side and with one space on the other side" leads to ` ++i;` (note the leading space) or `i-- ;` (note the lagging space which violates 3.1.m) rather than `++i;` or `i--;` respectively.

You're absolutely right, of course, in the legalistic sense of the rule.  We've never noticed this subtlety before, probably because we've just applied common sense and omitted the spaces in those situations.  When we make an updated standard, we'll look at how to reword the rule to include this exception.

What’s happening and how it’s done. Get in the know.

Sign up for our newsletter today!

Receive free how-to articles, industry news, and the latest info on Barr Group webinars and training courses via email. 

To prevent automated spam submissions leave this field empty.