Notes – Identifiers and Variables in C

In C programming, identifiers and variables are fundamental building blocks. Understanding them is key to writing clean and functional code.


What is an Identifier?

  • An identifier is the name used to identify variables, functions, arrays, structures, etc.
  • It is user-defined (you decide the name).
  • Example: total, count, main, sum1

Rules for Naming Identifiers

  • Can include letters (Aโ€“Z, aโ€“z), digits (0โ€“9), and underscores (_).
  • Must start with a letter or underscore, not a digit.
  • No spaces or special characters allowed.
  • Case-sensitive (Total and total are different).
  • Cannot use C keywords like int, return, float as names.

What is a Variable?

  • A variable is a named memory location used to store data.
  • The value of a variable can be changed during program execution.
  • Variables are a type of identifier, specifically used to hold data.

Declaring Variables

Before using a variable, you must declare it with a data type.

int age;          // declares an integer variable
float weight; // declares a floating-point variable
char grade; // declares a character variable

You can also assign a value during declaration:

int age = 25;

Types of Variables (by Scope)


TypeScopeExample Use
LocalInside functions or blocksTemporary calculations
GlobalDeclared outside all functionsShared across functions
StaticRetains value between function callsCounters, flags
External (extern)Declared in one file and used in anotherModular programs

Example

#include <stdio.h>

int main() {
int number = 10; // variable declaration + initialization
float price = 25.75;

printf("Number: %d\n", number);
printf("Price: %.2f\n", price);

return 0;
}