Identifiers in C Language
Names are represented in a C program by C identifiers, including those for variables, functions, arrays, structures, unions, labels, etc. Any letter, including capital and lowercase letters, underscores, and digits, can be used to create an identification, but the first letter must be either an alphabet or an underscore. If an identifier is not used in an external linkage, it is considered to be internal. If an identification is used in an external connection, it is termed as an external identifier.
It is feasible to express an identifier as a group of alphanumeric letters, each of which is used to represent a different programming element, such as a variable, function, array, structure, union, label, etc., and which either starts with an alphabetic character or an underscore. The identifiers are represented by ten integer digits (0–9), an underscore character, and 52 alphabetical letters (both capital and lowercase). The identifiers are delimited by 63 alphanumeric characters in total.
Rules for Constructing C Identifiers:
For creating proper identifiers in C, use these guidelines and conventions:
- Valid Characters: Any letter, including uppercase, lowercase, numerals, and underscores, can be used as an identifier. However, it must start with a letter or an underscore.
- Length: The first 31 characters of an identifier—which can be any length—are the most important. Identifiers up to this length are ensured to be distinct by the C standard.
- Case Sensitivity: Case sensitivity in characters Lowercase and uppercase letters are handled differently in C. For instance, "sample" and "Sample" are different Identifiers.
- Reserved Terms: You are forbidden to use reserved terms as identifiers. The syntax and grammatical structure of the C language are defined by reserved terms, which have specific meanings. In the C programming language, reserved words comprise "int," "if," "while," and "return."
- Meaningful Names: In order to improve the accessibility and maintainability of your code, it is best practice to use significant and evocative names for your identifiers. You may, for instance, refer to a variable's function by using "numStudents" rather than a name like "x".
Here are some examples of valid identifiers in C:
int age;
float average_grade;
char first_name;
int num_students;
void print_hello();
And here are some examples of invalid identifiers:
int 123abc; // Starts with a digit
float average-grade; // Contains a hyphen
char while; // Uses a reserved word as an identifier
int first name; // Contains a space
Types of Identifiers:
Based on their use and scope inside a program, identifiers in the C programming language come in many forms. Here are some examples of typical identifier types:
- Variable Identifiers: Identifiers for variables are used to give names to the memory locations where data is stored during program execution. In C programs, variables are declared and referenced using variable IDs. For instance:
int age;
float salary;
- Function Identifiers: Function Identifiers are used to give names to functions, which are specialized chunks of code. When defining, calling, and declaring functions, function identifiers are utilized. For instance:
void printMessage();
int addNumbers(int a, int b);
- Type Identifiers: Type identifiers are used with the typedef keyword to name user-defined data types. They enable the naming of brand-new data types for already-existing ones, which can enhance the modularity and readability of the code. For instance:
typedef int Number;
Number x;
- Structure/Union Identifiers: Using the struct and union keywords, respectively, structure and union identifiers are used to define user-defined data structures. These names are used to declare variables with the specified union or structure type. For instance:
struct Person {
char name[50];
int age;
};
union Data {
int x;
float y;
};
- Enumeration Identifiers: When using the enum keyword to declare named constants within an enumeration, enumeration Identifiers are utilized. A group of named values are represented by these identifiers. For instance:
enum Colours {
BLACK,
GREEN,
PINK,
BLUE,
WHITE,
YELLOW,
RED
};
- Label Identifiers: To indicate a target place within the program, use label identifiers along with the goto statement. By handing off control to a particular labelled statement, they are used to regulate the order in which a program is executed. For instance:
start:
printf("Start of the program\n");
goto end;
end:
printf("End of the program\n");
In C programming, these are some of the typical identifier types. Each sort of identifier has a distinct function inside the program structure and aids in efficient data and control flow organization and manipulation.