BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Structure Declarations and Initializations in C | Unit 4 | BKNMU BCA

by BCA School •

BKNMU Junagadh  |  BCA Sem 1  |  Unit 4



Structure Declarations and Initializations in C Programming

Complete Guide to Tagged & Anonymous Declarations, Compile-Time Initializations, Designated Initializers, and Assignment Rules

BKNMU Junagadh BCA Sem 1 Unit 4 struct Declaration struct Initialization Designated Initializers
In simple words: Creating a structure involves two distinct phases: defining the structural template (which reserves zero bytes of memory) and declaring variables based on that template (which physically allocates memory in RAM). Furthermore, assigning starting values to these members can be executed at compile time or at runtime. C supports several approaches for Structure Declarations and Initializations. This guide breaks down each approach systematically for BKNMU BCA Semester 1 (Unit 4).
📖 Structural Approaches to Declaration & Initialization

Variable creation and data populating are divided into four fundamental techniques:

Separate Declaration

1. Tagged Type Instantiation

Defining the struct TagName blueprint first, then instantiating variables later in main() using struct TagName var1;.

Combined Declaration

2. Suffix Variable Instantiation

Declaring variables directly along with the template definition between the closing brace and the terminating semicolon: } v1, v2;.

Sequential Initializer

3. Positional Initialization

Populating members using curly braces in the strict order of their definition: {101, "Amit", 89.5}. Missing values default to 0.

Designated Initializer

4. Field-Name Initialization

Assigning values directly by member names regardless of sequence: {.marks = 92.0, .id = 102} (C99 standard syntax).

1️⃣ Master Table: Structure Declaration & Initialization Styles

A complete architectural reference of syntax patterns, memory outcomes, and usage behaviors:

Pattern Name Syntax Example Description & Memory Action
Separate Declaration struct Book b1, b2; Instantiates variables in local or global scope after the template is created. Holds garbage values initially.
Combined Declaration struct Book { ... } b1, b2; Defines the blueprint and reserves memory for variables simultaneously in one unified statement.
Anonymous Declaration struct { int x, y; } point1; Creates a structure without a tag name. No other variables of this type can be created later.
Positional Initialization struct Book b1 = {1, "C Prog", 250.0}; Maps elements sequentially to structure fields in the exact order declared inside the template.
Designated Initialization struct Book b1 = {.price = 250.0, .id = 1}; Explicitly binds values by field names using dot prefixes. Available in modern C99 compilers.
ℹ️ Structure Copying Rules (Direct Assignment): Unlike arrays, where arr2 = arr1; is illegal, C allows direct copying between two structure variables of the same type: b2 = b1; This performs a bit-by-bit shallow copy of every member from b1 into b2.
💻 Practical Code Example: Declarations & Initializations in Action

The following program demonstrates positional initialization, partial initialization, designated initialization, and direct assignment copying:

#include <stdio.h>

// 1. Structure Template Definition
struct Employee {
    int emp_id;
    char name[25];
    float salary;
};

int main() {
    // Method A: Positional Initialization (Strict Order)
    struct Employee e1 = {101, "Rahul Dave", 45000.50};

    // Method B: Partial Initialization (Remaining fields default to 0/empty)
    struct Employee e2 = {102, "Pooja Joshi"}; 

    // Method C: Designated Initializer (Order does not matter)
    struct Employee e3 = {.salary = 52000.00, .emp_id = 103, .name = "Karan Patel"};

    // Method D: Uninitialized + Direct Assignment (Structure Copy)
    struct Employee e4;
    e4 = e1; // Copies all members of e1 directly into e4

    // Output all records
    printf("--- Employee 1 (Full Positional) ---\n");
    printf("ID: %d | Name: %s | Salary: %.2f\n\n", e1.emp_id, e1.name, e1.salary);

    printf("--- Employee 2 (Partial Init: Salary Defaults to 0) ---\n");
    printf("ID: %d | Name: %s | Salary: %.2f\n\n", e2.emp_id, e2.name, e2.salary);

    printf("--- Employee 3 (Designated Init) ---\n");
    printf("ID: %d | Name: %s | Salary: %.2f\n\n", e3.emp_id, e3.name, e3.salary);

    printf("--- Employee 4 (Direct Assignment Copy from E1) ---\n");
    printf("ID: %d | Name: %s | Salary: %.2f\n", e4.emp_id, e4.name, e4.salary);

    return 0;
}
🔄 Step-by-Step Flow: Memory Initialization Lifecycle
1

Template Registration

The compiler registers the member offset layout of struct Employee. Physical RAM consumption remains 0 bytes.

2

Variable Instantiation & Allocation

Declarations like struct Employee e1; allocate contiguous memory blocks according to member types plus alignment padding.

3

Compile-Time Data Mapping

Values provided in curly braces are written into respective member offsets. Unfilled slots automatically reset to zero.

💡 Critical Exam Insight: Cannot Initialize Members Inside Template: You cannot initialize variables inside the structure blueprint definition:
struct Student { int roll = 10; }; // COMPILE-TIME ERROR!
The template only specifies the layout, not the data. Initialization can only take place when declaring a structure variable.
⚠️ Structure Comparison Limitation: Although direct assignment (e2 = e1;) is completely valid, you cannot compare two structures directly using relational operators:
if (e1 == e2) // SYNTAX ERROR!
To verify equality, you must compare individual members one by one: if (e1.emp_id == e2.emp_id && ...).
📝 Quick Revision — Key Exam Points
  • Template vs Variable: Template defines schema (0 bytes); variable allocates physical memory.
  • No In-Declaration Init: Members cannot be given values inside the struct { ... }; blueprint.
  • Positional Init: Values must match member declaration order in curly braces { ... }.
  • Designated Init: Uses dot prefix {.field = value} to initialize independent of order (C99).
  • Assignment vs Comparison: e2 = e1 is valid; e2 == e1 is illegal in C.