What are the most common naming conventions in C?

Viewed 214836

What are the naming conventions commonly use in C? I know there are at least two:

  1. GNU / linux / K&R with lower_case_functions
  2. ? name ? with UpperCaseFoo functions

I am talking about C only here. Most of our projects are small embedded systems in which we use C.

Here is the one I am planning on using for my next project:


C Naming Convention

Struct              TitleCase
Struct Members      lower_case or lowerCase

Enum                ETitleCase
Enum Members        ALL_CAPS or lowerCase

Public functions    pfx_TitleCase (pfx = two or three letter module prefix)
Private functions   TitleCase
Trivial variables   i,x,n,f etc...
Local variables     lower_case or lowerCase
Global variables    g_lowerCase or g_lower_case (searchable by g_ prefix)
13 Answers

You know, I like to keep it simple, but clear... So here's what I use, in C:

  • Trivial Variables: i,n,c,etc... (Only one letter. If one letter isn't clear, then make it a Local Variable)
  • Local Variables: camelCase
  • Global Variables: g_camelCase
  • Const Variables: ALL_CAPS
  • Pointer Variables: add a p_ to the prefix. For global variables it would be gp_var, for local variables p_var, for const variables p_VAR. If far pointers are used then use an fp_ instead of p_.
  • Structs: ModulePascalCase (Module = full module name, or a 2-3 letter abbreviation, but still in PascalCase.)
  • Struct Member Variables: camelCase
  • Enums: ModulePascalCase
  • Enum Values: ALL_CAPS
  • Public Functions: ModulePascalCase
  • Private Functions: PascalCase
  • Macros: PascalCase

I typedef my structs, but use the same name for both the tag and the typedef. The tag is not meant to be commonly used. Instead it's preferrable to use the typedef. I also forward declare the typedef in the public module header for encapsulation and so that I can use the typedef'd name in the definition.

Full struct Example:

typdef struct UtilsExampleStruct UtilsExampleStruct;
struct UtilsExampleStruct{
    int var;
    UtilsExampleStruct *p_link;
};

When I will program in C I will use this convention:

definitions prefix examples
variables - local (trivial) / i, n, a, b...
ii, nn, aa, bb...
iii, nnn, aaa, bbb...
variable - local (descriptive) / variable, conection...
variable - global g_ g_variable, g_connection...
variable - constant c_ c_variable, c_connection...
pointer p_ p_variable , p_connection...
array a_ a_variable, a_connection...
struct s_ s_variable, s_connection...
union u_ u_variable, u_connection...
enum e_ e_variables, e_connection...
struct (member)
union (member)
enum (member)
m_
m_
m_
m_variable, m_connection...
m_variable, m_connection...
m_variable, m_connection...
struct (bit field) b_ b_variable, b_connection...
function f_ f_variable, f_connection...
macro o_ o_variable, o_connection...

Note:

Each definition except variables has a single letter prefix, so that they can be combined and then not being miss understood.

Note:

Because I ran out of letters and I insisted to have only lower case letter prefixes, prefix "o_" used for macro does not match the first letter of the definition (macro) but it matches the last letter (macro).

If you have any suggestions I am open minded.

There could be many, mainly IDEs dictate some trends and C++ conventions are also pushing. For C commonly:

  • UNDERSCORED_UPPER_CASE (macro definitions, constants, enum members)
  • underscored_lower_case (variables, functions)
  • CamelCase (custom types: structs, enums, unions)
  • uncappedCamelCase (oppa Java style)
  • UnderScored_CamelCase (variables, functions under kind of namespaces)

Hungarian notation for globals are fine but not for types. And even for trivial names, please use at least two characters.

I recommend you to read this document.

Abstract
This document is an updated version of the Indian Hill C Style and Coding Standards paper, with modifications by the last three authors. It describes a recommended coding standard for C programs. The scope is coding style, not functional organization.

https://www.doc.ic.ac.uk/lab/cplus/cstyle.html#N1026F

I think those can help for beginner: Naming convention of variables in c

  1. You have to use Alphabetic Character (a-z, A-Z), Digit (0-9) and Under Score (_). It’s not allow to use any special Character like: %, $, #, @ etc. So, you can use user_name as variable but cannot use user&name.
  2. Can not use white space between words. So, you can use user_name or username or username as variable but cannot use user name.
  3. Can not start naming with digit. So, you can use user1 or user2 as variable but cannot use 1user.
  4. It is case sensitive language. Uppercase and lowercase are significant. If you use a variable like username then you cannot use USERNAME or Username for father use.
  5. You can not use any keyword (char, int, if, for, while etc) for variable declaration.
  6. ANSI standard recognizes a length of 31 characters for a variable name
Related