Structures in C
Structures in C
#include <stdio.h>
#include <string.h>
// Define structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare structure variable
struct Person p1;
// Assign values
p1.age = 25;
p1.height = 5.9;
strcpy(p1.name, "Alice");
// Access values
printf("Name: %s\n", p1.name);
printf("Age: %d\n", p1.age);
printf("Height: %.1f\n", p1.height);
return 0;
}
Output:
Name: Alice
Age: 25
Height: 5.9
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p = {10, 20};
struct Point *ptr = &p;
Output:
x: 10, y: 20
x: 10, y: 20
3. Array of Structures
#include <stdio.h>
struct Student {
int id;
char name[50];
};
int main() {
struct Student s[2] = {
{1, "John"},
{2, "Emma"}
};
4. Nested Structures
#include <stdio.h>
#include <string.h>
struct Address {
char city[20];
int pincode;
};
struct Employee {
char name[50];
struct Address addr;
};
int main() {
struct Employee emp;
strcpy(emp.name, "Alice");
strcpy(emp.addr.city, "Mumbai");
emp.addr.pincode = 400001;
Output:
Name: Alice
City: Mumbai
Pincode: 400001
5. Structure Padding
Structure padding is the addition of empty bytes to align data members according
to the system architecture.
#include <stdio.h>
struct A {
char c;
int i;
};
int main() {
printf("Size of structure A: %lu\n", sizeof(struct
A));
return 0;
}
Size of structure A: 8
Unions in C
A union is like a structure but uses shared memory for all members.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.1f\n", data.f); // Overwrites i
strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str); // Overwrites f
return 0;
}
Output:
data.i: 10
data.f: 220.5
data.str: Hello
Enumerations in C
#include <stdio.h>
int main() {
enum Color c;
c = GREEN;
Output:
Color code: 1
#include <stdio.h>
int main() {
enum Days today = FRI;
if (today == FRI) {
printf("Weekend is near!\n");
}
return 0;
}
Output:
Weekend is near!
#include <stdio.h>
enum Level {LOW = 10, MEDIUM = 20, HIGH = 30};
int main() {
enum Level l = MEDIUM;
printf("Level value: %d\n", l);
return 0;
}
Output:
Level value: 20
4. Importance of Enums
Size of structure A: 8
Unions in C
Note: Each assignment overwrites the previous one because all members share the
same memory.
Enumerations in C
Structure Padding
Padding is extra bytes the compiler adds between members to align them for faster
access (depending on CPU architecture).
#include <stdio.h>
struct Padded {
char a; // 1 byte
// 3 bytes padding
int b; // 4 bytes
};
int main() {
printf("Size of struct Padded: %lu\n",
sizeof(struct Padded)); // Output: 8 on most 32-bit or
64-bit systems
return 0;
}
Why? After char a, 3 empty bytes are added to align int b to a 4-byte
boundary.
Structure Packing
#include <stdio.h>
#pragma pack(1)
struct Packed {
char a; // 1 byte
int b; // 4 bytes
};
int main() {
printf("Size of struct Packed: %lu\n",
sizeof(struct Packed)); // Output: 5
return 0;
}
struct Example {
char a; // 1 byte
// 3 bytes padding
int b; // 4 bytes
char c; // 1 byte
// 3 bytes padding
};
union U {
int a; // 4 bytes
float b; // 4 bytes
char str[10]; // 10 bytes
};
int main() {
printf("Size of union: %lu\n", sizeof(union U)); //
Output: 10
return 0;
}
struct Inner {
int x;
char y;
};
struct Outer {
char z;
struct Inner i;
};
int main() {
printf("Size: %lu\n", sizeof(struct Outer)); //
Padding depends on platform
return 0;
}
🔁 Struct Inside Union
union Mixed {
int i;
struct {
char c;
float f;
} s;
};
int main() {
union Mixed m;
m.s.c = 'X';
m.s.f = 1.5;
printf("Char: %c, Float: %.2f\n", m.s.c, m.s.f);
return 0;
}
Total union size = size of the largest member (struct inside here)
int main() {
struct Holder h;
h.type = 'i';
h.value.i = 100;
printf("Value: %d\n", h.value.i);
return 0;
}
union ID {
int aadhaar;
char passport[10];
};
struct Person {
char name[50];
enum Gender gender;
struct Date dob;
union ID id;
};
int main() {
struct Person p;
strcpy(p.name, "Alice");
p.gender = FEMALE;
p.dob.day = 5;
p.dob.month = 3;
p.dob.year = 2000;
p.id.aadhaar = 123456789;