How to use C language identifiers?
The naming rules for identifiers in the C language are used to name variables, functions, arrays, etc.
- It can only consist of letters (both upper and lower case), numbers, and underscores.
- The first character must be a letter or underscore.
- Identifiers are case-sensitive, for example, “abc” and “ABC” are different identifiers.
- You cannot use keywords in C language as identifiers, such as “int”, “for”, etc.
- There is no limit on the length of identifiers, but it is generally recommended not to exceed 32 characters.
Here are some examples:
- Valid identifiers: num, count, student_name, avg_score, _total, isPassed
- Invalid identifiers: 4num (starting with a number), for (keyword), studentName (containing illegal character “-“)
When using identifiers, the following standards should be followed:
- Identifiers should be descriptive and clearly reflect the meaning they represent.
- Choose between using camelCase or snake_case to name identifiers. In camelCase, the first letter of each word is capitalized, for example: getAvgScore; whereas in snake_case, words are separated by underscores, for example: get_avg_score.
- Avoid using abbreviations and simplified names, instead, use full words to name identifiers.
- Do not use the same identifier to name different variables or functions in the same scope, as this can lead to conflicts.