Identifiers in C Language – Rules, Types, Examples & More

Updated on September 19, 2024

Article Outline

Identifiers are a fundamental concept in the C programming language. They serve as a fundamental element in the program, including variables, functions, and other entities. Identifiers must start with the alphabet or underscore. This article explains the identifiers in the C language and their usage and rules.

What are Identifiers?

Identifiers are names in a programming language that define the identified variables, functions, arrays, or other user-defined items. They allow programmers or developers to access and manipulate them, and they must adhere to specific naming conventions and rules. In simple words, identifiers allow you to reference and manipulate these entities throughout your source code. For instance, when you declare a variable like int age, age is an identifier in the C programming language.

 

The following program demonstrates the Identifiers in C language:

 

Program

#include <stdio.h> float calculate_area(float radius) { const float PI = 3.14159; float area = PI * (radius * radius); return area; } int main() { float radius = 5.0; float area = calculate_area(radius); printf("The area of the circle with a radius of %.2f is %.2f.n", radius, area); return 0; }

Output

The area of the circle with a radius of 5.00 is 78.54.
*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Rules for Naming Identifiers in C

The C Developer must follow some rules before defining the Identifiers:

 

  • We cannot use any keyword as an identifier in C language.
  • All identifiers should have a unique name in the scope.
  • Identifiers can not start with a digit.
  • The identifier’s first character should always start with an alphabet or underscore, and then it can be followed by any character, digit, or underscore.
  • Special characters are not allowed within an identifier.
  • The identifier length should not exceed 31 characters.

Example of Identifiers in C

 

Program 1

#include <stdio.h> int globalVar = 10; void exampleFunction(); void First() { int localVar = 20; static int staticVar = 30; register int regVar = 40; printf("Inside exampleFunction:n"); printf("localVar (local variable) = %dn", localVar); printf("staticVar (static variable) = %dn", staticVar); printf("regVar (register variable) = %dn", regVar); staticVar++; } int main() { int mainVar = 50; printf("Inside main:n"); printf("globalVar (global variable) = %dn", globalVar); printf("mainVar (local variable) = %dn", mainVar); First(); First() ; int _number = 60; return 0; }

Output

Inside main: globalVar (global variable) = 10 mainVar (local variable) = 50 Inside exampleFunction: localVar (local variable) = 20 staticVar (static variable) = 30 regVar (register variable) = 40 Inside exampleFunction: localVar (local variable) = 20 staticVar (static variable) = 31 regVar (register variable) = 40

Program 2

#include <stdio.h> struct _student { int id; int class; char section; }; void isEven(int num) { if(num%2==0){ printf("It is an Even Number"); } else{ printf("It is not an Even Number"); } } void main() { int studentAge = 20; double Marks = 349.50; isEven(453);   }

Output

It is not an Even Number

Valid Identifiers in C

Let’s see the valid identifiers in C language that follow every rule of the naming convention of identifiers we have already discussed in the above section.

 

Example of Valid C Identifier:

 

  • Length: This identifier is valid because it contains only lowercase letters.
  • Total_sum: This contains only ‘_’ as a special character
  • _size: It starts with an underscore ‘_’ *len_ contains lowercase alphabets and an underscore.
  • Rajkumar1 : This identifier is also valid because it followed the rules.

 

Also Read: Quick Sort in C

Invalid Identifiers in C

Let’s see the invalid identifiers in C language that do not follow every single rule of the naming convention of identifiers.

 

Example of Invalid C Identifier:

 

  • 5num : It begin with a digit
  • @hello: It starts with a special character other than _
  • int : It is a predefined keyword
  • r n : It contains the blank space between the alphabet
  • M+n: It contains the special character

Difference between Keyword and Identifier

The difference between keywords and identifiers is fundamental in programming, particularly in languages like C.

 

Keyword Identifier
This is the reserved keyword, which is predefined by the language, used to perform specific functions or represent specific constructs The programmer creates the name to identify the variable function in the program.
To define the syntax and structure  of the programming languages To label and access program elements
This cannot be changed or redefined because the compiler already defines it They are user-defined and can be anything the programmer chooses within naming rules.
It is used directly by the language to perform operations (e.g., if, while, and int). The programmer uses it to name elements like variables, functions  and objects
int, return, class, public, if myVariable, calculate, UserProfile

 

Also Read: strcmp Function in C

Conclusion

In this article, we learned about the C language’s identifiers. Identifiers are essential for naming and acting on program elements, such as variables, functions, and arrays. Identifiers follow some specific rules, including starting with a letter or underscore and being followed by letters, digits, or underscores. By Understanding Identifiers, developers can enhance the readability and maintainability of source code.

FAQs
No, Identifiers cannot be the same as C keywords. Keywords have predefined meanings in C and are reserved by the language.
No, C-identifiers cannot start with a digit. They must begin with a letter or an underscore. Subsequent characters can include digits.
Yes, Identifiers are case-sensitive in C. This means variable, Variable, and VARIABLE are considered different identifiers.
No, identifiers cannot include whitespace characters . Whitespace is not allowed in identifiers and will result in a syntax error. Identifiers must be a continuous sequence of letters, digits or underscores.
Yes, identifiers can be used as labels in switch statements.

Updated on September 19, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

IIT Courses

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved