Write a structure in local scope program using C language

A structure in C is a collection of different datatype variables, grouped together under a single name. When a structure is declared inside a function or block, it has local scope and can only be accessed within that specific function or block.

Syntax

struct structure_name {
    datatype member1;
    datatype member2;
    datatype member_n;
};

Local Scope Structure Declaration

When a structure is declared inside a function, it is local to that function and cannot be accessed from outside −

void function() {
    struct local_structure {
        int member1;
        char member2[20];
    };
    struct local_structure var;  // Can only be used inside this function
}

Example: Global vs Local Structure

The following program demonstrates structures with different scopes −

#include <stdio.h>
#include <string.h>

/* Global structure - accessible throughout the program */
struct employee {
    char name[20];
    int age;
    int salary;
    char address[30];
} emp1, emp2;

int calculateManagerSalary() {
    /* Local structure - only accessible within this function */
    struct manager {
        char name[20];
        int age;
        int salary;
        char department[50];
    } mgr;
    
    strcpy(mgr.name, "John Manager");
    mgr.age = 27;
    
    if (mgr.age > 30)
        mgr.salary = 65000;
    else
        mgr.salary = 55000;
    
    printf("Manager %s (Age: %d) salary: %d<br>", mgr.name, mgr.age, mgr.salary);
    return mgr.salary;
}

int main() {
    /* Using global structure variables */
    strcpy(emp1.name, "Alice");
    strcpy(emp1.address, "New York");
    emp1.salary = 45000;
    emp1.age = 28;
    
    strcpy(emp2.name, "Bob");
    strcpy(emp2.address, "California");
    emp2.salary = 50000;
    emp2.age = 32;
    
    printf("Employee 1: %s, Address: %s, Salary: %d<br>", emp1.name, emp1.address, emp1.salary);
    printf("Employee 2: %s, Address: %s, Salary: %d<br>", emp2.name, emp2.address, emp2.salary);
    
    /* Calling function with local structure */
    int managerSal = calculateManagerSalary();
    printf("Returned manager salary: %d<br>", managerSal);
    
    return 0;
}
Employee 1: Alice, Address: New York, Salary: 45000
Employee 2: Bob, Address: California, Salary: 50000
Manager John Manager (Age: 27) salary: 55000
Returned manager salary: 55000

Key Points

  • Global structures are declared outside all functions and can be accessed from anywhere in the program.
  • Local structures are declared inside functions and have limited scope to that function only.
  • Structure variables can be initialized using assignment or strcpy() for strings.
  • Local structures are destroyed when the function ends, but their values can be returned.

Conclusion

Local scope structures in C provide encapsulation and memory efficiency by limiting access to specific functions. They are useful for temporary data storage and maintaining code modularity.

Updated on: 2026-03-15T13:10:00+05:30

508 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements