0% found this document useful (0 votes)
48 views

C Coding Style

The document provides coding standards and conventions for C style programs including formatting guidelines for indentation, comments, statements, naming conventions, and general programming practices. The standards are intended to facilitate code sharing and tool building by promoting consistency across C programs.

Uploaded by

dhigvijay mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

C Coding Style

The document provides coding standards and conventions for C style programs including formatting guidelines for indentation, comments, statements, naming conventions, and general programming practices. The standards are intended to facilitate code sharing and tool building by promoting consistency across C programs.

Uploaded by

dhigvijay mohan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C Style and Coding

Standards
• The purpose of having these standards is to facilitate sharing of each
other’s code, as well as to enable construction of tools (e.g., editors,
formatters). Through the use of these tools, programmers will be
helped in the development of their programs.

• More important than the particular coding style used is consistency of


coding style. This is particularly important when modifying an
existing program.

• The modifications should be coded in the same style as the program


being modified, not in the programmer’s personal style.
File Naming Conventions
• c source file names end with a suffix of .c
• assembler source file names end with a suffix of .s2
• relocatable object file names end with a suffix of .o
• include header file names end with a suffix of .h
• archived library files end with a suffix of .a
• c-shell source file names end with a suffix of .csh
• bourne shell source file names end with a suffix of .sh
• yacc source file names end with a suffix of .y
• lex source file names end with a suffix of .l
Program Organization
• A module is a group of procedures and data that together implement some
abstraction, for example, a memory management, symbol table, etc.
Procedures and data accessed by other modules are called public.
• Procedures and data known only inside the module are called private. Modules
are defined using two files: an interface file and an implementation file. The
interface file contains the public declaration of the module. The
implementation file contains the private definitions and the code for the
procedures in the module.
• In C, the role of an interface file is provided by a header file(.h file). The code
for the procedures and data definition is provided by the implementation file
(.c file). Private definitions in the implementation file are declared to be static.
Header Files Implementation Files

SCCS Identification: SCCS Identification:

#ifndef: Block Comment:

Block Comment: #includes:

#includes: typedefs & #defines:

Typedefs & #defines: Structure Definition:

Structure Declarations: Global Variable:

Function Declarations: Procedure Declarations:

External Variable Declarations: Definitions:

External Variable Declarations:


• Indentation
1. Each level should be indented by a tab (8 column tab).
2. Insert tabs in variable declarations to align the variables.
3. Occasionally, an expression will not fit in the available space in a
line, for example, a procedure call with many arguments. The
expression should be broken after the last comma in the case of a
function call
• Comments in C Programs
Block Comments - Block comments are used to describe the contents of
files, the functions of procedures, and data structures and algorithms.
Single-Line Comments - Single-Line comments are useful to describe
non-obvious loop conditions and return values of functions.
Trailing Comments - Trailing comments are most useful for
documenting declarations. Trailing comments appear on the same line
as the code they describe.
• Statements
Each line should contain at most one statement.
Do not nest the ternary conditional operator (?:).
• Compound Statements
Compound statements are statements that contain lists of statements
enclosed in {} braces.
White Space
• Blank Lines - Blank lines improve readability by setting off sections
of the code that are logically related.
• Blank Spaces - Blanks should not be used between a procedure name
(or macro call) and its opening parenthesis.

Naming Conventions
• Naming conventions make programs more understandable by making them easier
to read. They can also give information about the function of the identifier, e.g.,
constant, named type, that can be helpful in understanding the code.
• Variable and function names should be short yet meaningful.
Programming Practice
• Numerical constants should not be coded directly.
• The #define feature of the C preprocessor should be used to assign a
meaningful name.
• The enum data type is the preferred way to handle situations where a
variable takes on only a discrete set of values
• Use of goto is discouraged.
• Do not bury a variable initialization in the middle of a long declaration.
• Do not use multiple assignments, because it is hard to read,
• Don’t change C syntax via macro substitution,

You might also like