C++ Identifier
In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any language. For naming identifiers, each language has its own set of conventions.
In a nutshell, the C++ identifiers reflect the program's basic parts, which are listed below:
- Constants
- Variables
- Functions
- Labels
- Data types that have been defined
In both C and C++, there are several naming conventions that are universal. The following are the details:
- Only letters from the alphabet, numbers, and underscores are permitted.
- The initial letter of the identification name must be alphabetical and cannot begin with a number. We can use letters, numerals, or underscores after the initial character.
- Uppercase and lowercase letters are distinguished in C++. As a result, we may claim that identifiers in C++ are case sensitive.
- A defined keyword can't be used as the name of a variable.
Let see an example: Let's say we have two IDs, 'FirstName' and 'Firstname'. Both IDs will be distinct, with the letter 'N' in uppercase in the first case and lowercase in the second. As a result, it is demonstrated that identifiers are case-sensitive.
Valid Identifiers
The following are some valid identifier:
- Conclusion
- Valid2
- _add
- power
Invalid Identifiers
Invalid IDs can be found in the following examples:
Example:
add-1 // containing special character '-'.
2next // the first letter is a digit.
break // use of a keyword.
NOTE: Identifiers aren't allowed to be used as keywords. Although it is possible that it will not clash with the keywords, it is strongly advised that the keywords not be used as the identification name. To make your code more understandable and manageable, you should always name the identifiers in the same way.
The main distinction between C and C++ is the length restriction on variable names. In ANSI C, only the first 32 characters of a name are considered, however in ANSI C++, there is no restriction on the length of a name. Constants are identifiers that refer to a constant value that does not change during a program's execution. Literal constants are supported in both C and C++, and they can be stored in any memory region. The literal constants are, for example, 324, 11.13, 007, 1X0, and so on.
To grasp the notion of identifiers, consider the following example:
Example:
#include <iostream>
#include<bits/sdtc++.h>
using namespace std;
int main()
{
int x;
int Y;
cout<<"enter the values of 'x' and 'Y'";
cin>>x;
cin>>Y;
cout<<"\nThe values that you have entered are : "<<x<<" , "<<Y;
return 0;
}
OUTPUT:
The values that you have entered are: x, Y
Explanation:
The variables 'x' and 'Y' are declared in the above code. Both letters are identical, however they will function as distinct identities. Because identifiers are case-sensitive, both IDs will be stored in distinct memory locations.
Keywords
The reserved words that have a specific meaning for the compiler are known as keywords. They are set aside for a specific purpose and are not to be used as identifiers. For example, predefined terms are words whose meaning is already known by the compiler, such as 'for', 'break', 'while', 'if', 'otherwise', and so on. Identifiers, on the other hand, are the names given by the programmer to programme components like variables, functions, arrays, objects, and classes. The entire list of C++ keywords may be found in the table below:
auto | else | operator | Template |
break | enum | private | this |
case | extern | protected | throw |
catch | float | public | try |
char | for | register | typedef |
class | friend | return | union |
const | goto | short | unsigned |
continue | if | signed | virtual |
default | inline | sizeof | Void |
delete | Int | static | volatile |
do | long | struct | while |
- Declares that a section of code should be given to the assembler.
- auto is a storage class specifier for defining objects in a block.
- A break statement or a loop is used to end a switch statement or a loop.
- case: In a switch statement, case is used to define a match for the statement's expression.
- catch: Defines what happens if an exception occurs.
- Character objects are defined by the data type char.
- Declare a user-defined type that encompasses data members, actions, and member functions as a class.
- Const: To declare objects whose value will not change throughout the execution of the programme.
- continue:- Returns control to the beginning of the loop.
- default:- Handles expression values that aren't handled by case in a switch statement.
- Memory deallocation operator delete: Memory deallocation operator.
- do: denotes the commencement of a do-while statement, in which the sub-statement is performed until the expression's value is logical-false.
- double: The most basic data type for defining a floating-point value.
- else: A keyword that is only used in if-else statements.
- To specify a user-defined enumeration data type, use the enum keyword.
- extern: An identifier marked as extern is linked to the block from the outside.
- float: The most basic data type for defining a floating-point value.