Introduction to Identifiers in C

In C programming, identifiers are names given to various program elements like variables, functions, arrays, etc. Identifiers make it easier for programmers to understand the code, as they allow referencing different parts of the program by meaningful names.

Rules for Identifiers

As per the above rules, some examples of the valid and invalid identifiers are:

Valid C Identifiers:

age, Age, AGE, average_age, __temp, address1, phone_no_personal, _my_name

Invalid C Identifiers:

Average-age, my name, $age, #phone, 1mg, phy+maths

Syntax for writing Identifiers

<data_type> identifier_name;

Example of Identifiers using C program

#include <stdio.h>

// Function prototype
void greetUser();

// Global variable
int count = 5;

int main() {
    // Local variable
    int localValue = 10;

    greetUser(); // Function call

    printf("Global count: %d\n", count);        // Print global variable
    printf("Local value: %d\n", localValue);    // Print local variable

    return 0;
}

// Function definition
void greetUser() {
    printf("Hello, user!\n");
}

Explanation

Types of Identifiers in C

Example of Variable Identifier

int 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-define Identifier

struct student 
{
    int rollno;
    char *name;
    int m1, m2, m3;
    float percent;
};

struct student s1 = {1, "Raju", 50, 60, 70, 60.00};

Common Mistakes with Identifiers

Best practice for writing identifiers

Conclusion

Identifiers are fundamental in making the code readable, organized, and understandable for others working on the same code. Remember to follow the rules and best practices for creating effective identifiers in C programs.