Union Basics

Easy Structures & Unions
Solve in Playground →

Problem Statement

Write a C program using a union to store an integer and a float alternately, demonstrating shared memory allocation.

Input Format

An integer value followed by a float value.

Output Format

Print both values when assigned sequentially.

Constraints

-100000 <= IntVal <= 100000, 0.0 <= FloatVal <= 100000.0

Sample Input

10 5.5

Sample Output

Integer: 10
Float: 5.50

Explanation

Demonstrates union member assignment and shared storage behavior.

Starter Code

#include <stdio.h>

union Data {
    int i;
    float f;
};

int main() {
    union Data data;
    int val_i;
    float val_f;
    if (scanf("%d %f", &val_i, &val_f) == 2) {
        // Write your code here
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used