C Tutorial — Learn C Programming
Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.
C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.
Last updated: September 2026
Unions in C
Unions are similar to structures but all members share the same memory location. Only one member can hold a value at any given time, making unions memory-efficient for certain applications.
Union Declaration and Usage
#include <stdio.h>
union Data {
int intValue;
float floatValue;
char charValue;
};
int main() {
union Data data;
// Assign integer value
data.intValue = 42;
printf("Integer value: %d\n", data.intValue);
printf("Size of union: %lu bytes\n", sizeof(data));
// Assign float value (overwrites integer)
data.floatValue = 3.14;
printf("Float value: %.2f\n", data.floatValue);
printf("Integer value now: %d (corrupted)\n", data.intValue);
// Assign character value (overwrites float)
data.charValue = 'A';
printf("Character value: %c\n", data.charValue);
printf("Float value now: %.2f (corrupted)\n", data.floatValue);
return 0;
}Union vs Structure Memory Layout
#include <stdio.h>
struct StructExample {
int intValue;
float floatValue;
char charValue;
};
union UnionExample {
int intValue;
float floatValue;
char charValue;
};
int main() {
struct StructExample s;
union UnionExample u;
printf("Structure size: %lu bytes\n", sizeof(s));
printf("Union size: %lu bytes\n", sizeof(u));
printf("\nStructure member addresses:\n");
printf("intValue: %p\n", &s.intValue);
printf("floatValue: %p\n", &s.floatValue);
printf("charValue: %p\n", &s.charValue);
printf("\nUnion member addresses:\n");
printf("intValue: %p\n", &u.intValue);
printf("floatValue: %p\n", &u.floatValue);
printf("charValue: %p\n", &u.charValue);
return 0;
}Practical Union Example
#include <stdio.h>
enum DataType {
TYPE_INT,
TYPE_FLOAT,
TYPE_CHAR
};
struct TaggedData {
enum DataType type;
union {
int intValue;
float floatValue;
char charValue;
} data;
};
void printData(struct TaggedData *td) {
switch (td->type) {
case TYPE_INT:
printf("Integer: %d\n", td->data.intValue);
break;
case TYPE_FLOAT:
printf("Float: %.2f\n", td->data.floatValue);
break;
case TYPE_CHAR:
printf("Character: %c\n", td->data.charValue);
break;
}
}
int main() {
struct TaggedData data1, data2, data3;
// Store integer
data1.type = TYPE_INT;
data1.data.intValue = 100;
// Store float
data2.type = TYPE_FLOAT;
data2.data.floatValue = 25.75;
// Store character
data3.type = TYPE_CHAR;
data3.data.charValue = 'X';
printf("Tagged union data:\n");
printData(&data1);
printData(&data2);
printData(&data3);
return 0;
}Important Notes:
- Only one member of a union can be used at a time
- Union size equals the size of its largest member
- All members share the same memory address
- Use tagged unions for type safety