VOOZH about

URL: https://www.geeksforgeeks.org/cpp/naming-convention-in-c/

⇱ Naming Convention in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Naming Convention in C++

Last Updated : 4 Jun, 2026

Naming conventions are recommended guidelines for choosing names for classes, functions, variables, constants, and files in C++. While they are not enforced by the language, following consistent naming practices improves code readability, maintainability, and collaboration.

  • Class names are typically nouns and often follow PascalCase (e.g., StudentRecord).
  • Function names usually describe actions and commonly use camelCase (e.g., calculateSum).
  • Variable names should be meaningful and descriptive rather than generic names like x, y, or z.
  • Constants are commonly written in UPPER_CASE with underscores (e.g., MAX_SIZE).

Example: Consider a program that calculates the product of two numbers:

int var1 = 10;
int var2 = 20;
int product = var1 * var2;

Using descriptive names such as product makes the code easier to understand than using generic names like x, y, and z.

Common Naming Practices in C++

Consistent naming makes code easier to read, understand, and maintain. Although C++ does not enforce any specific naming style, most projects follow common conventions to keep code organized and readable.

  • Use descriptive names that clearly indicate the purpose of an identifier.
  • Follow a consistent naming style throughout the project.
  • Prefer meaningful names over short or ambiguous ones.
  • Common naming styles include PascalCase, camelCase, snake_case, and UPPER_CASE.

Classes and Class Attributes Names

Class names should represent what an object is and are typically written using PascalCase.

  • The class name should be a noun.
  • Capitalize the first letter of each word when using PascalCase.
  • The first character in the class name must be in upper case.
  • Underscores are generally avoided in class names when following PascalCase conventions.

class PerimeterRectangle
class FingerprintScanner

  • The private attribute name in class should be prepended with the character 'm'.
  • After prepending 'm', the same rules will be followed for the name as that for the class name.
  • Character 'm' also precedes other name modifiers also. For example, 'p' for pointers.

class PerimeterRectangle
{
    public:
    int perimeter;
    private:
    int mLength;
    int mWidth;
}

Functions and Function Argument Names

Usually, every function in C++ performs one or more actions, so the name of the function should clearly hint at what it does. Each method/ function name should begin with a verb.

  • Suffixes are sometimes useful. For example,
  • Count- the current count of the counter.
  • Key- the key value.
  • Prefixes are sometimes useful. For example,
  • get-get value.
  • set- set value.

Function names commonly follow camelCase, although some projects may use PascalCase or snake_case.

int getValue();
int solveEquation();

Parameter names are typically written in camelCase and should clearly describe the value they represent.

int PerimeterRectangle(int lengthRectangle, int widthRectangle)

Variables

Variable names should clearly describe the data they store and follow a consistent naming style to improve code readability and maintainability.

  • The variable name should begin with an alphabet.
  • Digits may be used but only after the alphabet.
  • No special symbols can be used in variable names except for the underscore('_').
  • No keywords can be used for variable names.

int total_cost;
int length;

Prefixing pointer variables with 'p' (e.g., pValue) is an optional naming convention (Hungarian notation) and not required in C++. The placement of '' (e.g., int ptr or int ptr) is a style preference.

int *pName;
int *pAge, address; // Here only pAge is a pointer variable

Using 'r' as a prefix for reference variables is an optional convention and not a standard C++ requirement. References are already indicated by '&', so such prefixes are generally unnecessary in modern C++ code.

Static variables should be prepended with 's'.

static int sCount;

Constant

The global constants should be all capital letters separated with '_'.

const double TWO_PI = 6.28318531; 

File Naming

File names should be simple, descriptive, and easy to identify.

  • No special character is allowed in the file name except for underscore ('_') and dash ('-').
  • C++ source files commonly use extensions such as .cpp, .cc, or .cxx.
  • Do not use filenames that already exist in /user/include. or any predefined header file name.

helloworld.c       // Valid
hello_world.cpp    // Valid
hello-world.cpp   // Valid
hel-lo_world.cpp  // Valid
hello* world.cpp  // Not Valid
iostream.cpp // Not Valid
hello123@world.cpp// Not Valid

Benefits of Following Naming Conventions

Following a consistent naming style provides several advantages:

  • Improves code readability and understanding.
  • Makes the purpose of identifiers easier to recognize.
  • Helps maintain consistency across a project or team.
  • Reduces the chances of naming conflicts.
  • Simplifies debugging and long-term maintenance.
Comment
Article Tags: