Variables in C

A Variable in C programming is a named memory location used to store, manipulate, and retrieve data dynamically during program execution. Variables act as named containers whose values can be altered during execution. Each variable is characterized by a specific data type, which specifies the amount of memory allocated, the range of valid values, and the set of operations that can be performed on it.

Variable Declaration, Definition, and Initialization

It is crucial for beginners to understand the technical distinction between these three stages:

 Declaration: Informs the compiler about the variable name and its data type without necessarily allocating storage (e.g., extern int total;).

 Definition: Informs the compiler to reserve physical memory for the variable (e.g., int count; allocates 4 bytes in RAM).

 Initialization: Assigns an initial value to the allocated memory location (e.g., int count = 0;).

Syntax and Working Example


data_type variable_name = initial_value;
							

#include <stdio.h>

int main()
{
    int student_age = 20;      /* Integer variable */
    float gpa = 8.75f;         /* Floating point variable */
    char grade = 'A';          /* Character variable */

    printf("Student Profile:\n");
    printf("Age   : %d years\n", student_age);
    printf("GPA   : %.2f\n", gpa);
    printf("Grade : %c\n", grade);

    return 0;
}
							

Mandatory Rules for Naming Variables in C

 Variable names can only consist of letters (a-z, A-Z), digits (0-9), and the underscore (_).

 The first character must always be a letter or an underscore. It cannot begin with a number (e.g., 1num is invalid; num1 is valid).

 Variable names are strictly case-sensitive. Hence total, Total, and TOTAL represent three completely separate variables.

 No whitespace, punctuation marks, or special characters (such as $, @, #, -) are permitted.

 Keywords (like int, float, while, return) cannot be used as variable names.

Classification of Variables by Scope & Lifetime

Variable Type

Scope (Where Accessible)

Lifetime (How Long Stored)

Default Value

Local Variable

Inside the defining function / block { }

Until the block execution ends

Garbage (Indeterminate)

Global Variable

Accessible by all functions in the file

Throughout the entire program run

Zero (0)

Static Variable

Local to the function where declared

Persists across multiple function calls

Zero (0)

Constant (const)

Depends on location (local or global)

Read-only; cannot be reassigned

Must be initialized

Static Variables vs Local Variables (BCA Exam Special)

Notice how a static variable retains its previous value across successive function calls:


#include <stdio.h>

void counterFunction() {
    int normal_var = 1;       /* Re-initialized on every call */
    static int static_var = 1; /* Initialized only once */

    printf("normal_var = %d | static_var = %d\n", normal_var, static_var);

    normal_var++;
    static_var++;
}

int main() {
    counterFunction();  /* Prints: normal = 1 | static = 1 */
    counterFunction();  /* Prints: normal = 1 | static = 2 */
    counterFunction();  /* Prints: normal = 1 | static = 3 */
    return 0;
}
							

Practice Session for Beginner Students

Test your understanding of variables, scope rules, and naming conventions.

Part 1: Spot the Bug & Predict the Output

Problem 1: What is the output of the following C program?


#include <stdio.h>
int x = 50;  /* Global variable */
int main() {
    int x = 20;  /* Local variable */
    printf("%d", x);
    return 0;
}
							

Answer & Explanation: Output is 20. In C, a local variable always “shadows” (takes precedence over) a global variable with the same name within its local scope.

Part 2: Multiple Choice Questions (BCA Exam Preparation)

Q1. Which of the following is an invalid variable name in C?
A) _total_score
B) student1
C) float
D) sum_of_numbers
Correct Answer: C) float (Reserved keywords cannot be used as variable names).

Q2. What is the default initial value of an uninitialized local integer variable in C?
A) 0
B) 1
C) Garbage value
D) NULL
Correct Answer: C) Garbage value.

Q3. What qualifier is used to create a variable whose value cannot be modified after initialization?
A) static
B) const
C) volatile
D) extern
Correct Answer: B) const.

Part 3: Hands-on Lab Assignments for Beginners

Assignment 1: Write a C program to swap two variables without using a third temporary variable.
Logic Hint: Use arithmetic operators: a = a + b; b = a - b; a = a - b; or bitwise XOR: a ^= b; b ^= a; a ^= b;.

Part 4: Frequently Asked University Viva Questions

1. What is variable shadowing in C?
Answer: When a local variable inside an inner block shares the exact same identifier as an outer or global variable, the inner variable hides (shadows) the outer variable within that block.

2. What is an lvalue and rvalue?
Answer: An lvalue refers to an object that has an identifiable memory location (e.g. variable name on the left of assignment: x = ...). An rvalue refers to the data value assigned to that location (e.g. ... = 10;).

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!