Back to C Programming
2026-07-125 min read

Examples of C Identifiers

Learn Examples of C Identifiers step by step with clear examples and exercises.

Why This Matters

Understanding C identifiers is crucial for writing clean, efficient, and error-free programs. Proper naming conventions not only make the code more readable but also reduce the chances of errors and improve maintainability. In this lesson, we will delve deeper into various examples of C identifiers, common mistakes to avoid, practice questions, and frequently asked questions.

Prerequisites

To fully grasp the concepts covered in this lesson, it is recommended that you have a basic understanding of:

  1. Variables and their data types (int, char, float, etc.)
  2. Basic C syntax, including variables declaration and assignment
  3. The compilation process in C programming
  4. Understanding of keywords and operators in C
  5. Familiarity with the standard input/output functions (printf, scanf)
  6. Knowledge of control structures such as if-else statements and loops
  7. Understanding of pointers and arrays in C
  8. Basic file I/O operations in C

Core Concept

What are Identifiers?

In C programming, identifiers are symbols used to name variables, functions, structures, and other entities. They help make the code more readable and manageable by giving meaningful names to these entities.

Rules for Identifiers

  1. An identifier can contain letters (A-Z, a-z), digits (0-9), underscores (_), at sign (@), and the dollar sign ($). However, the first character cannot be a digit or an underscore.
  2. An identifier cannot exceed 31 characters in length.
  3. Identifiers are case sensitive. For example, myVariable and MyVariable are considered different identifiers.
  4. Certain keywords (like int, float, if, etc.) are reserved and cannot be used as identifiers.
  5. Identifiers should be chosen to clearly represent the purpose of the variable or function they are naming.
  6. It is recommended to follow a consistent naming convention, such as using camelCase (e.g., myVariable) for variables and PascalCase (e.g., MyFunction) for functions.
  7. It's also good practice to use Hungarian notation, which indicates the data type of a variable at the beginning of its name (e.g., szArray for size array or pMyVar for pointer to my variable).

Examples of Valid and Invalid Identifiers

// Valid identifiers
int my_variable;
float Pi;
char firstName_;
int _myIdentifier;
int myVariableLengthyName; // up to 31 characters

// Invalid identifiers (contains keywords, starts with a digit or underscore, exceeds 31 characters)
int 1var; // contains a digit at the start
int ifStatement; // contains a keyword
int longIdentifier; // exceeds 31 characters
int _123_variable; // starts with an underscore followed by a digit

Worked Example

Let's create a simple C program that declares and initializes variables, demonstrating various naming conventions:

#include <stdio.h>

int main() {
int my_integer = 42; // valid identifier with meaningful name
char firstInitial = 'A'; // valid identifier using underscore for clarity
float piValue = 3.14; // valid identifier using a descriptive name
char array[5] = {'H', 'e', 'l', 'l', '\0'}; // valid identifier for an array with Hungarian notation

printf("my_integer: %d\n", my_integer);
printf("firstInitial: %c\n", firstInitial);
printf("piValue: %.2f\n", piValue);
printf("array: %s\n", array);

return 0;
}

Common Mistakes

  1. Using reserved keywords as identifiers: This can lead to compile-time errors, as the compiler treats these names as part of its syntax.
// Incorrect: int ifStatement = 0; // ifStatement is a keyword in C
  1. Naming variables with inconsistent case: Using mixed case for variable names can make code harder to read and maintain.
// Incorrect: int myvariable = 5; // inconsistent case
  1. Using invalid characters in identifiers: This can lead to compile-time errors or confusion when reading the code.
// Incorrect: int 1var = 0; // contains a digit at the start
// Incorrect: int if_statement = 0; // contains an underscore in an invalid position
  1. Not following a consistent naming convention: Using inconsistent or unclear names for variables and functions can make code harder to read, understand, and maintain.
  1. Ignoring Hungarian notation: While not mandatory, using Hungarian notation can help improve the readability of your code by indicating the data type of a variable at its beginning.

Practice Questions

  1. Write a C program that declares and initializes three variables with valid identifiers, then prints their values using printf. Use camelCase, PascalCase, Hungarian notation, and an array.
  2. What is the maximum length of an identifier in C programming?
  3. What happens if you try to use a reserved keyword as an identifier in C?
  4. Why is it important to choose meaningful names for your variables and functions in C?
  5. Explain the difference between camelCase, PascalCase, and Hungarian notation naming conventions, and provide examples of each.
  6. What are some common naming conventions for variable and function names in C programming?
  7. How can using consistent naming conventions help improve the readability and maintainability of your code?
  8. Write a C program that reads user input and stores it in a character array, then prints the reversed version of the string using a loop. Use camelCase, Hungarian notation, and proper indentation.
  9. What are some best practices for writing efficient and readable C code?
  10. How can you use comments effectively in C to make your code more understandable?

FAQ

Q: Can I use spaces in my identifiers?

A: No, you cannot have spaces in identifiers. However, you can use underscores (_) or camelCase notation (e.g., myVariableName) to make your code more readable.

Q: Are C identifiers case sensitive?

A: Yes, C identifiers are case sensitive. For example, myVariable and MyVariable are considered different identifiers.

Q: Can I use special characters other than underscores in my identifiers?

A: While you can use the underscore character (_) and the dollar sign ($) in your identifiers, it's generally discouraged to use other special characters. This helps keep your code clean and readable for others.

Q: Why is it important to follow a consistent naming convention when writing C programs?

A: Consistent naming conventions help make the code more readable, maintainable, and easier to understand for both you and other developers who might work on the same project. It also reduces the likelihood of errors due to misinterpretation or confusion about variable and function names.

Q: What is Hungarian notation?

A: Hungarian notation is a convention used in C programming that indicates the data type of a variable at the beginning of its name (e.g., szArray for size array or pMyVar for pointer to my variable). It helps improve the readability and maintainability of code by making it easier to understand what each variable represents.