-
Notifications
You must be signed in to change notification settings - Fork 10
StyleGuide CSourceFiles
Every C source file has a single corresponding C header file that defines all of the necessary features necessary to use the functionality implemented in the C source file.
Names for C Source and Header files must be prefixed with the name of the associated library or other name space concept with a single character separating the prefix from the rest of the file name. This is to visually separate the name-space of a file in a directory listing or similar.
For example:
Name | |
---|---|
parc_LinkedList.c |
A module in the PARC library |
LongBow_Buffer.c |
A module in the LongBow library |
ccnx_Interest.c |
A module in the CCNx library |
See the Naming Conventions in C Programs document offers a detailed description of the naming conventions for C header and source files.
The general layout of a C source file is:
- Copyright Notice
- Preprocessor Include Directives
- Private Preprocessor Constant Definitions
- Private Preprocessor Macro Definitions
- Static Function Definitions
- Global Function Definitions
The Copyright notice for PARC C source file is:
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* Copyright 2014 Palo Alto Research Center (PARC), a Xerox company.
* All Rights Reserved.
* The content of this file, whole or in part, is subject to licensing terms
.
* If distributing this software, include this License Header Notice in each
* file and provide the accompanying LICENSE file.
*/
The following outlines the types of preprocessor include directives:
Include File Kind | Description |
---|---|
Configuration |
config.h and other configuration files. |
System | Files originating in /usr/include
|
Third party | Third party libraries and support |
LongBow | LongBow runtime include files |
Project | Project include files |
Private | Private include files |
Each grouping of include files should be separated by a blank line.
This is the same as the description for the C Header File.
Less is better: Forward declarations are simply more code to maintain. Defining the function once means you only have to modify the file in one place if you need to change the function’s signature.
static int
_myFunction(void)
{
return 1;
}
As a comparision, a forward declaration may easily get lost if you change the function’s signature.
static int _myFunction(void);
// ...
// possibly many intervening lines of code and comments ;
// ...
static int
_myFunction(void)
{
return 1;
}
Static functions need documentation as much as any globally defined function. Keep in mind that the audience is different. The global function is intended to be used by arbitrary users of the function, while a static function is intended to be used and understood by the maintainer of the module using it internally.
All global functions declared in the corresponding C Header file are defined in the C Source file, and vice versa. The definition of the function always places the function’s storage class and type on one line and the function name on the following line, starting in the first column as in: For example:
size_t
parcBuffer_Limit(const PARCBuffer *buffer)
{
parcBuffer_AssertValid(buffer);
return buffer->limit;
}
Not:
size_t parcBuffer_Limit(const PARCBuffer *buffer)
{
parcBuffer_AssertValid(buffer);
return buffer->limit;
}