Identifiers In C | Rules, Types, Valid, Invalid & More (+Examples)
Identifiers in C or any other programming language, for that matter, are just like names that help us recognize/ identify various entities like functions, variables, labels, user-defined data types, etc. It comprises a string of alphanumeric characters whose first character must either be an alphabetic letter or an underscore. The rest may be anything from the character set, like the alphabet, any digit, or even underline.
While we can define the identifiers, there are specific rules for doing so. In this article, we are going to know more about the character set in C, what is an identifier in C language, the rules for naming identifiers in C, the types of identifiers in C, and more.
The Character Set In C
Since the elements of the identifier must belong to the character set, it is important to know what that is if we want to learn about identifiers. It essentially refers to the set of characters that are permitted/ allowed for use in C programs. Note that the character set is based on the ASCII (American Standard Code for Information Interchange) character encoding standard.
There are total 4 types of character sets in C programming, which are as follows:
1. Alphabets
This set consists of both lowercase alphabets and uppercase alphabets. The C programming language accepts both of these as variables and functions. That is-
- Uppercase: A B C ................................... X Y Z
- Lowercase: a b c ...................................... x y z
2. Digits:
As is evident by the name, this set of characters consists of all numerical digits like- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and so on.
3. Special Characters:
The table below lists the various special characters that are a part of the character set in C.
Special Characters in C Programming |
||||
, |
< |
> |
. |
_ |
( |
) |
; |
$ |
: |
% |
[ |
] |
# |
? |
' |
& |
{ |
} |
" |
^ |
! |
* |
/ |
| |
- |
\ |
~ |
+ |
|
4. White Space Characters:
The space characters in C consist of newline, horizontal tab, carriage return, blank space, and form feed. Each of these characters has its own purpose in C code. For example, the newline escape sequence shifts the cursor to the next line.
What Are Identifiers In C?
Now that we know what a character set is let's move to discuss what identifiers in C are in greater detail.
Identifiers are a string of characters that serves as the name (hence identifier) of a variable, function, array, pointer, structure, etc. In other words, in the C programming language, an identifier can be described as the user-specified name used to identify a specific item. This name could be a variable name, function name, array name, pointer name, structure name, label, etc.
This way, programmers can give custom names to various entities when writing a program. It helps uniquely identify an entity during program execution. Now, let's look at some common examples and then a code example of identifiers in C.
Here are some common examples of identifiers in C:
int number;
double point;
Here, the terms- number and point are the two identifiers that we have selected to represent variables of data type integer and double, respectively.
Code Example:
Output:
b
3.760000
The sum is: 7
Explanation:
In the simple C program above, we begin by including the input/ output header file, i.e., <stdio.h>.
- We then define a function called sum, which takes two integer variables, num1 and num2, as parameters.
- It returns the sum of the two variables calculated using the addition arithmetic operator.
- Then, we define the main() function, which is the entry point of the program.
- Inside main(), we declare two variables, i.e., Character (of type char) and double_number (double/ floating point literal). We then initialize these variables with the string value b and the value 3.7600, respectively.
- These variable names are the identifiers, which we then use in two printf() statements to display the values to the console.
- Next, we call the sum function with values 3 and 4 as parameters. The result of this addition function is stored in the variable long_identifier_to_sum. This is the identifier for the integer variable here.
- Again, we use a printf() statement to display the sum to the console, with a message through a formatted string.
- Finally, the program's main() function terminates with a return 0, indicating no errors in execution.
Note: Identifiers form a part of the umbrella term token in C. Read more about it here- Tokens In C | A Complete Guide To 7 Token Types (With Examples)
Rules For Naming/ Defining Identifiers In C
As we have mentioned in the introduction, while identifiers give the freedom to define custom names, every programmer must adhere to a set of rules when doing so.
Here is the list of rules and regulations to follow when defining identifiers in C:
- The identifier cannot begin with a digit/ numeric digit.
- The initial letter/ first element of an identifier should always either be an underscore or an alphabet.
- The letters that follow the first could be a character, a number, or an underscore in the identifier.
- However, note that no special symbol can be used.
- The alphabetical characters can be written in either uppercase letters or lowercase letters.
- This is because identifiers are case-sensitive and can distinguish between uppercase and lowercase letters.
- Commas and blank spaces are not permitted when naming an identifier.
- The length of an identifier in C is limited to 31 characters.
- The use of keywords in C as an identifier is strictly prohibited. That is, you cannot use an in-built keyword as an identifier as well.
- And lastly, identifiers must be short yet descriptive and also simple to understand.
Types Of Identifiers In C
Identifiers in C can primarily be divided into two types. They are-
- Internal identifier
- External identifier
We will explore both these types of identifiers in C in detail ahead.
Internal Identifiers In C
The term internal identifier refers to an identifier that is used internally within a program and cannot be utilized in an external connection or external linkage. For example, internal identifiers are variable names or function names that are used in a C program, used to refer to these program entities.
In this sense, these identifiers are local to a specific scoop/ module of the program, meaning they can only be accessed within a specific block or function. Local variables may also serve as internal identifiers. Also, these identifiers are not visible or accessible from outside that specific scope or module.
Syntax:
{
data_type identifier;}
Here,
- The data_type refers to the type of data stored in the variable whose name is given by the term identifier.
- The curly brackets signify that the variable (or any other element) is being declared inside a specific code block.
Code Example:
Output:
x=20
y=10
Explanation:
In the sample C code above-
- We declare an integer variable x and initialize it to the value 20 inside the main() function. Then, we display the value of x using a printf() function.
- Next, we initialize another variable, y, inside another code block with the value of 20 and print it to the console using the printf() function.
- Now, as mentioned in the code comments, if we try to access y outside of the given code block, for example, to print its value, the compiler will throw an error.
- This is because x is defined outside of any blocks, so it is an external identifier that can be retrieved from anywhere in the code.
- But y is declared inside a block using curly brackets, which means that it can only be accessed within that block.s
- We cannot access y from outside of that block, so when the program tries to display the value of y once again outside of the block, a compilation error happens. An error happens because y is not provided at that scope.
External Identifier
The term external identifier refers to an identifier that is utilized in an external linkage. That is, these are those identifiers in C that, when declared in one source file, can be accessed by another source file in the same program. Function names and global variables can both be used as external identifiers.
Syntax:
<data_type> <identifier>;
Here, the elements are the same as in the case of internal identifiers; the only difference is there is no specific code block where the element is being declared/ defined.
Code Example:
Output:
The value of num is 10
Explanation:
In this example-
- We first define and initialize an integer variable num to the value 10 outside of the main() function.
- Then, inside main(), we use the printf() function to display the value of the variable on the console.
- Here, num is an external identifier defined at the top of the file, outside of any functions.
Check this out- Boosting Career Opportunities For Engineers Through E-School Competitions
Invalid Identifiers In C
Invalid identifiers are those that go against the rules or suggestions given in the c programming language. Protected in this listing are names that-
- Begin with a variety of or comprise special characters.
- Are reserved phrases or are keywords in C.
- Are too long.
- Incorporate special characters.
The way to make code comprehensible and clean is then to use proper, meaningful names for identifiers. It is also vital to stick to the policies of the language to keep away from syntax mistakes and different troubles.
Here are some invalid identifiers:
$var; // incorrect
x!y; // incorrect
my var;//incorrect
Now, let's take a look at an example that shows what happens when we use invalid identifiers in C programs.
Code Example:
Output:
error: expected initializer before 'var'
char my var;
^~~
error: expected initializer before '!' token
double x!y;
^
Explanation:
Multiple syntax mistakes in the code above prevent it from compiling and executing properly. The mistakes are as follows:
- A space, which is not permitted in C variable names, appears in the character variable name- my var.
- The mistake in the variable name- x!y is the use of the exclamation mark, which is a special character and is not permitted for use in identifiers.
It is clear that one must select appropriate variable names that follow the convention of identifiers in C in order to avoid or correct these mistakes. Variable names can contain letters, digits, and underscores and should begin with a letter or underscore. Exclamation points and spaces are not permitted.
Valid Identifiers In C
The term valid identifiers refers to those identifiers that the C language recognizes as names for variables, functions, and other program entities. After the initial letter or underscore character in an identifier, any combination of characters, numbers, or underscores may follow.
The identifiers will be used in the program and understood by the C compiler if these rules are followed. It is essential to choose names for identifiers that are useful and illustrative in order to make the code easier to understand and read.
Here are some valid identifiers:
total; // correct
sum_avg; // correct
my_var;//correct
_number;//correct
Now, let's take a look at an example C code where we use valid identifiers to gain a deeper understanding of the same.
Code Example:
Output:
my_variable = 1
average_grade = 87.500000
first_initial = J
pi = 3.141593
message = Hello, world!
Explanation:
- We declare and initialize 5 variables of different data types in the main() function. Here-
- The integer variable called my_variable is assigned the value 1 using the basic assignment operator.
- Float and double variables avg_grade and x (respectively) are assigned values 87.5 and 3.14159..., respectively.
- The character variable initial is assigned a value J, and the character string message is assigned the string value- Hello, World!
- Next, we use a series of printf() statements to display the value of these variables to the console with messages in formatted strings with their variable name identifiers.
- Here, we use format specifiers, i.e., %d for integer, %f for floating-point value, %c for character, %lf for long floating-point/ double, and %s for string.
- Lastly, the program terminates with a return 0 statement, indicating a successful execution.
Overall, this output displays how several format specifiers are employed in C to print values of various data kinds.
Read about the constant identifier here- Constant In C | How To Define & Its Types Explained With Examples
Using Keywords As Identifiers In C
As per the guidelines for naming identifiers, one cannot use keywords for naming entities. If someone attempts to use keywords for identification, an error warning will appear. It is, hence, best to avoid using keywords as identifiers, which is the practice of using reserved words from a programming language as variable names or other identifiers in a program.
Code Example:
Output:
error: two or more data types in declaration specifiers
int void = 0;
^
Explanation:
In this example-
- We declare an integer variable with the identifier name while inside the main() function and initialize it with the value of 10.
- The keyword while is marked for use when defining a loop control construct (specifically the while loop) that regularly runs a piece of code while a specific condition is true.
- So, the program given above breaks the language's syntax rules by attempting to use it as a variable name. This will cause the compiler to produce an error, as shown in the output.
Differences Between Keywords And Identifiers In C
Listed in the table below are some common differences between keywords and identifiers in C.
Keyword |
Identifier |
A keyword is an in-built or predefined word. |
The identifier is a user-defined word. |
It must be typed in lowercase letters only. |
Both lowercase and capital letters may be used to write identifiers. |
The C compiler has pre-defined its special meaning. |
The C compiler does not define its meaning. |
It consists of alphabetic characters only. |
It consists of several alphanumeric characters. |
There is no underscore character in it. |
There is an underscore character in it. |
Frequently Asked Questions
Q. What is the maximum length of the identifier in C?
The maximum length of an identifier in the C programming language (such as a variable name, function name, etc.) is implementation-defined, which means that it might differ depending on the compiler being used.
However, identifiers that are at least 31 characters long are supported by the majority of current compilers. While some compilers may support lengthier identifiers, it is often advised to keep identifier names short and useful for readability and maintenance purposes.
Q. What are the examples of identifiers in C?
Variables, functions, arrays, and other user-defined objects are identifiers in the C programming language. Some examples of valid C identifiers are given in the table below:
variableName | Max_Of_Numbers |
functionName | employee_record |
arrayName | customerName |
_underscore | totalAmount |
MAX_VALUE |
Q. What is the size of the identifiers?
The size of an identifier in the C programming language, or the amount of bytes it takes up in memory, depends on the implementation and is often not expressly stated by the language standard.
- However, the underlying hardware architecture and the particular version of the C compiler in use are often what define an identifier's size.
- An identifier, for example, might take up 4 bytes of memory on a 32-bit system while taking up 8 bytes on a 64-bit machine.
- The size of an identifier is usually quite small compared to other data types used in the program. Hence it is generally not a serious worry for most programs.
Q. What is the range of identifiers in C?
In C, the implementation determines the scope of an identifier (such as a variable name, function name, etc.).
- The C standard only defines an identifier's minimum length, which is one character.
- Letters (both capital and lowercase), numbers, and underscores can all be used as identifiers.
- However, a letter or an underscore must always be the first character in an identifier; numbers are not permitted as the first character.
Practical identifier length restrictions apply to the majority of C implementations. According to the C standard, an implementation shall handle identifiers of at least 31 characters. However, longer identifiers might be supported by some compilers.
Q. What is the limit of identifiers in C?
The language standard for the C programming language cannot determine the actual length limit of an identifier. It is rather up to the implementation (i.e., the compiler and platform) to make that decision.
The majority of contemporary C compilers typically let identifiers have a maximum length of 31 characters, while certain compilers may support greater IDs. Regardless of the particular restriction set by the compiler, it is typically advised to keep the names of identifiers in C concise and understandable for readability and maintenance.
Here are a few other topics for you to explore:
- 6 Relational Operators In C & Precedence Explained (+Examples)
- Increment And Decrement Operators In C With Precedence (+Examples)
- Conditional/ If-Else Statements In C | The Ultimate Guide
- For Loop In C | Structure, Working & Variations With Code Examples
- Difference Between C And C++ Explained With Code Example
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Sharon Teoh 1 year ago