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;

void print_bytes(uint32_t input) {
    Register a;
    a.value = input;

    printf("%d %d %d %d", a.bytes[0], a.bytes[1], a.bytes[2], a.bytes[3]);     
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

120 86 52 18