Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Rules to Name an Identifier in C++:
3.
Frequently Asked Questions
3.1.
Are identifiers case-sensitive in C++?
3.2.
Can I use special characters like @ or $ in identifiers?
3.3.
What happens if I use a keyword as an identifier?
4.
Conclusion 
Last Updated: Jun 13, 2024
Easy

Identifiers in C++

Author Rahul Singh
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C++ is a popular programming language which is used by developers worldwide. While we write code in C++, we use identifiers to name variables, functions, classes, & other elements. Identifiers play a crucial role in making our code readable & understandable. 

Identifiers in C++

In this article, we will discuss what identifiers are, the rules for naming them, with some examples of valid & invalid identifiers in C++.

Rules to Name an Identifier in C++:

In C++, there are certain rules we need to follow when naming identifiers. These rules ensure that our code is consistent & follows the language standards. These rules are : 

  • Identifiers can only contain letters (a-z, A-Z), digits (0-9), & underscores (_). No other special characters are allowed.
     
  • Identifiers cannot start with a digit. They must begin with a letter or an underscore.
     
  • C++ is case-sensitive, which means uppercase & lowercase letters are considered different. For example, "myVariable" & "myvariable" are two different identifiers.
     
  • Keywords (reserved words) in C++ cannot be used as identifiers. For instance, you cannot use "int" or "for" as an identifier name.
     
  • Identifiers should be descriptive & meaningful. They should clearly indicate the purpose of the variable, function, or class they represent.
     
  • It is good practice to use camelCase or snake_case when naming identifiers. For example: "myVariableName" or "my_variable_name".
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Are identifiers case-sensitive in C++?

Yes, identifiers in C++ are case-sensitive. This means that "myVariable" & "myvariable" are considered different identifiers.

Can I use special characters like @ or $ in identifiers?

No, identifiers in C++ can only contain letters, digits, & underscores. Special characters like @, $, or - are not allowed.

What happens if I use a keyword as an identifier?

Using a keyword as an identifier will result in a compilation error. Keywords are reserved words in C++ & cannot be used as identifiers.

Conclusion 

In this article, we learned about identifiers in C++. We discussed the rules for naming identifiers, such as using only letters, digits, & underscores, starting with a letter or underscore, & avoiding keywords. We also saw examples of valid & invalid identifiers. By following these rules & conventions, we can write clean, readable, & maintainable code in C++.

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass