In this tutorial, we are going to learn about identifiers in C++. We will see some examples so that you can understand the concept of identifiers, and later on, you would have no problem in naming classes, variables, functions, etc.
Identifiers in C++
What are Identifiers in C++?
Identifiers are just the names that we give to variables, functions, classes, objects, etc. At times, in our programs, we would create some functions, some classes, some variables, objects, etc. and in such situations, we need to know the correct rules for naming these things.
Let’s discuss some rules for identifiers, and later, we will also see some valid, and invalid examples, for identifiers.
- An identifier can start with letters (a-z) or (A-Z).
- An identifier can contain digits(0-9), but it cannot have a digit at starting.
- An identifier can contain an underscore( _ ) (at the start too).
- An identifier cannot have symbols like %, @, $, etc.
- C++ is a case-sensitive programming language. So, the names are too case sensitive, like here, the variables named gyanipandit, and Gyanipandit is different.
An identifier cannot have the name of a keyword(as you know keywords have special meanings and they cannot be used anywhere else)
Here are some valid examples of identifiers-
- gyanipandit
- gyani_pandit
- language
- sum1
- variable11
- _something
- a
- i
- j
Some examples of invalid identifier names-
- 1variable
- var$
- 1name
or something like that.
I want to tell you something else about naming identifiers. This is not a rule, but it can be considered as a good practice. Whenever you name your variables, functions, classes, objects, etc, you should name them in such a way that the name makes sense. For example, if there is some variable that is supposed to hold the count of something, then instead of naming that variable something like a, b, var1, etc, you should name it to count, or something like that which makes sense. This can improve the understanding of the code of an individual.
Summary
In this tutorial, we have discussed identifiers, in which, we have seen about some rules for naming things like variables, functions, classes, objects, etc. Remember that you DO NOT need to memorize the rules, since once you begin using these rules, you would gradually get familiar with them, and comfortably give good names to the functions, variables, classes, objects, etc.
If you find this tutorial to be interesting, you can explore our other tutorials related to C programming language to learn more about C programming language. You can also explore our other courses, related to Python, Machine learning, and Data Science, through which, you can upskill yourself.
FAQs related to identifiers in C++
Q: What are identifiers in C++?
Ans: We can understand identifiers as names given to some things, like variables, classes, functions, objects, etc.
Q: Is 1number a valid identifier?
Ans: No, since it violates the rule, that the identifier should be starting with an alphabet or an underscore.
Q: What are some rules for naming identifiers?
Ans: Here are some rules for naming identifiers
1. An identifier can start with letters (a-z) or (A-Z).
2. An identifier can contain digits(0-9), but it cannot have a digit at starting.
3. An identifier can contain an underscore( _ ) (at the start too).
4. An identifier cannot have symbols like %, @, $, etc.
5. C++ is a case-sensitive programming language. So, the names are too case sensitive, like here, the variables named gyanipandit, and Gyanipandit is different.
6. An identifier cannot have the name of a keyword(as you know keywords have special meanings and they cannot be used anywhere else)