C++ Identifiers

Hi everyone, inside this article we will see about C++ Operators.

In the C++ programming language, an identifier is a name given to a variable, function, class, or other entities. An identifier can only contain letters, digits, and underscores, and must start with a letter or an underscore.

C++ is case sensitive, meaning that “variable” and “Variable” are considered two different identifiers. There are also certain reserved keywords in C++ that cannot be used as identifiers.

Identifiers of C++

There are several types of identifiers in C++:

Variable names: used to store values in memory

Function names: used to define functions and perform actions

Class names: used to define classes and create objects

Object names: instances of classes

Enumeration constants: used in enumerated data types

Label names: used to label statements in a program

Namespaces: used to organize code and prevent naming conflicts.

Each identifier must adhere to the rules for naming identifiers in C++, such as starting with a letter or underscore and consisting of only letters, digits, and underscores.

Naming Convention of Identifier in C++

In C++, there are several naming conventions for identifiers that are widely followed to make the code more readable and maintainable:

CamelCase: The first letter of each word in a multi-word identifier is capitalized, with no underscores. This is commonly used for variables and function names. For example: “myVariableName”.

PascalCase: The first letter of each word in a multi-word identifier is capitalized, with no underscores. This is commonly used for class names. For example: “MyClassName”.

snake_case: Each word in a multi-word identifier is separated by an underscore and all letters are lowercase. This is commonly used for constants and macro names. For example: “my_constant_name”.

ALL_CAPS: All letters in a multi-word identifier are in uppercase, with underscores separating each word. This is commonly used for macros. For example: “MY_MACRO_NAME”.

It’s important to note that while these naming conventions are widely followed, they are not enforceable by the C++ compiler and are merely conventions followed by the C++ community to improve code readability and maintainability.

Here are some examples of valid C++ identifiers:

count

student_name

_private

totalSum

X_AXIS

PI

avg_age

EmployeeRecord

MAX_SIZE

salary_bonus

Note that C++ keywords such as “int” or “while” cannot be used as identifiers.

Here are some examples of invalid C++ identifiers:

123number (starts with a digit)

float (a reserved keyword in C++)

for-loop (contains a hyphen)

new-word (contains a hyphen)

char# (contains a special character other than underscore)

class (a reserved keyword in C++)

while_loop (contains a space)

public (a reserved keyword in C++)

hello world (contains a space)

break% (contains a special character other than underscore).

Note that in C++, identifiers must start with a letter or an underscore and can only contain letters, digits, and underscores.

We hope this article helped you to understand about C++ Identifiers in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.