C Identifiers Naming Rules And Examples

Syntax

It has the following syntax:

Example

// Variable declaration

float temp = 36.5;  //identifier

// Function definition

void display() 

{

}
  • temp: The temp identifier represents a float-type variable.
  • display: It represents a function that is identified by its display value.

Identifiers play a crucial role in distinguishing and retrieving various elements of a program. Each identifier must be distinct within its specific scope and adhere to established C naming guidelines to enhance code clarity and avoid potential clashes.

Simple C Identifier Example

Let's consider an example to illustrate the concept of Identifiers in the C programming language.

Example

Example

#include <stdio.h>

struct Student  // Structure definition with an identifier.

{

    char name[60]; 

    int roll_no; 

};

void display(struct Student s) {

    printf("The Student Name is: %s\n", s.name);

    printf("The Student Roll Number is: %d\n", s.roll_no);

}

int main()   //main function

{ 

    struct Student stu;  // Variable identifiers

    

    snprintf(stu.name, sizeof(stu.name), "Alice"); 

    stu.roll_no = 101;

        display(stu); 

    return 0;

}

Output:

Output

The Student Name is: Alice

The Student Roll Number is: 101

Explanation:

In this instance, a Student construct is defined with name and roll_no as attributes. Subsequently, a display method is implemented to showcase the student's information.

Rules for Naming Identifiers in C

In C programming, identifiers are names in the program, such as variables, functions, arrays, structures, and other user-defined elements. If we want to create identifiers, we should follow certain rules to ensure that the identifiers are valid and meaningful. These standards are essential to ensure compatibility, prevent errors, and write readable code. Some of the main rules are as follows:

  • The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the alphabetic letters (A-Z) and (a-z), digit (0-9), or underscore (_).
  • Identifiers should not start with any numerical digit.
  • Identifiers are case-sensitive.
  • Commas or blank spaces cannot be specified within an identifier.
  • Keywords cannot be represented as an identifier.
  • The length of the identifiers should not be more than 31 characters.
  • Identifiers cannot include special characters like @, #, $,%, ^, &, *, or spaces. Only digits, letters, and underscores are allowed.
  • Valid and Invalid Identifiers in C

A proper C identifier needs to adhere to certain criteria in order to be deemed legitimate. An acceptable identifier commences with a letter (A-Z, a-z) or an underscore (_) and can subsequently be succeeded by another letter, number (0-9), or underscore. It is imperative that it does not encompass any special characters or spaces, and it must also not coincide with any reserved keywords.

Valid Identifiers

These follow all naming conventions:

  • total
  • average
  • _(Underscore)
  • sum_1

These identifiers are correctly structured using permissible characters, start appropriately, and do not clash with keywords in the C language.

Invalid Identifiers

Several examples of invalid identifiers in the C programming language include:

  • 2sum
  • char
  • Naming conventions in C

C programming does not impose intricate rules for naming identifiers. Typically, adhering to recognized naming conventions enhances the readability, maintainability, and clarity of the code. These conventions are derived from community norms and simplify the process of conveying the intent behind each identifier.

For Variables

  • In C, variable names should be in camelCase format (such as totalMarks, studentAge, etc.).
  • Start the variable names with a lowercase letter.
  • Select descriptive and meaningful names that represent the variable's purpose.
  • Use UPPERSNAKECASE when defining constants (such as MAXLENGTH, PIVALUE, etc.).

C Naming Convention Example for Variables

Let's consider a scenario to demonstrate the usage of variables within naming conventions in the C programming language.

Example

Example

#include <stdio.h>

int main()  //main function

{

    // Declaring a variable with the identifier 'a'.

    int a;

    // Assigning a value to the variable using its name.

    a = 25;

    // Accessing the variable using its identifier.

    printf("The age is: %d\n", a);

    return 0;

}

Output:

Output

The age is: 25

Explanation:

In this instance, a numerical variable named "a" has been initialized with the integer value of 25, serving as its unique label. Subsequently, the content of variable a is retrieved and displayed by referencing the variable's identifier.

For Functions

  • In C programming, use camelCase for function names (such as calculateAverage and displayResult.
  • Function names should be verbs or verb phrases that characterize the function's behavior.
  • Keep function names concise but clear.

C Naming Convention Example for Functions

Let's consider an example to demonstrate the role of naming conventions in C programming.

Example

Example

#include <stdio.h>

// Function definition with a user-defined identifier 'multiply'.

int multiply(int a, int b) 

{

    return a * b;

}

int main()

{

    // Calling the function using its identifier.

    int ans = multiply(12, 30);

    printf("The product of two numbers is: %d\n", ans);

    return 0;

}

Output:

Output

The product of two numbers is: 360

Explanation:

In this illustration, a function named multiply is established to calculate the multiplication of two numerical values, utilizing 'multiply' as its name. Subsequently, the main function invokes multiply with the numbers 12 and 30, stores the outcome in ans, and proceeds to display it.

For Structures

  • In C programming, use PascalCase, which helps to capitalize the initial letter of each word (such as Employee and StudentRecord).
  • Structure names should be nouns or noun phrases describing the type of entity.

C Naming Convention Example for Functions:

Let's consider an example to demonstrate the conventions used for naming structures in the C programming language.

Example

Example

#include <stdio.h>

struct Student   // Defining a structure with the identifier 'Student'.

{

    char name[50];

    int roll_no;

};

int main()   //main function

{

    // Creating a variable of type 'Student' with identifier 'p1'.

    struct Student s1;

    // Assigning values to structure members

    s1.roll_no = 30;

    snprintf(s1.name, sizeof(s1.name), "John");

    // Accessing values using the structure variable identifier.

    printf("The name of the student is: %s\n", s1.name);

    printf("The age of the student is: %d\n", s1.roll_no);

    return 0;

}

Output:

Output

The name of the student is: John

The age of the student is: 30

Explanation:

In this instance, a structure named Student is defined with two components: name and roll_no. An instance variable named s1 is then instantiated from this structure. Subsequently, the values for the components of s1 are initialized and displayed by referencing the structure data through the identifier s1.

Types of Identifiers in C

In the C programming language, identifiers can be divided into two types based on their scope and linkage: internal and external identifiers.

These unique names play a crucial role in developing code that is organized, easy to maintain, and scalable, particularly in extensive software projects.

Internal Identifiers

If the symbol is not utilized for external linkage, it is referred to as an internal symbol. Internal symbols typically pertain to local variables. These designations are restricted in visibility to the specific file or function where they are defined. Symbols play a crucial role in managing both data and operations within a particular module or function.

External Identifiers

If the token is employed in external linkage, it is referred to as an external token. These external tokens may encompass function names or global variables. They are essentially functions, variables, or constants that are accessible across multiple files within a software application.

These symbols are valuable for extensive programs containing multiple source files or shared libraries. External linkage allows them to be accessed beyond the specific file in which they are declared.

Using Keywords as Identifiers in C

In the realm of C programming, keywords are predefined terms with distinct significance within the language's syntax. These terms are off-limits for naming variables, functions, or any custom identifiers. Any endeavor to assign them to such elements results in a compilation error.

C Example using Keywords as Identifiers

Example

Example

#include <stdio.h>

int main()

{

    // Trying to use 'return' (a keyword) as an identifier name.

    int return = 1000;

    printf("%d\n", return);

    return 0;

}

Output:

Conclusion

In the realm of C programming, identifiers play a crucial role as they are primarily utilized for naming variables, functions, arrays, labels, structures, and other custom-defined entities. To employ a legitimate identifier, it is imperative to adhere to certain precise guidelines, including commencing with a letter (A-Z or a-z) or underscore (_) and avoiding reserved keywords.

Input Required

This code uses input(). Please provide values below: