Identifiers and Variables in C Language – BKNMU Junagadh | BCA Sem 1 Notes



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

BKNMU Junagadh NEP 2020 C Identifiers C Variables Memory Scope
In simple words: An Identifier is the user-defined name given to program elements like variables, functions, and arrays. A Variable is a named container in computer memory that holds data values that can change (vary) during program execution.
📖 Identifier vs Variable — What's the Difference?

While often used interchangeably, there is a fundamental conceptual difference:

student_age, calculate_sum()

Identifier (The Name)

A textual label used to identify a variable, function, or custom data structure uniquely.

int age = 20;

Variable (The Memory Container)

A specific block of RAM allocated to store data of a specific type (e.g., integer, float).

0x7ffd5102 (RAM)

Memory Address

The exact physical RAM location where a variable's data is written and read by the CPU.

🎨 Anatomy of a Variable Declaration

Code: int mark = 85;

Data Type: int (2/4 Bytes) Identifier Name: mark Assignment Operator: = Stored Value: 85

The identifier mark refers to a memory location allocated to store the integer value 85.

1️⃣ Master Rules for Naming Identifiers in C

To write valid C code, every identifier must strictly adhere to these rules enforced by the compiler:

Rule No.Compiler Rule DescriptionValid Identifier ExamplesInvalid Identifier Examples
Rule 1Must start with an alphabet (A-Z, a-z) or an underscore (_). Cannot start with a digit.sum, _count, total11total (Starts with a digit)
Rule 2Can only contain letters, digits, and underscores. No special symbols allowed.student_name, val_2student-name, mark$
Rule 3Cannot contain spaces or blank characters.first_name, firstNamefirst name (Contains space)
Rule 4Cannot use any of the 32 reserved C keywords.integer_val, my_forint, for, return
Rule 5C is Case-Sensitive. Uppercase and lowercase names are distinct.Score, SCORE, score are 3 distinct identifiersN/A
ℹ️ Length Limit Tip: ANSI C compilers recognize at least the first 31 characters of an identifier name. Keeping variable names concise yet descriptive improves code readability!
🔍 Classification of Variables by Scope & Lifetime
Local Variables
  • 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!
Global Variables
  • 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
💻 Code Sample — Variable Declaration, Initialization & Scope

Observe how local and global variables are declared and manipulated in C:

#include <stdio.h> // Global Variable (Accessible everywhere) int global_count = 100; int main() { // Local Variable Declaration & Initialization int student_id = 101; // Integer variable float gpa = 3.85; // Real variable char grade = 'A'; // Character variable printf("ID: %d | GPA: %.2f | Grade: %c\n", student_id, gpa, grade); printf("Global Count: %d\n", global_count); return 0; }
💡 Exam Tip: Always distinguish between Variable Declaration (informing compiler about type and name) and Variable Definition/Initialization (allocating memory and assigning an initial value).
🔄 Life Cycle of a Variable in RAM
1

1. Declaration

Informs compiler: int x; reserves memory bytes based on data type.

2

2. Initialization

Assigns initial value: x = 50; writes value to memory address.

3

3. Execution & Modification

Program reads or updates value: x = x + 10; modifies stored data.

4

4. Destruction (Scope End)

Local variable memory is released when function execution completes.

⚠️ Common Bug: Uninitialized local variables contain Garbage Values (random residual memory data). Always initialize variables before reading them in math calculations!
📝 Quick Revision — Key Exam Points
  • 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, and COUNT are 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.

Post a Comment

Thanks for comment.

Previous Post Next Post