Define Identifier in C
In the C language of programming, operations, parameters, arrays of elements, frameworks, and other user-defined objects are all identified by labels called identifiers. The identifier is a group of characters (from uppercase to lowercase), numerals as well and underscores (_) that adhere to the specifications. Identifiers can be utilized to give program things meaningful names, enhancing the comprehension and accessibility of the code.
Example:
#include <stdio.h>
int main() {
// Declaring and initializing variables
int age = 25;
float weight = 65.5;
char initial = 'J';
char name[] = "John";
// Printing the values of the variables
printf("Name: %s\n", name);
printf("Initial: %c\n", initial);
printf("Age: %d\n", age);
printf("Weight: %.2f\n", weight);
return 0;
}
Output:
![Define Identifier in C](https://static.tutorialandexample.com/c/define-identifier-in-c1.png)
Rules for Creating Identifiers
The main guidelines for creating identifiers in C are as follows:
- The first character must be a capital or lowercase letter or underscore (_).
- After the initial character, any combination of letters, numbers, and underscores may be used.
- Because identifiers are case-sensitive, letters in uppercase and lowercase are treated differently. For instance, the terms "variable" and "Variable" would be regarded as two different IDs.
- C keywords (reserved terms) cannot be used as identifiers. The language uses keywords with specific meanings and for predetermined goals.
- Identifiers shouldn't have a digit at the beginning.
- Although there isn't a legal restriction on identifier length, most programmers have a reasonable one.
Valid identifier examples
- myName
- count
- _info
- MAX_SIZE
- function_name
Invalid identifier examples
- 456variable(starts with a digit)
- If(a C keyword)
- my-variable(contains a hyphen, which is not allowed)
- while-loop(contains an invalid character)
Naming Conventions
Although there are no specific rules for identifying identifiers in C, developers frequently adhere to the essential standards to make their code easier to read and consistent:
1.Camel Case: The technique calls for capitalizing every first character of each word that follows the identifier's initial lowercase letter. For instance, use myVariableName and calculate the total amount.
2. Snake Case: Underscores are used to separate words in this style; all characters are lowercase. Examples include user_id and max_value.
3. Pascal Case: Similarly to Camel Case, the identifier's initial letter is also capitalized. Primarily used to name types and functions. Examples are MyStructType and CalculateSum.
4. All Uppercase: used to give forever names. All letters are capitalized, and underscores are used to divide words. For instance, PI and MAX_LENGTH.
Scope and Lifetime
Identifiers are also connected to scope and lifetime:
1.Scope: The domain of a program where an identifier can be utilized and identified. A variable that is defined inside of a function, for illustration, has a scope specific to that method.
2. Lifetime: the period of time that a code is present and helpful. For instance, global variables persist throughout the execution of the program. In contrast, local variables within an operation have a duration connected to the execution of the function.
Examples of Identifiers
1.Variables: Identifiers that serve as value storage. Examples are double temperature and int age.
2. Functions: Identifiers that are used to call or define functions. void printMessage(), int addNumbers(int a, int b), as an illustration.
3. Arrays: array-defining identifiers. Examples include char name[50] and int scores[5].
4. Structures: Structures are defined by identifiers. An illustration is struct Person {char name[50], int age }.
5. Pointers: the names of the addresses in memory. For instance, double *data and int *ptr.
6. Enums: Identifiers that define the constants used in enumeration. For instance, enter "enum Day" as { MON, TUES, WED, THUR, FRI, SAT, SUN }.
Identifiers and Linkage
Identifiers in C can have a variety of linking types:
1.External Linkage: External linkage exists for identifiers specified outside functions and at the global scope. They can be seen in several source files. Global variables, as well as functions, are some examples.
2. Internal Linkage: The static keyword creates internal links for identifiers. They can only be accessed from the file in which they are defined. By doing so, naming conflicts between several files may be avoided.
3.No Linkage: There is no linkage between block-scope identifiers and variables defined inside a block (like inside a function). They are restricted to the region and are only reachable from places.
Identifiers and Storage Duration
Identifiers in C can also have various storage times:
1.Atomic Storage Duration: Block-scope variables have a default storage time of this amount of time. When the block holding the statement is entered, variables with dynamic storage duration are created, and while the block is exited, they are removed.
2. Static Storage Duration: Static storage duration applies to variables containing the static word (outside of functions). They receive a single initialization and maintain that value throughout the program.
3. Thread Storage Duration: Thread storage duration applies to parameters defined with the _Thread_local keyword. These variables are instances that are unique to each thread.
4. Dynamic Storage Duration: Dynamic storage duration applies to memory allocated using operations like malloc and calloc. The programmer explicitly allocates and deallocates them.