Union Extract Bytes from a 32-bit Value

Code

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

union Reg{
    uint32_t val;
    unsigned char arr[4];
};

// Write logic here to extract bytes using union
void print_bytes(uint32_t input) {
    // Your code here
    union Reg my_reg = {.val =  input};
    for(int i=0; i<4; i++)
    {
        printf("%d ", my_reg.arr[i]);
    }
}

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