Identifier In CPP





In C++, an identifier is a name given to various program elements such as variables, functions, classes, etc. Identifiers are used to uniquely identify these elements within the program.

Here's a more formal definition:

An identifier in C++ is a sequence of characters (letters, digits, and underscores) that serves as a name for various program elements, such as variables, functions, classes, etc.



In C++, identifiers follow certain rules:

  • They can consist of letters (both uppercase and lowercase), digits, and underscores.
  • The first character must be a letter or an underscore.
  • Identifiers are case-sensitive.
  • They cannot be keywords (reserved words) of the C++ language.
  • They cannot contain spaces or special characters (except underscores).


Examples of valid identifiers in C++:

myVariable
_count
calculateSum
ClassName
MAX_SIZE
daysOfWeek
my_function  


Examples of invalid identifiers in C++:

2ndValue (starts with a digit)
class (reserved keyword)
my variable (contains space)
my-variable (contains special character)
sum# (contains special character) 


An identifier is a sequence of characters used to indicate one of the following:

  • Variable Name :
    int age;
    float salary;
    
  • Function Name :
    void greetUser()
    {
    
    }
    
    int calculateSum(int a, int b)
    {
    
    }
    
  • Label Name :
    startLoop:
        // loop code
        goto startLoop;
    
  • Constant names (often defined using const):
    const int MAX_SIZE = 100;
    
  • Class Name :
    class Student {
        // class members
    };
    
  • structure Name :
    struct StructureName {
        // Member declarations
    };
    
  • union Name :
    union UnionName {
        // Member declarations
    };
    
  • Object Name :
    Student s1;
    
  • Enum Type Name :
    enum DaysOfWeek {
        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
    };
    
  • Data Member of a class Name :
    class MyClass {
    public:
        int myNumber; // Declaration of a data member
    };
    
  • Class-member Function Name :
    class MyClass {
    public:
        void myFunction(int x, int y); // Declaration of a member function
    };
    
  • typedef Name :
    typedef int Name;
    Name myNumber = 5;
    
  • Macro Name :
    #define SQUARE(x) ((x) * (x))
    
  • Macro parameter Name :
    #define MACRO_NAME(parameter_name) // Macro definition
    
  • Namespace names:
    namespace math {
    // namespace contents
    }
    
  • Template parameter names:
    template 
    class Container {
        // class members
    };