What are the naming conventions commonly use in C? I know there are at least two:
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)
This question is related to
c
naming-conventions
I think those can help for beginner: Naming convention of variables in c
The most important thing here is consistency. That said, I follow the GTK+ coding convention, which can be summarized as follows:
MAX_BUFFER_SIZE
, TRACKING_ID_PREFIX
.GtkWidget
, TrackingOrder
.gtk_widget_show()
, tracking_order_process()
.GtkWidget *foo
, TrackingOrder *bar
._refrobnicate_data_tables()
, _destroy_cache()
."Struct pointers" aren't entities that need a naming convention clause to cover them. They're just struct WhatEver *
. DON'T hide the fact that there is a pointer involved with a clever and "obvious" typedef. It serves no purpose, is longer to type, and destroys the balance between declaration and access.
Coding in C#, java, C, C++ and objective C at the same time, I've adopted a very simple and clear naming convention to simplify my life.
First of all, it relies on the power of modern IDEs (such as eclipse, Xcode...), with the possibility to get fast information by hovering or ctrl click... Accepting that, I suppressed the use of any prefix, suffix and other markers that are simply given by the IDE.
Then, the convention:
And that's it.
It gives
class MyClass {
enum TheEnumeration {
FIRST_ELEMENT,
SECOND_ELEMENT,
}
int class_variable;
int MyMethod(int first_param, int second_parameter) {
int local_variable;
TheEnumeration local_enum;
for(int myindex=0, myindex<class_variable, myindex++) {
localEnum = FIRST_ELEMENT;
}
}
}
I would recommend against mixing camel case and underscore separation (like you proposed for struct members). This is confusing. You'd think, hey I have get_length
so I should probably have make_subset
and then you find out it's actually makeSubset
. Use the principle of least astonishment, and be consistent.
I do find CamelCase useful to type names, like structs, typedefs and enums. That's about all, though. For all the rest (function names, struct member names, etc.) I use underscore_separation.
You know, I like to keep it simple, but clear... So here's what I use, in C:
i,n,c
,etc... (Only one letter. If one letter isn't
clear, then make it a Local Variable)lowerCamelCase
g_lowerCamelCase
ALL_CAPS
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_
.ModuleCamelCase
(Module = full module name, or a 2-3 letter abbreviation, but still in CamelCase
.)lowerCamelCase
ModuleCamelCase
ALL_CAPS
ModuleCamelCase
CamelCase
CamelCase
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 TheName TheName;
struct TheName{
int var;
TheName *p_link;
};
Well firstly C doesn't have public/private/virtual functions. That's C++ and it has different conventions. In C typically you have:
C++ is more complex. I've seen a real mix here. Camel case for class names or lowercase+underscores (camel case is more common in my experience). Structs are used rarely (and typically because a library requires them, otherwise you'd use classes).
There could be many, mainly IDEs dictate some trends and C++ conventions are also pushing. For C commonly:
Hungarian notation for globals are fine but not for types. And even for trivial names, please use at least two characters.
I'm confused by one thing: You're planning to create a new naming convention for a new project. Generally you should have a naming convention that is company- or team-wide. If you already have projects that have any form of naming convention, you should not change the convention for a new project. If the convention above is just codification of your existing practices, then you are golden. The more it differs from existing de facto standards the harder it will be to gain mindshare in the new standard.
About the only suggestion I would add is I've taken a liking to _t at the end of types in the style of uint32_t and size_t. It's very C-ish to me although some might complain it's just "reverse" Hungarian.
Here's an (apparently) uncommon one, which I've found useful: module name in CamelCase, then an underscore, then function or file-scope name in CamelCase. So for example:
Bluetooth_Init()
CommsHub_Update()
Serial_TxBuffer[]
You should also think about the order of the words to make the auto name completion easier.
A good practice: library name + module name + action + subject
If a part is not relevant just skip it, but at least a module name and an action always should be presented.
Examples:
os_task_set_prio
, list_get_size
, avg_get
OS_TASK_PRIO_MAX
Source: Stackoverflow.com