Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
Now Reading
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
When it comes to C programming, identifiers are a cornerstone that programmers cannot ignore. Identifiers refer to the names you give to entities such as variables, functions, and arrays to distinguish them in your code. This article will unravel the identifiers in C rules, types, scope, and more.
In C programming, identifiers are the names assigned to different entities such as variables, functions, arrays, etc. Here's a simple code example:
int score = 90; |
In this case, 'score' is an identifier that we've used to name our integer variable. There are several reasons why understanding identifiers is of paramount importance, especially in the C programming language.
1. Enhanced Readability and Maintenance: Effective use of identifiers makes your code self-explanatory, thereby helping programmers understand the code's purpose without diving deep into its implementation. For instance, consider the following code snippets:
int v = 3.14 * r * r; |
While this code is technically correct, it's not immediately apparent what 'v' and 'r' are. Now let's change the identifiers:
int area = 3.14 * radius * radius; |
With the use of 'area' and 'radius', the purpose of the variables is immediately clear, leading to an increase in code readability.
2. Debugging: Understanding identifiers and their rules can save a programmer from potential bugs.
3. Adhering to Language Syntax: Each programming language has its set of rules and syntax, and identifiers in C are no exception. There are regulations concerning valid characters, length, and reserved keywords, which, if violated, would lead to compile-time errors.
4. Efficient Memory Use: The scope of an identifier (whether it is local, global, or static) impacts the memory allocation and deallocation, affecting the overall performance of the program.
Identifiers, as the name suggests, are utilised to identify various elements in a program, such as variables, functions, arrays, etc. The freedom of naming these identifiers as we see fit can seem empowering, but with great power comes great responsibility. The responsibility here lies in adhering to certain rules and conventions while naming these identifiers.
Let's examine these rules in greater depth.
The first rule while naming an identifier is that it must start with either a letter (lowercase or uppercase) or an underscore. The subsequent characters can be a mix of letters (a-z or A-Z), digits (0-9), or underscores (_). Identifiers must only contain alphanumeric characters.
For example:
int _score1; // Valid identifier |
These identifiers adhere to the rules and will be accepted by the C compiler.
Contrary to valid characters, certain characters are not allowed in identifiers. These include special characters (e.g., $, @, #, %, etc.), spaces, punctuation, and identifiers that cannot start with a digit. Any identifier with these characteristics will be rejected by the compiler.
For instance:
int 123score; // Invalid identifier: starts with a digit |
The C compiler will throw an error when it encounters these identifiers.
While the ANSI C standard does not impose a specific limit on the length of identifiers, some older compilers may have had limitations. Modern compilers generally support longer identifiers, and the actual maximum length can vary depending on the compiler and implementation. It is recommended to choose meaningful and concise identifiers without excessive length, following good coding practices.
int number_of_students_in_the_class; // Too long |
Identifiers are case-sensitive, meaning the same sequence of characters but with a different case is treated as a different identifier. Therefore, 'score' and 'Score' are two distinct identifiers in C.
int score = 10; |
As demonstrated, 'score' and 'Score' are treated as two different variables holding separate values.
C language reserves certain words, known as keywords, for its syntax. These keywords carry special meanings for the compiler and, therefore, cannot be used as identifiers. Attempting to use such words as identifiers will result in a compilation error. Reserved keywords include int, char, for, while, if, etc.
int for = 10; // Invalid identifier: 'for' is a reserved keyword |
The identifier rules in C act as a guide to naming identifiers correctly, facilitating the creation of clean, error-free code.
While the rules for naming identifiers form the necessary foundation, naming conventions take a step further into the realm of good practices. Adhering to these conventions is widely regarded as a hallmark of high-quality, maintainable code.
Here are some widely adopted naming conventions in C:
1. Descriptive Names: Identifiers should accurately reflect the purpose of the variable, function, or other entity they're naming. For example, if a variable holds the age of a person, 'age' would be a more descriptive identifier than a vague name like 'a' or 'x'.
int age; // Good naming convention |
2. Use of Underscores: Underscores can be used to separate words in an identifier, making it more readable. This is commonly known as snake_case.
int student_age; // Good naming convention |
3. Case Sensitivity: Since C is case-sensitive, it's important to be consistent with the case you use for an identifier. It's easy to forget the case and inadvertently create or reference the wrong variable.
int score; // Consistent usage of lowercase |
4. Avoid Abbreviations: Where possible, avoid abbreviations or make sure they're explained clearly in the comments.
int num_students; // Clear identifier |
5. Consistency: Consistency is a key aspect of good naming conventions. If you're using snake_case for multi-word identifiers, stick to it throughout your code. If you're starting function names with a verb (like 'get_value', 'set_value'), do that for all your functions. Consistency makes your code much easier to navigate.
In the realm of C programming, each identifier type plays a unique role in how the program operates; hence understanding these types is critical for efficient coding. Let's delve deeper into the types of identifiers in C:
1. Variable Identifiers: Variable identifiers refer to memory locations where values are stored and manipulated throughout the code. For instance:
int score = 100; // 'score' is a variable identifier |
2. Function Identifiers: Function identifiers are names given to functions, which are blocks of code designed to perform specific tasks. The function identifier is used to call the function wherever its operation is needed.
void displayScore() { |
3. Array Identifiers: An array is a collection of elements of the same data type, and an array identifier is a name given to such an array. It represents the entire collection and can be used to access the individual elements.
int scores[5] = {90, 92, 88, 96, 94}; // 'scores' is an array identifier |
4. Structure Identifiers: Structures in C are user-defined data types that group variables of different data types. A structure identifier refers to such a structure and can be used to access its member variables.
struct student { |
5. Union Identifiers: Unions are similar to structures, i.e., they group variables of different data types. However, in a union, all member variables share the same memory location. A union identifier names such a union.
union job { |
6. Enumeration Identifiers: Enumerations, or enums, are user-defined data types that consist of integral constants. An enumeration identifier is a name given to such an enumeration.
enum week {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; // 'week' is an enumeration identifier |
7. Typedef Identifiers: Typedef is a keyword in C used to assign a new name to an existing data type. A typedef identifier is this new name, which can then be used in place of the original data type.
typedef unsigned long ulong; // 'ulong' is a typedef identifier |
Keywords and identifiers in C are fundamental concepts which might seem similar, though they have distinct roles within a program and follow different rules and conventions.
A keyword is a predefined word that C reserves for a specific purpose. Keywords are part of the language syntax, and each keyword carries a special meaning to the C compiler. Examples of keywords include int, char, for, while, if, etc. You cannot use a keyword as an identifier in your program.
An identifier, on the other hand, is a name given by the programmer to a particular entity in the code, such as a variable, function, array, etc. Identifiers are not predefined and are created by the programmer.
Let's compare the two using a table to outline their key differences:
Keyword | Identifier |
Predefined in the C language. | Defined by the programmer. |
Cannot be used as a variable name. | Used as names for variables, functions, arrays, etc. |
Each keyword carries a special meaning and is used in specific contexts. | An identifier doesn't inherently have a special meaning and can be used as needed by the programmer. |
Examples: int, float, return, for, while. | Examples: score, displayScore, student_age, week. |
The scope of an identifier in a C program refers to the region of the program where the identifier is recognised and can be accessed. It denotes the visibility and lifetime of an identifier. The scope of an identifier is defined by where it is declared within the C code.
A local identifier is declared within a function or a block and can only be accessed within that function or block. Once the execution leaves the function or block, the identifier ceases to exist. Hence, its scope is limited to the function or block where it's defined.
void display() { |
In the above example, the identifier 'score' is local to the function 'display'. It cannot be accessed outside this function.
A global identifier is declared outside all functions, typically at the top of the program. Global identifiers are accessible throughout the program from the point of declaration to the end of the program. They exist for the lifetime of the program.
int score = 100; // 'score' is a global identifier |
In this example, 'score' is a global identifier and can be accessed within the function 'display' as well as anywhere else in the program.
A static identifier retains its value even after the execution leaves its scope. The 'static' keyword is used to declare such identifiers. A static identifier can be local or global, but its value persists between function calls.
void display() { |
In the function 'display', 'count' is a static identifier. Even though 'count' is a local identifier within the 'display' function, it retains its value between function calls due to the 'static' keyword.
Storage Class Specifiers are used to control two fundamental properties of variables in C: scope (visibility) and lifespan (lifetime). These specifiers provide additional information about the variables and define how and where they should be stored in memory during program execution.
Storage Class Specifiers help communicate variable behaviour to the compiler, influencing storage location, duration, and access methods.
In the C language, there are four Storage Class Specifiers: auto, register, static, and extern. Let’s explore their unique purposes!
It is the default storage class for all local variables.
void myFunction() { |
It is used to define local variables that should be stored in a register instead of RAM.
void myFunction() { |
The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope.
void myFunction() { |
Extern storage class is used to give a reference of a global variable that is visible to ALL the program files.
extern int extern_var; // 'extern_var' is an extern identifier |
Understanding and effectively using identifiers and keywords in C is vital for anyone looking to master this powerful and versatile programming language. Remember, the right identifier naming practices can make your code more comprehensible, maintainable, and robust.
If you want to dig deeper into C programming and other programming languages, check out upGrad's comprehensive courses, such as DevOps Engineer Bootcamp, designed for all levels of learners.
1. What is an identifier in C?
An identifier in C is a name given to entities such as variables, functions, arrays, etc.
2. What are the rules for naming identifiers in C?
Identifiers in C must start with a letter or an underscore, followed by any combination of letters, digits, or underscores. They are case-sensitive and cannot be reserved keywords.
3. What is the difference between a keyword and an identifier?
Keywords are predefined words with special meanings in the C language. Identifiers, on the other hand, are user-defined names assigned to variables, functions, etc.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...