Identifiers in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In C programming, identifiers serve as fundamental elements, uniquely naming variables, functions, and other entities. These names, such as "section" in the example, distinguish entities within the program. C identifiers encompass a range of programming elements like variables, functions, arrays, and more. Comprising letters (uppercase and lowercase), underscores, and digits, identifiers must start with an alphabet or underscore. If used with external linkage, they are external identifiers; otherwise, they are internal identifiers. Identifiers are compositions of alphanumeric characters, totaling 63 (52 alphabetical characters, underscore, and 10 numerical digits), crucial for uniquely representing diverse programming elements in the C language.

Rules For Naming Identifiers in C

There are specific rules for naming the identifiers in C language.

  • We can not use any keyword as an identifier.
  • All the identifiers should have a unique name in the same scope.
  • Identifiers can not start with a digit.
  • The first character of an identifier should always start with an alphabet or underscore, and then it can be followed by any of the characters, digit, or underscore.
  • The special characters such as '*','#','@','$' are not allowed within an identifier.
  • All the identifiers are case sensitive means the identifiers "hello" and "Hello" will be treated differently. However, both names are identical, but one has a lowercase alphabet, and the other has an uppercase alphabet.
  • Length of an identifier should not exceed 31 characters.
  • Any blank spaces or commas are not allowed within an identifier.

Example of Identifiers in C

In the above C program, first, we created a function with the identifier add to find the sum. The function has two parameters with the name num1 and num2. In the main function, we have created three different data type variables with identifiers Character, double_number, and long_identifier_to_store_sum.

At last, created an array with the identifier arr.

Valid Identifiers in C

The valid identifiers in C are those identifiers that follow every single rule of the naming convention of identifiers that we have discussed above.

Examples of Valid C identifier

  • length - It contains only lowercase alphabets.
  • total_sum - It contains only '_' as a special character.
  • _size - It starts with an underscore '_'. * len_ - Contains lowercase alphabets and an underscore.
  • num1 - Here, the numeric digit comes at the end.
  • num_2 - It starts with lowercase and ends with a digit.

Invalid Identifiers in C

The Invalid identifiers are those identifiers that do not follow every single rule of the naming convention of identifiers.

Example of Invalid C identifier

  • 5size (it begins with a digit)
  • \@hello (starts with a special character other than '_')
  • int ( it is a keyword)
  • m n (contains a blank space)
  • m+n (contains a special character)

Types of Identifiers in C

There are two types of identifiers in C language.

  • Internal identifier
  • External identifier

External Linkage Process: When we write an implementation file (.c, .cpp, etc), the compiler generates a translation unit. A translation unit is a source file that is the combination of both implemented file and all the header files included in it.

Internal linkage refers to everything only in the scope of a translation unit. In contrast, External linkage refers to a whole program that combines all the translation units (or object files).

Internal identifier

Internal identifiers are the ones that are not used in any of the external link processes. Internal identifiers, also known as internal names; include the names of local variables. The internal identifier can be a local variable. It has at least 31 significant characters.

Difference between Keyword and Identifier

KeywordIdentifier
Keywords are special predefined reserved words which have special meanings and cannot be used elsewhere in the program.Identifiers are used to define some variables, structures, unions, integers and other things.
It specifies the type of some particular variable.It specifies the name of a particular variable.
It only consists of lowercase alphabets and starts with a lowercase.It can have both uppercase and lowercase, and it can start with the alphabet or underscore.
No special symbol can be used.Underscore can be used.
Example-: int, float, for, do, class etc.Example-: Name, Roll_Number etc.

Summary

  • Identifier is a string used to define or identify an entity.
  • Identifier is a user-defined word that is not already defined in the programming language and is defined by the user.
  • Identifiers in C should always be unique means no two entities have the same name.
  • Identifier should not be the same as the keywords which are already defined in the programming language.

See Also -