Back to C Programming
2026-07-125 min read

C - Identifiers

Learn C - Identifiers step by step with clear examples and exercises.

Why This Matters

Welcome to this in-depth guide on C identifiers! We'll delve into the world of variables, constants, and other essential identifiers that make up the heart of a C program. This lesson is designed to provide you with practical insights, real-world examples, and common mistakes to help you master C programming like never before.

Why This Matters

Understanding C identifiers is crucial for any aspiring programmer as they form the foundation of data storage in C programs. Identifiers allow us to name variables, constants, functions, and other programming constructs, making our code more readable and maintainable. In interviews, recruiters often test your knowledge of identifiers, so it's essential to have a solid grasp of this topic.

Prerequisites

To follow along with this lesson, you should already be familiar with the basics of C programming, such as data types, operators, and control structures. If you need a refresher on these topics, check out our previous lessons on C Basics and Control Structures in C.

Core Concept

What are Identifiers?

In C programming, identifiers are names given to variables, constants, functions, and other programming constructs. They help make our code more readable and easier to understand by giving meaningful names to different parts of the program.

Rules for Identifiers

  1. An identifier can contain letters (A-Z, a-z), digits (0-9), underscores (_), and the at sign (@). However, the first character cannot be a digit or an underscore.
  2. Identifiers are case-sensitive in C, meaning myVariable and myvariable are considered different identifiers.
  3. Keywords, such as int, char, and if, are reserved words and cannot be used as identifiers.
  4. An identifier should not contain any spaces or special characters except for underscores and the at sign (@).
  5. Identifiers in C can have a maximum length of 31 characters.

Variables and Constants

Variables and constants are two types of identifiers that store data in a program. Variables can hold values that may change during execution, while constants store fixed values that cannot be altered.

Variables

To declare a variable in C, we use the following syntax:

data_type variable_name;

For example:

int age;
char name[20];
float price;

In this example, age, name, and price are variables of type int, char, and float, respectively.

Constants

Constants in C can be of two types: integral constants and floating-point constants. Integral constants can be further divided into character constants and integer constants.

const data_type constant_name = value;

For example:

const int MAX_AGE = 100;
char CONST_CHAR = 'A';
float PI = 3.14;

In this example, MAX_AGE, CONST_CHAR, and PI are constants with values 100, 'A', and 3.14, respectively.

Const Qualifier

The const keyword can be used to declare a variable as a constant, ensuring that its value cannot be changed during the execution of the program.

data_type const variable_name;

For example:

int const MY_CONST = 10;

In this example, MY_CONST is a constant integer with a value of 10.

Worked Example

Let's create a simple program that declares and initializes variables, constants, and uses the const qualifier:

#include <stdio.h>

int main() {
// Variables
int age = 25;
char name[20] = "John Doe";
float salary = 30000.00;

// Constants
const int MAX_AGE = 100;
char CONST_CHAR = 'A';
float PI = 3.14;

// Const Qualifier
const int MY_CONST = 10;

printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Maximum Age: %d\n", MAX_AGE);
printf("Constant Character: %c\n", CONST_CHAR);
printf("PI: %.3f\n", PI);
printf("My Const: %d\n", MY_CONST);

return 0;
}

When you run this program, it will output the following:

Name: John Doe
Age: 25
Salary: 30000.00
Maximum Age: 100
Constant Character: A
PI: 3.140
My Const: 10

Common Mistakes

  1. Forgetting to declare a variable before using it: Always declare your variables at the beginning of the function or block where they are used.
  2. Using invalid identifiers: Make sure your identifiers follow the rules for naming conventions in C.
  3. Mistaking constants for variables: Constants cannot be changed during program execution, while variables can.
  4. Using global variables without understanding their scope and potential issues: Global variables can lead to unintended side effects and hard-to-debug issues if not used carefully.
  5. Not using the const qualifier when appropriate: Using const can help improve code readability and prevent accidental changes to certain values.

Practice Questions

  1. Declare a variable named myNumber of type int and initialize it with the value 42.
  2. Create a constant named MAX_SPEED with the value 60 and another constant named GRAVITY with the value 9.81.
  3. Write a function that takes two integer arguments, adds them together, and returns the result using the const qualifier for both arguments.
  4. Create a program that declares an array of 10 integers and initializes it with values from 1 to 10. Then, calculate and print the sum of all even numbers in the array.

FAQ

Q: Can I use special characters other than underscores and the at sign (@) in my identifiers?

A: No, C only allows underscores and the at sign as special characters in identifiers.

Q: What happens if I declare a variable with the same name as a keyword in C?

A: If you try to declare a variable with the same name as a keyword, the compiler will generate an error, as keywords are reserved words in C and cannot be used as identifiers.

Q: Can I change the value of a constant in my program?

A: No, constants in C have fixed values that cannot be changed during program execution. However, you can use the const qualifier to declare variables that cannot be modified by accident or mistake.