Skip to content

Instantly share code, notes, and snippets.

@davidzchen
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidzchen/9188331 to your computer and use it in GitHub Desktop.
Save davidzchen/9188331 to your computer and use it in GitHub Desktop.
Sample C code using the Python C coding style
/*
* Sample file using the Python C coding style
*
* http://legacy.python.org/dev/peps/pep-0007/
*
* General rules:
* - Use 4 spaces for indentation.
* - Each line must be at most 79 characters long.
* - Use C-style comments.
* - File names should be lower_case.c
*
* Note: There are a few rules I am not following here for the sake of
* relevancy. For example, the style says to use the Py prefix for public
* functions.
*/
#include <stdlib.h>
#include <stdbool.h>
/* For macros, use ALL_CAPS separated by underscore: */
#define FLAG_FOO 0x0
/*
* If a macro constant's replacement is not just a literal, enclose it in
* parentheses:
*/
#define FLAG_BAZ (0x1 << 3)
/*
* Macros should have the PascalCase prefix of the current module (each file
* is a module) followed by a underscore an an ALL_CAPS name:
*/
#define Sample_ADD_ONE(x) (x + 1)
/*
* It appears that the Linux kernel codebase tends to use #defines for
* constants. Global constants are treated similar to global variables.
*/
const int state_foo = 0;
/* Enum values can either look like macros: */
enum mode {
MODE_FOO,
MODE_BAR,
MODE_BAZ,
MODE_QUX
};
/*
* Names of members of structs are lower_case and separated by underscores.
* Avoid typedef-ing types unless the type is intended to be used as an opaque
* type.
*/
typedef struct sample {
int first_field;
int second_field;
Mode mode;
State state;
struct sample *next;
} Sample;
/*
* Function names are PascalCase with underscores separating the prefix
* designating the module (each file is a module) with the rest of the
* function name.
*/
int
Sample_Equal(Sample *self, Sample *other)
{
/* Blank lines after local variables. */
int ret = 0;
/* Local variables are lower_case and separated by underscores. */
if (self == NULL && other == NULL) {
return 1;
}
if (self == NULL || other == NULL) {
return 0;
}
/*
* For statements that span multiple lines, use spaces for alignment
* only.
*/
if (self->first_field == other->first_field &&
self->second_field == other->second_field &&
self->state == other->state &&
self->mode == other->mode &&
self->next == other->next) {
return 1;
}
return 0;
}
/*
* For function declarations that span multiple lines, align them with the
* first argument.
*/
Sample *
Sample_New(int first_field,
int second_field,
Mode mode,
State state,
Sample *next)
{
/* No space after cast */
Sample *sample = (Sample *)malloc(sizeof(*sample));
if (sample == NULL) {
return NULL
}
memset(sample, 0, sizeof(sample));
sample->first_field = first_field;
sample->second_field = second_field;
sample->mode = mode;
sample->state = state;
sample->next = next;
return sample;
}
Sample *
Sample_Clone(Sample *sample)
{
if (sample == NULL) {
return NULL;
}
/*
* For function calls that span multiple lines, align them with the first
* argument.
*/
return Sample_New(sample->first_field,
sample->second_field,
sample->mode,
sample->state,
sample->next);
}
/*
* For function declarations (and function calls) where you cannot fit
* the parameters with the first after the opening parentheses, then align
* the parameters indented four spaces on the next line:
*/
static void
Sample_DoSomethingWithALongName(
Sample *sample,
int parameter_with_a_long_name,
int another_parameter,
int another_parameter)
{
if (sample == NULL) {
return;
}
int local_variable;
/*
* else and else if appear on the next line rather than the previous
* closing brace.
*/
if (parameter_with_a_long_name == MODE_FOO) {
local_variable = 1;
}
else {
local_variable = 0;
}
sample->first_parameter += another_parameter;
sample->second_parameter |= local_variable;
}
@ZyX-I
Copy link

ZyX-I commented Feb 24, 2014

+1 . Though Linux Kernel is also good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment