In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any other user-defined items. It is a name that uniquely identifies a program element and can be used to refer to it later in the program.
Example:
C
// Creating a variable
int val = 10;
// Creating a function
void func() {}
In the above code snippet, "val" and "func" are identifiers.
Rules for Naming Identifiers in C
A programmer must follow a set of rules to create an identifier in C:
- Identifier can contain following characters:
- Uppercase (A-Z) and lowercase (a-z) alphabets.
- Numeric digits (0-9).
- Underscore (_).
- The first character of an identifier must be a letter or an underscore.
- Identifiers are case-sensitive.
- Identifiers cannot be keywords in C (such as int, return, if, while etc.).
The below image and table show some valid and invalid identifiers in C language.
Example
The following code examples demonstrate the creation and usage of identifiers in C:
Creating an Identifier for a Variable
C
#include <stdio.h>
int main() {
// Creating an integer variable and
// assign it the identifier 'var'
int var;
// Assigning value to the variable
// using assigned name
var = 10;
// Referring to same variable using
// assigned name
printf("%d", var);
return 0;
}
If you are not familiar with variables and functions, don’t worry! We will discuss them in the later sections.
Creating an Identifier for a Function
C++
#include <stdio.h>
// Function declaration which contains user
// defined identifier as it name
int sum(int a, int b) {
return a + b;
}
int main() {
// Calling the function using its name
printf("%d", sum(10, 20));
return 0;
}
Naming Conventions
In C programming, naming conventions are not strict rules but are commonly followed suggestions by the programming community for identifiers to improve readability and understanding of code. Below are some conventions that are commonly used:
For Variables:
- Use camelCase for variable names (e.g., frequencyCount, personName).
- Constants can use UPPER_SNAKE_CASE (e.g., MAX_SIZE, PI).
- Start variable names with a lowercase letter.
- Use descriptive and meaningful names.
For Functions:
- Use camelCase for function names (e.g., getName(), countFrequency()).
- Function names should generally be verbs or verb phrases that describe the action.
For Structures:
- Use PascalCase for structure names (e.g., Car, Person).
- Structure names should be nouns or noun phrases.
Keywords vs Identifiers
Here’s a table that highlights the differences between keywords and identifiers in C:
Feature | Keyword | Identifier |
---|
Definition | A keyword is a reserved word with a special meaning in C that cannot be used as an identifier. | An identifier is a user-defined name used to refer to variables, functions, arrays, etc. |
---|
Usage | Keywords are predefined in the C language and are used to define the structure and control flow of the program (e.g., int, if, while). | Identifiers are used by programmers to name variables, functions, and other user-defined elements (e.g., age, sum, main). |
---|
Example | int, return, for, if, while | totalAmount, studentAge, calculateTotal |
---|
Modification | Keywords cannot be modified or used for any other purpose. | Identifiers can be created and used as per the programmer’s needs. |
---|
Position in Code | Keywords are part of the syntax of C and are used to structure the program. | Identifiers are used for variable names, function names, and more throughout the code. |
---|
Case Sensitivity | Keywords are case-sensitive (e.g., int and Int are different). | Identifiers are also case-sensitive (e.g., age and Age are different). |
---|
What happens if we use a keyword as an Identifier in C?
In the below code, we have used const as an identifier which is a keyword in C. This will result in an error in the output.
C++
#include <stdio.h>
int main() {
// used keyword as an identifier
int const = 90;
return 0;
}
Output
​./Solution.c: In function 'main':
./Solution.c:5:14: error: expected identifier or '(' before '=' token
int const = 90;
^
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read