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
        #endif
      

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();

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.