Variables & Function Parameters

Other C language standards' pages:

Naming Standards
Abbreviations
C Language Standards

Camel Case

Variable names and function parameter names should be in CamelCase.

Prefix

Variables are never defined inside header files so they never need prefixes.

Pointers

Pointers are used differently than non-pointer variables. A range of new operators become available when you use pointers, and mathematical operations take on a whole new meaning. That's why it's important to distinguish pointer variables from non-pointer variables.

Pointer variable names must end in "Ptr".

int index = 0;
int* indexPtr = &index;

There's an exception when working with opaque types, because pointers are often used to implement opaque types in C. A header file might contain something like this:

typedef struct foo_Sandwich* foo_SandwichRef_t;

This defines a type that can be used to refer to a "Sandwich" object. Note struct foo_Sandwich is never defined outside of the "foo" module (and may not even be defined inside of the foo module). To all outsiders, this reference looks like a pointer to an incomplete type, and can't be dereferenced. Inside the "foo" module, struct foo_Sandwich could be defined or this pointer type could even be used to hold an integer, a char, or anything else that's the same size or smaller than a pointer.

A "file reference" could be implemented on some systems as an integer file descriptor. If the reference is an opaque type that's never used as a pointer outside of the module that implements it, or might not be a pointer even inside the module that implements it, then variables of opaque pointer types don't need a "Ptr" suffix.

Static Variables

Variables that exist on the stack are function parameters and automatic variables (non-static variables declared inside of functions). When these variables go out of scope, they disappear; their memory is released so it can be used for other purposes. To make it obvious that these variables are stack variables and not static variables, they must start with a lower-case letter.

Because the scope of these variables is limited to a specific function or compound block within a function, they don't need a prefix to avoid naming conflicts:

static void Function(int numericParameter)
{
char* stringAutoVariable;
 
 
...
}
Note
Only stack variable names and struct/union member names can (and must) start with lower-case letters.

Static variables aren't stored in either the stack or the heap. They are statically allocated in the data segment of the process's address space. They have scope limited to either a compound statement (if defined inside a function), or a file (if defined outside of all functions). Because these variables are never visible outside a file, they don't need any special prefix to prevent naming conflicts. But they're stored differently than variables on the stack (and don't disappear when they go out of scope), so they start with an upper-case letter to distinguish them from stack variables:

static int CallCounter = 0; // This is a file-scope variable.
 
static void Function(int numericParameter)
{
static bool WasCalledBefore = FALSE;
char* stringAutoVariable;
 
if (!WasCalledBefore)
{
...
WasCalledBefore = TRUE;
}
 
CallCounter++;
 
...
}

Abbreviations

To keep identifiers a manageable length, developers often abbreviate. If these abbreviations aren't carefully chosen, they can create confusion.

See Abbreviations. Other abbreviations should not be used.