BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Identifiers and Variables in C Language — BKNMU Junagadh
Naming Conventions, Memory Allocation, Declaration, Initialization & Scope Rules
While often used interchangeably, there is a fundamental conceptual difference:
Identifier (The Name)
A textual label used to identify a variable, function, or custom data structure uniquely.
Variable (The Memory Container)
A specific block of RAM allocated to store data of a specific type (e.g., integer, float).
Memory Address
The exact physical RAM location where a variable's data is written and read by the CPU.
Code: int mark = 85;
The identifier mark refers to a memory location allocated to store the integer value 85.
To write valid C code, every identifier must strictly adhere to these rules enforced by the compiler:
| Rule No. | Compiler Rule Description | Valid Identifier Examples | Invalid Identifier Examples |
|---|---|---|---|
Rule 1 | Must start with an alphabet (A-Z, a-z) or an underscore (_). Cannot start with a digit. | sum, _count, total1 | 1total (Starts with a digit) |
Rule 2 | Can only contain letters, digits, and underscores. No special symbols allowed. | student_name, val_2 | student-name, mark$ |
Rule 3 | Cannot contain spaces or blank characters. | first_name, firstName | first name (Contains space) |
Rule 4 | Cannot use any of the 32 reserved C keywords. | integer_val, my_for | int, for, return |
Rule 5 | C is Case-Sensitive. Uppercase and lowercase names are distinct. | Score, SCORE, score are 3 distinct identifiers | N/A |
- Declared inside a function or code block (`{}`)
- Accessible only within that specific block
- Created when block enters; destroyed when block exits
- Contains garbage value if not initialized!
- Declared outside all functions (top of file)
- Accessible by any function throughout the program
- Exists for the entire lifetime of the program
- Automatically initialized to `0` by default
Observe how local and global variables are declared and manipulated in C:
1. Declaration
Informs compiler: int x; reserves memory bytes based on data type.
2. Initialization
Assigns initial value: x = 50; writes value to memory address.
3. Execution & Modification
Program reads or updates value: x = x + 10; modifies stored data.
4. Destruction (Scope End)
Local variable memory is released when function execution completes.
- Identifier: User-defined name for variables, functions, arrays, and structures.
- Variable: Named memory location used to store dynamic data values.
- First Character Rule: Identifiers must start with a letter (A-Z, a-z) or underscore (
_). - Case Sensitivity:
count,Count, andCOUNTare three separate variables. - No Keywords: Reserved C keywords cannot be used as identifier names.
- Local vs Global: Local variables exist inside functions; global variables exist across the program.
- Garbage Value: Uninitialized local variables hold unpredictable leftover memory values.