ADVERTISEMENT
ADVERTISEMENT

Identifiers in C with Examples

In the C programming language, an identifier is a name used to identify a variable, function, or other element in the code. Identifiers can be composed of letters, digits, and underscores, and must begin with a letter or an underscore.

Here are some examples of valid identifiers in C:

  • age
  • _temp
  • sum_of_squares
  • MAX_VALUE

In the examples above, "age" is an identifier for a variable that could represent someone's age, "_temp" is an identifier for a variable that could be used as a temporary storage location, "sum_of_squares" is an identifier for a variable or function that could be used to calculate the sum of squares of a set of numbers, and "MAX_VALUE" is an identifier for a constant that could represent the maximum value that a particular variable can take.

It's important to note that C has a set of reserved keywords that cannot be used as identifiers. These include keywords such as "int", "char", "if", and "while", among others. If you try to use a reserved keyword as an identifier, you will get a compile-time error.

For example, the following code would result in a compile-time error because "for" is a reserved keyword and cannot be used as an identifier:

int for;  // Compile-time error: "for" is a reserved keyword

Rules for constructing C identifiers

In the C programming language, there are a few rules that you need to follow when declaring identifiers:

  1. Identifiers must start with a letter or an underscore.
  2. Identifiers can only contain letters, digits, and underscores.
  3. No Commas or blank spaces  within an identifier.
  4. Identifiers are case-sensitive.
  5. Identifiers cannot be a reserved keyword.
  6. The length of the identifiers should not be more than 31 characters.

Here are some examples of valid and invalid identifiers in C:

Valid:

  • age
  • _temp
  • sum_of_squares
  • MAX_VALUE

Invalid:

  • 123abc (starts with a digit)
  • while (reserved keyword)
  • my-age (contains a hyphen)

It's important to choose descriptive and meaningful names for your identifiers to make your code easier to read and understand. Using descriptive names can also help you avoid naming conflicts and reduce the risk of introducing bugs into your code.


ADVERTISEMENT

ADVERTISEMENT