A function that prints out the values of each of the bytes of a variable, which value is given from stdin.
Consists of size detector and a loop.
#include <stdio.h>
#include <math.h>
int main(void)
{
unsigned char uint_8;
unsigned short uint_16;
unsigned long uint_32;
unsigned long long uint_64, n = 0;
int i;
size_t size;
printf("Enter a number: ");
scanf("%lld", &n);
if(n < 257) size = 1;
if(n < 256*256 && n > 256) size = 2;
if(n < 256*256*256 && n > 256*256) size = 3;
if(n < pow(256, 4) && n > pow(256, 3)) size = 4;
for(i = 0; i < size; i++)
{
unsigned char* p = (((unsigned char*)&n) + i);
printf("byte(%i) : %i\n", i, *p);
}
return 0;
}
I don't like the size detection though, it could be more safely coded with one line of bit operations I believe.