# data[].attributes.attachment_link.readable_text
/* This code created by authors of the class textbook, second edition, "Computer Systems, A Programmer's Perspective",
*   Bryant and O'Hallaron.  Chapter 2, figure 2.4.
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* $begin show-bytes */
typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, int len) {
    int i;
    for (i = 0; i < len; i++) 	    
	printf(" %.2x", start[i]);   //the reference start[i] indicates we want to read the byte that is i positions beyond the location pointed by start
    printf("\n");
}

/*  Functions show_int, show_float, and show_pointer use show_bytes to print
the byte representations of C program objects of type int, float, and void*.  They
pass show_bytes a pointer &x to their argument x, casting the pointer to type "unsigned char *".
This cast indicates to the compiler that the program should consider the pointer to be a sequence
of bytes rather then to an object of the original data type.
sizeof operator is used to determine the number of bytes used by the object.  Using sizeof rather then
a fixed value is one step toward writing code that is portable across diferent machine types*/

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); 
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float)); 
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *)); 
}
/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val) {
    int ival = val;
    float fval = (float) ival;
    int *pval = &ival;
    show_int(ival);
   show_float(fval);
    show_pointer(pval);

}
/* $end test-show-bytes */


int main()
{
    int val = 12345;
   char *c = (char*)&val; 
   if (*c+48)     
       printf("Little endian"); 
   else
       printf("Big endian");

    //  byte_pointer valp = (byte_pointer) &val;
    byte_pointer valp = (byte_pointer) &val; 
    show_bytes(valp,1);
    return 0;
}

# data[].attributes.attachment_link.readable_text
#include <stdio.h>
int main(){
/* a representative sample of builtin types */
    unsigned char uc; char c;
    enum{e1, e2, e3}e123;
    short si; int i; long li;
    unsigned short su; unsigned u; unsigned long lu;
    float sf; float f; double lf; long double llf;
    union {char c; unsigned u; int i; float f; }ucuif;  /* manual casting only */
    struct {char c; unsigned u; int i; float f; }scuif; /* manual casting only */
    int ai[99];
    int (*rai)[99];
    int *ri;
    uc = '1';
/* a representitive sample of implied casts for subtypes of personality int/float */
    c=uc;
    si=uc; si=c;
    su=uc; su=c; su=si;
    i=uc;  i=c;  i=si;  i=su;
    e123=i; i=e123;
    u=uc;  u=c;  u=si;  u=su;  u=i;
    li=uc; li=c; li=si; li=su; li=i; li=u;
    lu=uc; lu=c; lu=si; lu=su; lu=i; lu=u; lu=li;
    sf=uc; sf=c; sf=si; sf=su; sf=i; sf=u; sf=li; sf=lu;
    f=uc;  f=c;  f=si;  f=su;  f=i;  f=u;  f=li;  f=lu;  f=sf;
    lf=uc; lf=c; lf=si; lf=su; lf=i; lf=u; lf=li; lf=lu; lf=sf; lf=f;
    llf=uc;llf=c;llf=si;llf=su;llf=i;llf=u;llf=li;llf=lu;llf=sf;llf=f;llf=lf;
/*  ucuif = i; no implied cast; try: iucuif.i = i */
/*  ai = i; no implied cast; try: rai = &i */
/* a representitive sample of implied casts, type pointer */
    rai = &ai; /* starts at the first element of ai */
    ri = ai;  /* points to the first element of ai */
/* a summary result, using the longest sizeof increasing casting path */
    printf("%LF %d %d %d %d %d %c \nAnalyze this output ",
           llf=(lf=(i=(si=c))), sizeof llf, sizeof lf, sizeof i, sizeof si,sizeof c, c);
return 0;
}