C Program to Redeclaration of global variable

In C programming, variable redeclaration refers to declaring a variable more than once in the same scope. The behavior of redeclaration varies between global and local variables, and differs between C and C++. Understanding these differences is crucial for avoiding compilation errors.

Syntax

// Global variable redeclaration
int var;
int var;  // Allowed in C without initialization

// Local variable redeclaration  
int main() {
    int var;
    int var;  // Not allowed in C
}

Global Variable Redeclaration Without Initialization

C allows redeclaration of global variables when they are not initialized −

#include <stdio.h>

int var;
int var;  /* Redeclaration allowed in C */

int main() {
    printf("Var = %d
", var); return 0; }
Var = 0

Global Variable Redeclaration With Initialization

When one declaration includes initialization, C allows this pattern −

#include <stdio.h>

int var;      /* First declaration without initialization */
int var = 10; /* Second declaration with initialization */

int main() {
    printf("Var = %d
", var); return 0; }
Var = 10

Local Variable Redeclaration

C does not allow redeclaration of local variables in the same scope −

#include <stdio.h>

int main() {
    int var;
    int var; /* Error: redeclaration not allowed */
    printf("Var = %d
", var); return 0; }
error: redeclaration of 'var' with no linkage

Comparison Table

Variable Type Redeclaration C Behavior Result
Global Without initialization Allowed Uses default value (0)
Global With initialization Allowed (one init) Uses initialized value
Local Any redeclaration Not allowed Compilation error

Key Points

  • C follows tentative definition rules for global variables − multiple declarations are merged into one definition.
  • Global variables without explicit initialization are automatically initialized to zero.
  • Local variables have block scope and cannot be redeclared in the same scope.
  • Only one initialization is allowed even for global variable redeclarations.

Conclusion

C allows global variable redeclaration due to tentative definitions, but prohibits local variable redeclaration. This flexibility in global scope helps with separate compilation units while maintaining strict rules within functions.

Updated on: 2026-03-15T14:26:27+05:30

854 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements