Identifier in C helps in identifying variables, constants, functions etc., in a C code. C, being a high-level computer language, allows you to refer to a memory location with a name instead of using its address in binary or hexadecimal form.
C IdentifiersIdentifiers are the user-defined names given to make it easy to refer to the memory. It is also used to define various elements in the program, such as the function, user-defined type, labels, etc. Identifiers are thus the names that help the programmer to use programming elements more conveniently.
When a variable or a function is defined with an identifier, the C compiler allocates it the memory and associates the memory location to the identifier. As a result, whenever the identifier is used in the instruction, C compiler can access its associated memory location. For example, when we declare a variable age and assign it a value as shown in the following figure, the compiler assigns a memory location to it.
Even if the programmer can use an identifier of his choice to name a variable or a function etc., there are certain rules to be followed to form a valid identifier.
Naming Rules of C IdentifiersGiven below are the rules using which an identifier is formed −
As per the above rules, some examples of the valid and invalid identifiers are as follows −
Valid C Identifiersage, Age, AGE, average_age, __temp, address1, phone_no_personal, _my_nameInvalid C Identifiers
Average-age, my name, $age, #phone, 1mg, phy+mathsExamples of C Identifiers
The following program shows an error −
#include <stdio.h> int main () { /* variable definition: */ int marks = 50; float marks = 65.50; printf("%d %f", marks, marks); return 0; }Error
main.c: In function 'main': main.c:7:10: error: conflicting types for 'marks'; have 'float' 7 | float marks = 65.50; | ^~~~~ main.c:6:8: note: previous definition of 'marks' with type 'int' 6 | int marks = 50; | ^~~~~ main.c:8:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=] 8 | printf("%d %f", marks, marks); | ~^ ~~~~~ | | | | int double | %f
In C language, the scope of identifiers refers to the place where an identifier is declared and can be used/accessed. There are two scopes of an identifier:
Global IdentifiersIf an identifier has been declared outside before the declaration of any function, it is called as an global (external) identifier.
Example#include <stdio.h> int marks= 100; // external identifier int main() { printf("The value of marks is %d\n", marks); }
Output
The value of marks is 100
This is because marks is defined outside of any blocks, so it is an external identifier.
Local IdentifiersOn the other hand, an identifier inside any function is an local (internal) identifier.
Example#include <stdio.h> int main() { int marks= 100; // internal identifier printf("The value of marks is %d\n", marks); }
Output
The value of marks is 100
This is because marks is defined inside main function, so it is an internal identifier.
Examples of Different Types of C IdentifiersIdentifiers can also appear in a forward declaration of a function. However, the declaration signature of a function should match with the definition.
Example of Variable Identifierint marks1 = 50, marks2 = 60; float avg = (float) (marks1+marks2)/2;Example of Function Identifier
int average(int marks1, int marks2) { return (float) (marks1+marks2)/2; }Example of User-defined Type Identifier
struct student { int rollno; char *name; int m1,m2,m3; float percent }; struct student s1 = {1, "Raju", 50, 60, 70, 60.00};Example of Typedef Identifier
struct student { int rollno; char *name; int m1,m2,m3; float percent }; typedef struct student STUDENT; STUDENT s1 = {1, "Raju", 50, 60, 70, 60.00};Example of Label Identifier
#include <stdio.h> int main() { int x=0; begin: x++; if (x>=10) goto end; printf("%d\n", x); goto begin; end: return 0; }
Output
1 2 3 4 5 6 7 8 9Example of Enum Identifier
#include <stdio.h> enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun}; int main() { printf("The value of enum week: %d\n",Mon); return 0; }
Output
The value of enum week: 10
Thus, the identifiers are found everywhere in the C program. Choosing right identifier for the coding element such as the variable or a function is important for enhancing the readability and debugging and documentation of the program.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4