All submissions

Union Extract Bytes from a 32-bit Value

Create a local copy of data into union

#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) {
    uint8_t i;
    Register temp;
    temp.value = input;
    for(i=0; i<4; i++){
        printf("%d", temp.bytes[i]);
        if(i<3)     putchar(' ');
    } 
}

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

Solving Approach

create a copy and use array to print the elements, use .bytes[i], default little endian is used.

 

 

 

Loading...

Input

305419896

Expected Output

120 86 52 18