✍️ C++ Basic Syntax & Language Elements
Estimated reading: 3 minutes 195 views

C++ Identifiers – Naming Variables, Functions, and More


Introduction – What Are Identifiers in C++?

Identifiers are the names used to identify variables, functions, arrays, classes, objects, and other user-defined entities in C++. They are one of the most fundamental language elements, allowing developers to assign readable, meaningful names to different program elements.

In this guide, you’ll learn:

  • What identifiers are and where they are used
  • Rules for writing valid identifiers
  • Differences between identifiers and keywords
  • Common identifier naming conventions and best practices

What Is an Identifier?

An identifier is a user-defined name used to uniquely label variables, functions, classes, arrays, and objects.

int age = 25;
float calculateArea(float radius);
  • age, calculateArea, and radius are identifiers
  • int and float are keywords

Rules for Writing Identifiers in C++

C++ has strict syntax rules for creating identifiers:

Rule Example
Must begin with a letter (A–Z, a–z) or underscore __count, total, Name
Cannot begin with a digit2sum
Cannot use C++ keywordsint, return, class
Can contain letters, digits, and underscoresuser_1, balance2025
Case-sensitivevalue, Value, VALUE are different identifiers

Examples – Valid & Invalid Identifiers

//  Valid identifiers
int age;
float _temperature;
string userName123;

//  Invalid identifiers
int 123number;       // Cannot start with a digit
float float;         // float is a keyword
string first-name;   // Hyphens not allowed

Identifiers vs Keywords

AspectIdentifier Keyword
Defined by user? Yes No (reserved by compiler)
Case-sensitive? Yes Yes
UsageNames for variables, functionsLanguage syntax building blocks
Examplesscore, myFunc, Temp_1int, return, class

Identifier Use Cases

ElementExample
Variable nameint salary = 50000;
Function namevoid printMessage() {}
Class nameclass Student { ... };
Object nameStudent s1;
Constant nameconst float PI = 3.14;

Naming Conventions (Best Practices)

Follow consistent naming for clarity and readability:

Convention Usage Example
CamelCasecalculateSalary, totalMarks
snake_casestudent_name, max_value
ALL_CAPS (Constants)PI, MAX_LIMIT
PrefixingisActive, getName, setAge

Summary – Recap & Next Steps

Key Takeaways:

  • Identifiers are names given to program elements like variables, functions, and classes
  • Must follow naming rules: no starting digits, no keywords, no special characters
  • Case-sensitive: value and Value are different
  • Follow naming conventions for clarity and maintainability

Real-World Relevance:
Well-named identifiers make your code more readable, debuggable, and collaborativeβ€”especially in large projects or shared codebases.


FAQs – C++ Identifiers

Can I use a keyword as an identifier?
No. Keywords are reserved and cannot be reused.

Are identifiers case-sensitive?
Yes. Sum, sum, and SUM are treated as different.

Can I start an identifier with an underscore?
Yes, but in practice, avoid names starting with _ unless needed, as some are reserved for the compiler or standard libraries.

Can identifiers contain spaces or hyphens?
No. Only letters, digits, and underscores are allowed.

Are function names also identifiers?
Absolutely. Functions, variables, arrays, classesβ€”all use identifiers.


Share Now :
Share

C++ Identifiers

Or Copy Link

CONTENTS
Scroll to Top