Identifiers in C
A C language identifier is a label or symbolic name that the developer can assign to variables, constants, functions, and so on. Identifiers are used to identify and access these entities within a program.
In this lesson, we will focus on the rules to follow when creating identifiers in C and which keywords to avoid using as identifiers.
Identifiers
When we create programs in the C language, we often need to assign names to variables, constants, functions, and so on. These names are called identifiers.
In C, an identifier can contain letters, digits, and the underscore character _
. However, the fundamental rule is that an identifier cannot start with a digit.
Here are some examples of valid identifiers:
number
number1
number_1
_value_
Here are some examples of invalid identifiers:
1number
value!
10_times
next-item
In these cases:
1number
starts with a digit;value!
contains an invalid character;10_times
starts with a digit;next-item
contains an invalid character.
Identifiers in the C Language
In the C language, an identifier is a name assigned to variables, constants, functions, and so on.
An identifier may contain letters, digits, and the underscore _
, but it may not begin with a digit.
Another important feature of the C language is that it is case-sensitive. This means that uppercase and lowercase letters are considered distinct characters.
For this reason, the following identifiers are considered different:
number
Number
NUMBER
The identifiers shown above are considered three distinct identifiers, and therefore can be used for different purposes. However, this is not good programming practice, as it can lead to confusion.
The C Language Is Case-Sensitive
The C language is case-sensitive, so uppercase and lowercase letters are treated as different characters.
Be careful not to use similar identifiers that differ only in case, as this can lead to confusion and programming errors.
Because of this, C developers often adopt naming conventions. These are not rules enforced by the language, but rather guidelines that help make code more readable and maintainable.
For example, to name variables, it is common to use all lowercase letters, separated by underscores _
in the case of compound names. For example:
number_of_elements
maximum_value
loop_count
This naming style is called snake_case.
Other developers prefer using camelCase notation. In this case, the first letter of each word—except the first—is capitalized. For example:
numberOfElements
maximumValue
loopCount
There is no correct style. The choice depends on the programmer and the guidelines of the development team. The important thing is to maintain a consistent style throughout the project.
The C language standard does not impose a maximum limit on the length of identifiers. Usually, the limit is enforced by the specific compiler used. In any case, it is always good practice to use descriptive names, so that the code is clearer and more readable.
For example, the identifier numberOfElements
is clearer and more descriptive than n
.
Identifier Length in the C Language
The C language does not impose a maximum length for identifiers.
However, it is always good practice to use descriptive names to make the code clearer and more readable.
Keywords
In C, some identifiers have a special meaning to compilers and cannot be used as variable, constant, or function names. These identifiers are called keywords and are considered reserved.
Below is a table of the C language's keywords. It is not important to memorize them all now, but it's useful to know them to avoid using them as variable, constant, or function names.
As we continue studying the C language, we'll see how to use these keywords appropriately.
Keyword | Description |
---|---|
auto |
Defines a local variable |
break |
Exits a loop or switch |
case |
Label for a switch |
char |
Defines a character |
const |
Defines a constant |
continue |
Skips to the next loop iteration |
default |
Default label for a switch |
do |
Begins a do-while loop |
double |
Defines a floating-point number |
else |
Part of an if-else statement |
enum |
Defines an enumerated type |
extern |
Defines an external variable |
float |
Defines a floating-point number |
for |
Begins a for loop |
goto |
Jumps to a specific code location |
if |
Begins an if statement |
inline |
Declares an inline function (C99) |
int |
Defines an integer |
long |
Defines a long integer |
register |
Defines a register variable |
restrict |
Declares a restricted pointer (C99) |
return |
Returns a value from a function |
short |
Defines a short integer |
signed |
Defines a signed integer |
sizeof |
Returns the size of a type |
static |
Declares a static variable |
struct |
Defines a structure |
switch |
Begins a switch statement |
typedef |
Defines a data type |
union |
Defines a union |
unsigned |
Defines an unsigned integer |
void |
Defines a void type |
volatile |
Declares a variable that can be externally modified |
while |
Begins a while loop |
_Bool |
Defines a Boolean type (C99) |
_Complex |
Defines a complex type (C99) |
_Imaginary |
Defines an imaginary type (C99) |
Keywords in the C Language
Keywords are reserved identifiers that have a special meaning to compilers and cannot be used as variable, constant, or function names.
Since the C language is case-sensitive, keywords must be used exactly as shown in the table. For example, if
is a keyword, but IF
or If
are not.
Avoid Using Identifiers Similar to Keywords
A very important tip is to avoid using names similar to keywords, but with different casing, as identifiers in your programs.
For example, avoid using WHILE
or If
as identifiers, as they may cause confusion with the keywords while
and if
.
In Summary
In this lesson, we explored the concept of identifiers in the C language. Identifiers are names assigned to variables, constants, functions, and so on.
We saw that an identifier can contain letters, digits, and the underscore _
, but cannot start with a digit. Also, C is case-sensitive, meaning uppercase and lowercase letters are treated as different characters.
Finally, we introduced the keywords of the C language, which are reserved identifiers with special meaning for compilers. It's important to avoid using them as variable, constant, or function names.
In general, it is good practice to adopt a consistent naming style for identifiers, in order to make code more readable and maintainable.