Identifiers in C

In C, identifiers are the names used to identify variables, functions, arrays, or other user-defined elements. These are essential building blocks in programming, as they allow you to assign meaningful names to elements in your code.

Rules for Naming Identifiers

When creating identifiers, there are specific rules you need to follow:

  1. An identifier can consist of alphabets (A-Z, a-z), digits (0-9), and underscores (_).
  2. The first character must be a letter or an underscore; it cannot start with a digit.
  3. Identifiers are case-sensitive (e.g., Variable and variable are different).
  4. Keywords cannot be used as identifiers.
  5. Special characters (like @, $, %) are not allowed in identifiers.

Examples of Valid and Invalid Identifiers

Identifier Type Examples Explanation
Valid `variable1`, `sum_total`, `_temp`, `count2` Start with a letter or underscore, followed by letters, digits, or underscores. No special characters or spaces.
Invalid `2variable`, `total-sum`, `void`, `@temp` Cannot start with a digit, use special characters (except `_`), or use reserved keywords like `void`.

Difference Between Identifiers and Keywords

While keywords are predefined and reserved by the language, identifiers are user-defined. For example:

Feature Identifiers Keywords
Definition Identifiers are the names assigned to variables, functions, arrays, or other user-defined elements. Keywords are predefined reserved words in C that have special meanings and cannot be used as identifiers.
Usage Used to name program elements like variables, constants, and functions. Used to define the structure and syntax of the program.
Rules Must begin with a letter (A-Z, a-z) or an underscore (_) and can be followed by letters, digits, or underscores. Must be used as defined by the C language, and their meanings cannot be altered by the user.
Examples `variableName`, `_function1`, `arraySize` `int`, `return`, `if`, `while`
Total Number Unlimited (user-defined). 32 (in C90 standard) or more in newer standards.

Example Programs Using Identifiers

1. Basic Variable Declaration

        
#include <stdio.h>
int main() 
{
  int age = 25;  // 'age' is an identifier
  float salary = 50000.50;  // 'salary' is an identifier
        
  printf("Age: %d\n", age);
  printf("Salary: %.2f\n", salary);
        
  return 0;
}
        
        

Output

Age: 25
Salary: 50000.50

2. Using Identifiers in a Function

        
#include <stdio.h>
// 'calculateSum' is an identifier for the function
int calculateSum(int a, int b) 
{
    return a + b;
}
        
int main() 
{
    int num1 = 10, num2 = 20;  // 'num1' and 'num2' are identifiers
        
    printf("Sum: %d\n", calculateSum(num1, num2));
        
    return 0;
}
        
        

Output

Sum: 30

3. Invalid Identifier Example

        
#include <stdio.h>
int main() 
{
  int 2value = 100;  // Invalid identifier (starts with a digit)
  printf("Value: %d\n", 2value);
        
  return 0;
}
        
        

Output

Compilation Error

4. Using Identifiers in Loops

        
#include <stdio.h>
int main() 
{
  for (int i = 1; i <= 3; i++)  // 'i' is an identifier
  {
     printf("Iteration: %d\n", i);
  }
        
 return 0;
}
        
        

Output

Iteration: 1
Iteration: 2
Iteration: 3