What are the most common naming conventions in C?

前端 未结 11 2346
走了就别回头了
走了就别回头了 2020-12-02 04:13

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 UpperC
相关标签:
11条回答
  • 2020-12-02 04:20

    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.

    0 讨论(0)
  • 2020-12-02 04:21

    The most important thing here is consistency. That said, I follow the GTK+ coding convention, which can be summarized as follows:

    1. All macros and constants in caps: MAX_BUFFER_SIZE, TRACKING_ID_PREFIX.
    2. Struct names and typedef's in camelcase: GtkWidget, TrackingOrder.
    3. Functions that operate on structs: classic C style: gtk_widget_show(), tracking_order_process().
    4. Pointers: nothing fancy here: GtkWidget *foo, TrackingOrder *bar.
    5. Global variables: just don't use global variables. They are evil.
    6. Functions that are there, but shouldn't be called directly, or have obscure uses, or whatever: one or more underscores at the beginning: _refrobnicate_data_tables(), _destroy_cache().
    0 讨论(0)
  • 2020-12-02 04:21

    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[]
    
    0 讨论(0)
  • 2020-12-02 04:25

    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
    0 讨论(0)
  • 2020-12-02 04:27

    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: lowerCamelCase
    • Global Variables: g_lowerCamelCase
    • 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: ModuleCamelCase (Module = full module name, or a 2-3 letter abbreviation, but still in CamelCase.)
    • Struct Member Variables: lowerCamelCase
    • Enums: ModuleCamelCase
    • Enum Values: ALL_CAPS
    • Public Functions: ModuleCamelCase
    • Private Functions: CamelCase
    • Macros: 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;
    };
    
    0 讨论(0)
  • 2020-12-02 04:28

    "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.

    0 讨论(0)
提交回复
热议问题