C Programs Tutorials | IT Developer
IT Developer

C Programming - C Identifiers



Share with a Friend

C Programming - C Identifiers

C Identifiers

In the C programming language, an identifier is the name used to identify variables, functions, arrays, or any user-defined element. Identifiers are fundamental for writing meaningful and readable code.

Characteristics of C Identifiers

  1. Uniqueness:
    • Each identifier must be unique within its scope.
  2. Case Sensitivity:
    • Identifiers are case-sensitive (age and Age are different).
  3. Defined Rules:
    • Must begin with a letter (A-Z or a-z) or an underscore (_).
    • Can be followed by letters, digits (0-9), or underscores.
    • Cannot use special symbols like @, $, %, etc.
    • Cannot be a keyword or reserved word.

Rules for Naming Identifiers

  1. Start with a Letter or Underscore:
    • Valid: name, _temp
    • Invalid: 1stName, -value
  2. No Spaces:
    • Valid: first_name
    • Invalid: first name
  3. No Special Characters:
    • Valid: value1
    • Invalid: value#1
  4. Avoid Keywords:
    • Valid: data
    • Invalid: int

Examples of Valid and Invalid Identifiers

Valid Identifiers Invalid Identifiers

total

123total

_result

total@result

num123

int

sum_of_numbers

sum of numbers

Examples in Code

Valid Identifiers

#include <stdio.h>

int main() {

    int age = 25;  // 'age' is a valid identifier

    float _salary = 50000.50;  // '_salary' is a valid identifier

    char grade = 'A';  // 'grade' is a valid identifier

   

    printf("Age: %d\n", age);

    printf("Salary: %.2f\n", _salary);

    printf("Grade: %c\n", grade);

   

    return 0;

}

Invalid Identifiers

#include <stdio.h>

int main() {

    int 1number = 10;  // Error: Identifier cannot start with a digit

    float total$ = 100.5;  // Error: Special characters are not allowed

    char return = 'B';  // Error: 'return' is a keyword

   

    return 0;

}

Best Practices for Naming Identifiers

  1. Use Descriptive Names:
    • Instead of x or y, use student_age or total_score for clarity.
  2. Follow Naming Conventions:
    • Use camelCase (studentAge) or snake_case (student_age) consistently.
  3. Avoid Ambiguity:
    • Choose names that clearly indicate their purpose.
  4. Limit Length:
    • Avoid extremely long names, but ensure they are descriptive.

Key Points

  • Identifiers are essential for writing readable and maintainable code.
  • Always follow the naming rules to avoid compilation errors.
  • Use meaningful names to enhance code clarity and ease debugging.