Union Extract Bytes from a 32-bit Value

Code

#include <stdio.h>
#include <stdint.h>

typedef union {
    uint32_t value;
    uint8_t bytes[4];
} Register;

// Write logic here to extract bytes using union
void print_bytes(uint32_t input) {
    // Your code here
    Register r;
    r.value= input;
    printf("%d ", r.bytes[0]);
    printf("%d ", r.bytes[1]);
    printf("%d ", r.bytes[2]);
    printf("%d", r.bytes[3]);
}

int main() {
    uint32_t num;
    scanf("%u", &num);
    print_bytes(num);
    return 0;
}

Solving Approach

declare a variable of type register

transfer the value of input to the register value

pickup the bytes from the register value

 

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

120 86 52 18