Identifiers In C
Identifiers In C
In any programming language, identifiers are the names given to a variable, function, or any entity. We have some rules to take the identifier names in the programming languages.
Let us discuss the rules in detail.
- Identifier names should not start with an integer value.
- There should be no spaces in the identifier.
- We cannot use keywords as identifiers because the keywords are already defined in the respective programming language.
- Identifiers that are declared in the code should be unique.
- In an identifier, we cannot use special characters; the only special characters we can use is _.
- The identifiers are case sensitive, both a and A are different identifiers.
Let us discuss every rule in detail:
1) Identifier should not start with an integer value: let us understand this rule by taking an example:
Ex:
int a;
int 1;
In the above example, we have declared one variable name as 'a', which is allowed, and another variable name as 1, which is not allowed; using the identifier which starts with an integer value will give an error.
Let us see the implementation of the code:
CODE:
#include <stdio.h>
int main()
{
int a=10;
int 1=10;
print ("%d %d ", a, 1);
printf("Hello World ");
return 0;
}
Explanation:
In the above code, we declared two variables as a & 1 and assigned some value to them, then printed their values.
Let us see the implementation of the code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c1.png)
In the above output snippet, we see that it is throwing an error as the identifier is incorrect, i.e., we cannot name an identifier that starts with an integer value.
Even if the identifier is '1a', it will also give the same error because it cannot start with an integer value.
Below is the implementation if we use '1a' as the identifier.
CODE:
#include <stdio.h>
int main()
{
int a = 10;
int 1a = 10;
print("%d %d ", a, 1a);
printf("Hello World ");
return 0;
}
Explanation:
We have taken identifiers as 'a' and '1a' and assigned some values. Next, we are trying to print the values assigned to it.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c2.png)
We can see that the identifier declared as '1a' also throws the same error as the identifier is incorrect.
From this, we formulated that we cannot define an identifier name that starts with an integer value.
Now, how do we remove the error? Changing the identifier from starting with an integer value to starting with a non-integer value will not throw the error.
Let us deploy the code such that it doesn't throw an error:
CODE:
#include <stdio.h>
int main()
{
int a1=10;
int _a=10;
printf("%d %d \n ", a1, _a);
printf("Hello World ");
return 0;
}
Explanation:
In the above code, we have declared two identifiers as a1 and _a and assigned some values to them, and then we print them.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c3.png)
The above code is not producing an error in the declaration of the identifiers, and here we declare the identifiers as a1 and _a, in which both identifiers don't start with an integer value.
We can have the integer value after the non-integer values ex: a1. Here first, we have an alphabet, and then we have an integer value.
The only special character allowed in the identifier name declaration is _; we can see that in the second identifier name, we have declared it as _a, which also doesn't give any error.
From this, we can conclude that we cannot declare an identifier that starts with an integer or a value.
2) no spaces between the identifier:
When we declare an identifier, there should be no spaces present between the identifier.
Using a space while declaring an identifier will throw an error as no spaces are allowed in declaring an identifier.
Let us deploy a code by declaring an identifier with spaces to know the error:
CODE:
#include <stdio.h>
int main()
{
int a b=10;
int ab=10;
printf(" %d %d \n ", a b, ab);
printf("Hello World ");
return 0;
}
Explanation: in the above code, we have declared the variable as 'ab' and 'a b', assigned some values, and then printed them.
Below is the implementation of the deployed code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c4.png)
In the above, the output has generated an error as there is something wrong in declaring the variable, and the error is we have given the space while declaring the variable name.
So, if we avoid the space in between declaring an identifier, then it will not throw any errors:
Let us write a code in which variable doesn't contain spaces:
CODE:
#include <stdio.h>
int main()
{
int ab1=10;
int ab=10;
printf(" %d %d \n ", a b, ab);
printf("Hello World ");
return 0;
}
EXPLANATION:
In the above code, we declared the identifiers ab1 and ab, where variable names don't have any spaces between them. And then, we print the values of the declared identifiers. So, this code will not give any errors.
Below is the deployment of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c5.png)
From the above output, we can see that the code will not give any errors.
3)identifier should not be the same as the built-in identifiers.
What are built-in identifiers?
The names are already defined in their functionalities.
Let us understand this with an example:
In C, 'int' is used to declare the integer values, i.e., if we write int a, then variable a will store the values of the type integer.
Similarly, we have a large number of keywords that are already defined previously.
So, we cannot use those keywords as the identifier while declaring.
Let us implement a code using the identifier name as a predefined keyword in c.
CODE:
#include <stdio.h>
int main()
{
int int=10;
int float=10;
printf("%d %d\n",ab,ab);
printf("Hello World");
return 0;
}
Explanation:
In the above code, we have declared two identifiers, int and float, predefined keywords in c.
Let us see the error we get when we run the code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c6.png)
The above output shows that the error is generated as a wrong identifier or a wrong identifier name.
So, to avoid errors, we should not declare the identifier name as a predefined keyword.
The updated code is:
CODE:
#include <stdio.h>
int main()
{
int int1=10;
int float1=10;
printf("%d %d\n",int1,float1);
printf("Hello World");
return 0;
}
Explanation:
we can see that the identifiers are changed to int1 and float1, not keywords in the c language. So, this will not produce any errors.
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c7.png)
In the above output, no errors were generated when we changed the identifier names from int, float to int1, float1.
4) identifier should be unique:
In a program, we cannot declare an already-used identifier. i.e., we cannot use the same identifier multiple times while declaring it.
Let us see an example to understand this:
int name;
int name;
In the above, we have declared two integer identifiers with name and name, but according to the rules, we cannot define the same identifier multiple times.
So, if we use the same identifier multiple times, then it will produce an error called redefinition of the same identifier. i.e., we are declaring the same identifier multiple times.
Let us see the code where we declare the two identifiers identical.
CODE:
#include <stdio.h>
int main()
{
int name=10;
int name=10;
printf("%d %d\n",name,name);
printf("Hello World");
return 0;
}
Explanation:
in the above code, we have two identifiers as the same, leading to an error called redefinition of the identity.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c8.png)
The above output generates the error as a redefinition of the identifier 'name'.
From this, we learnt that we shouldn't use the same identifier multiple times.
To avoid this error, we will name the identifier's differently.
Let us deploy the code where we use different identifier names.
CODE:
#include <stdio.h>
int main()
{
int name1=10;
int name2=10;
printf("%d %d\n",name1,name2);
printf("Hello World");
return 0;
}
Explanation:
In the above code, we can see that we have declared the two identifiers as name1 and name2, which are different, so this code will not generate any errors because we have used different identifiers.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c9.png)
From the above output, we can see no errors generated in the output.
5) no special characters are allowed while declaring an identifier except '_'.
In declaring an identifier in c, we shouldn't use special characters like: !, @, #, $, %, ^, &, *, (, ) +, =.
We shouldn't use the above special characters in the identifier; if we use them, they will produce errors.
Let us deploy a code where we use the special characters as the identifier:
CODE:
#include <stdio.h>
int main()
{
int h@ = 10;
int h2 = 10;
printf(" %d %d\n ", h@, h2);
printf("Hello World ");
return 0;
}
Explanation:
We used the identifiers h@ and h2 in the above code. In the first identifier h@, we have used the special character @, which is not supposed to be used according to the rules. We should remove the use of special characters while declaring an identifier to remove errors.
Not only @, above declared all special characters cannot be used during the identifier declaration.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c10.png)
In the above output, we can see that the error is generated by declaring an identifier. i.e., the identifier name should not contain special characters.
To avoid this kind of error, we should avoid using special characters.
Below is the implementation of the code after removing the special characters.
CODE:
#include <stdio.h>
int main()
{
int h1 = 10;
int h2 = 10;
printf(" %d %d\n", h1, h2);
printf("Hello World ");
return 0;
}
Explanation:
We have changed the identifier name from h@ to h1, where we removed the special characters from the identifier name.
So now, it should not produce any errors.
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c11.png)
We can see no errors generated in the above output because we haven't used any special characters in the identifier.
But the only special character allowed is '_'; we can use this special character to declare an identifier.
Let us deploy a code using the identifier '_'.
CODE:
#include <stdio.h>
int main()
{
int _ = 10;
int h_2 = 10;
printf(" %d %d\n ", _, h_2);
printf("Hello World ");
return 1;
}
Explanation: the above code shows that the identifiers used are _, h_2. And assigned some values to both identifiers and then printed them.
Here _ is allowed while declaring an identifier, so the code should not produce any error:
Below is the implementation of the above code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c12.png)
We can see no errors in the output snippet in the above output.
6) Identifiers are case-sensitive:
A and a are different identifiers.
Let us understand this using an example:
int a;
int A;
The above two declarations are different, where a and A are different identifiers. i.e., uppercase and lowercase letters will make a difference in changing the identifier.
Also, name, nAme, Name, NAME, NAme, NAMe, naMe, … all these identifiers are different.
i.e., any change in the case of the letter will make a different identifier.
Let us implement a code to see the identifier with case sensitivity.
CODE:
#include <stdio.h>
int main()
{
int name = 10;
int Name = 20;
int NAme = 30;
int NAMe = 40;
int nAME = 50;
int NAME = 60;
int nAme = 70;
int NamE = 80;
printf("%d\n ", name);
printf("%d\n ", Name);
printf("%d\n ", NAme);
printf("%d\n ", NAMe);
printf("%d\n ", nAME);
printf("%d\n ", NAME);
printf("%d\n ", nAme);
printf("%d\n ", NamE);
return 1;
}
Explanation:
In the above code, we have taken multiple identifiers such as name, Name, NAme, NAMe, nAME, NAME, nAme, and NamE. Here every identifier is different from one another.
We have taken this identifier name to understand the concept of case sensitive in c.
Below is the implementation of the code:
![Identifiers In C](https://static.tutorialandexample.com/c/identifiers-in-c13.png)
We can see no errors in the above output because the defined identifiers are different.
These are the main rules to be followed while declaring the identifier.
Finally, an identifier length can be of any size; there is no restriction on the size of the identifier.