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 (
Totalandtotalare different). - Cannot use C keywords like
int,return,floatas 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)
| Type | Scope | Example Use |
|---|---|---|
| Local | Inside functions or blocks | Temporary calculations |
| Global | Declared outside all functions | Shared across functions |
| Static | Retains value between function calls | Counters, flags |
External (extern) | Declared in one file and used in another | Modular 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;
}
