Identifiers in C

Identifiers in C. Identifiers in C Tutorial. Learn Identifiers in C
Identifiers in C

What are Identifiers in C?

Identifiers in C refer to the names given to various programming elements such as variables, functions, arrays, structures, and unions. These names are used to identify and access these programming elements within the code.

The purpose of identifiers is to give a meaningful name to programming elements so that they can be easily identified and used throughout the code. It also makes the code more readable and understandable for other programmers who may need to read or modify the code in the future.

Example:

int myNumber = 10;

Explanation:

In this example, “myNumber” is an identifier used to represent an integer variable with a value of 10.

Types of Identifiers in C:

  1. Variable Identifier: Used to identify a variable and its associated value.
  2. Function Identifier: Used to identify a function and its associated code block.
  3. Array Identifier: Used to identify an array and its associated elements.
  4. Structure Identifier: Used to identify a structure and its associated members.
  5. Union Identifier: Used to identify a union and its associated members.

Rules while declaring Identifiers in C:

  1. Identifiers cannot start with a number or special character.
  2. Identifiers can contain only letters, digits, and underscores.
  3. Identifiers are case-sensitive, meaning “myNumber” and “mynumber” are considered different.
  4. Identifiers should not be the same as keywords reserved by the C language.
  5. Identifiers should be descriptive and meaningful.

What is Internal Identifier?

Internal identifiers are used within a function to represent local variables, which can only be accessed within the same function.

Example:

#include <stdio.h>

void myFunction() {
    int myNumber = 5;
    printf("Internal identifier: %d", myNumber);
}

int main() {
    myFunction();
    return 0;
}

Output:

Internal identifier: 5

Explanation:

In this example, “myNumber” is an internal identifier used to represent a local variable within the “myFunction” function. It can only be accessed within the function and its value is printed using the printf statement.

What is External Identifier?

External identifiers are used to represent global variables, which can be accessed and modified from any function within the same program.

Example:

#include <stdio.h>

int myNumber = 5;

void myFunction() {
    myNumber++;
}

int main() {
    printf("External identifier before: %d\n", myNumber);
    myFunction();
    printf("External identifier after: %d\n", myNumber);
    return 0;
}

Output:

External identifier before: 5
External identifier after: 6

Explanation:

In this example, “myNumber” is an external identifier used to represent a global variable that can be accessed and modified from any function within the same program. Its initial value is printed before and after the “myFunction” function is called, which increments the value of “myNumber” by 1.

Modifications that can be made while using Identifiers:

In C programming language, identifiers are used to give names to various programming elements such as variables, functions, arrays, and so on. Here are some modifications that can be made while declaring identifiers in C:

  1. Case Sensitivity: C is a case-sensitive language, which means that lowercase and uppercase letters are considered different. For example, “myVariable” and “myvariable” are two different identifiers.
  2. Length: Identifiers can be of any length, but only the first 31 characters are significant. If an identifier is longer than 31 characters, the compiler will only consider the first 31 characters.
  3. Allowed Characters: An identifier can only contain alphabets (both uppercase and lowercase), digits, and underscores. Special characters such as @, #, $, %, etc. are not allowed.
  4. Starting with alphabets: An identifier must start with an alphabet (uppercase or lowercase) or an underscore. It cannot start with a digit.
  5. Reserved Keywords: Identifiers cannot be reserved keywords in C, such as int, float, double, char, void, etc.
  6. Meaningful Names: It is good programming practice to give meaningful names to identifiers that indicate their purpose. For example, a variable storing a person’s age could be named “age”.
  7. Camel Case: It is a common naming convention to use CamelCase for naming identifiers in C. In CamelCase, the first letter of each word is capitalized, except for the first word. For example, “myVariableName” is a valid identifier in C.

By following these modifications, you can declare meaningful and easily understandable identifiers in your C programs, making your code more readable and maintainable.

More: