C Naming Convention

C programming language has a set of naming conventions that help maintain clarity and consistency in code. Here's a breakdown of these conventions for various identifiers, constants, functions, files, etc., with examples:

1. Identifiers (Variables and Functions)

  • Camel Case: Used for local variables and function parameters.

    • Example: int localVariable;
  • Pascal Case: Sometimes used for functions.

    • Example: void CalculateInterest();
  • Underscore Separation: Commonly used for global variables and functions.

    • Example: int global_variable;, void perform_calculation();

2. Constants

  • Upper Case with Underscores: Constants are usually written in upper case with underscores separating words.

    • Example: #define MAX_SIZE 100

3. Macros

  • Upper Case with Underscores: Similar to constants, macros are also in upper case.

    • Example: #define CALCULATE_AREA(x) (3.14 * (x) * (x))

4. File Names

  • Lower Case with Underscores: File names are typically in lower case with underscores to separate words.

    • Example: file_operations.c, data_processing.h

5. Structs and Enums

  • Typedef Structs: Often in Camel or Pascal Case.

    • Example: typedef struct Car { ... } Car;
  • Enums: Enum types are often in Camel or Pascal Case, while enum values are in upper case.

    • Example:

        typedef enum { RED, GREEN, BLUE } Color;

6. Pointers

  • Asterisk Close to Variable Name: The asterisk is kept close to the variable name, not the type.

    • Example: int* pointerVariable;

7. Global Variables

  • Prefix with g_ or similar: Sometimes, global variables are prefixed to distinguish them.

    • Example: int g_globalVariable;

8. Header Guards

  • Upper Case with Underscores: Header guards are in upper case.

    • Example:

        #ifndef MY_HEADER_H
        #define MY_HEADER_H
        // header content

These conventions are not strict rules but guidelines that are widely adopted in the C programming community to improve code readability and maintenance.

Camel case and Pascal case are both naming conventions used in programming for identifiers such as variable names, function names, class names, etc. They differ in how they capitalize the first letter of each word in the identifier.

Camel Case (camelCase)

  • In camel case, the first letter of the identifier is lowercase, and each subsequent word starts with an uppercase letter.

  • Usage: Commonly used for variable names and function names in many programming languages.

  • Example:

    • int currentBalance;

    • void calculateInterest();

Pascal Case (PascalCase)

  • In Pascal case, each word starts with an uppercase letter, including the first word.

  • Usage: Often used for class names, constructor names, and sometimes for function names, especially in languages like C#, Java, and others.

  • Example:

    • class BankAccount { ... }

    • void CalculateInterest();