Notes – Identifier & Keywords in C++

In C++, identifiers and keywords are basic elements of any program.

  • Identifiers are the names used to identify variables, functions, classes, arrays, etc.
  • Keywords are reserved words in the language that have special meaning and cannot be used as identifiers.

What Is an Identifier?

An identifier is the name given by the programmer to a variable, function, class, or object.

Examples:

int age;
float totalMarks;
void printData() {}

In the above code:

  • age, totalMarks, and printData are identifiers

Rules for Naming Identifiers


RuleDescription
Must begin with a letter (Aโ€“Z, aโ€“z) or underscore _
Can include letters, digits (0โ€“9), and underscores
Cannot start with a digit
Cannot use C++ keywords as identifiers
Case-sensitive โ€” Score and score are different
Avoid special characters like @, $, % (not allowed)

Valid vs Invalid Identifiers


Valid IdentifiersInvalid IdentifiersReason
totalMarks2ndPlaceCannot start with a digit
employee_namefloatfloat is a keyword
_value123student-scoreHyphen not allowed

Tips for Writing Good Identifiers

  • Use meaningful names (totalAmount instead of ta)
  • Use consistent naming styles like camelCase or snake_case
  • Avoid single-letter names (unless in loops like i, j)

What Are Keywords?

Keywords are predefined and reserved words in C++.

They have special meanings and cannot be used as variable or function names.


Common C++ Keywords

int       float     return     class     public  
private if else while for
break continue void this new
delete try catch throw const

C++ has over 60 keywords. You canโ€™t use any of them as identifiers.