C Variable Names (Identifiers)

1. What is an Identifier?

  • An identifier is a name given to variables, functions, arrays, or any user-defined item in C.

  • It is used to identify and refer to memory locations where data is stored.

Example:

int age; // 'age' is an identifier
float height; // 'height' is an identifier
char grade; // 'grade' is an identifier

2. Rules for Naming Variables (Identifiers)

  1. Start with a letter or underscore (_):

    int age; // valid
    int _count; // valid
    int 1num; // invalid (cannot start with a number)
  2. Can contain letters, digits, and underscores:

    int age1; // valid
    int _total2; // valid
  3. Cannot be a C keyword (reserved word):

    int int; // invalid
    int return; // invalid
  4. Case-sensitive:

    int age; // different from 'Age'
    int Age; // different from 'AGE'
  5. No special characters or spaces allowed:

    int my-age; // invalid
    int my age; // invalid

3. Best Practices for Variable Names

  1. Use meaningful names:

    int age; // good
    int a; // not descriptive
  2. Use camelCase or snake_case for multi-word names:

    int studentAge; // camelCase
    int student_age; // snake_case
  3. Avoid single letters unless in loops:

    int i; // okay for loop index
  4. Keep names short but readable.


4. Examples of Valid Identifiers

int age;
float height;
char grade;
int _count;
int student1;
float salaryAmount;

5. Examples of Invalid Identifiers

int 1age; // starts with a digit ❌
float my-salary; // contains '-' ❌
char first name; // contains space ❌
int return; // keyword ❌

6. Example Program Using Identifiers

#include <stdio.h>

int main() {
int age = 25;
float height = 5.9;
char grade = 'A';

printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);

return 0;
}

Output:

Age: 25
Height: 5.9
Grade: A

Key Takeaways:

  • Always follow the rules for identifiers.

  • Use meaningful names for clarity.

  • C is case-sensitive, so be consistent.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *