Memory, hex, little/big endian - please explain where the output of each variabl
ID: 3884689 • Letter: M
Question
Memory, hex, little/big endian - please explain where the output of each variable displayed from the main function came from. - explain so i can learn from this.
Determine the type of addressing and byte ordering. justify your answer.
Verify the value of ival using its binary integer representation.
Explain the results for pval. (4) Explain the results for s.
Determine the sign bit "s", the exponent field "exp", and fraction field "frac", respectively, for the float type variable. Derive the value in decimal according to the IEEE floating-point standard.
Determine the sign bit "s", the exponent field "exp", and fraction field "frac", respectively, for the double type variable. Derive the value in decimal according to the IEEE floating-point standard.
Explain the displayed value of I + 10.
#include <stdio.h>
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]);
printf(" ");
}
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_double(double x)
{
show_bytes((byte_pointer) &x, sizeof(double));
}
void show_pointer(void *x)
{
show_bytes((byte_pointer) &x, sizeof(void *));
}
int main()
{
int I=2147483645;
int ival=-54321;
float fval=(float) ival;
double dval=(double)ival;
int *pval=&ival;
char *s="ABCDEF";
show_int(ival);
show_float(fval);
show_double(dval);
show_pointer(pval);
show_bytes(s,strlen(s));
I=I+10;
printf("2147483645+10= %d",I);
}
Explanation / Answer
The result is as follows (answers are highlighted)-
int l, ival both are out of range of integer (-32768 to 32767)
In the lines -
float fval=(float) ival;
double dval=(double)ival;
explicit casting of ival into float and double type variables
For lines-
int *pval=&ival; // pval is a pointer to ival which contains the address
char *s="ABCDEF"; // s is a pointer to the string
show_int(ival); // the values of ival is passed in show_int method. Inside this method which later calls show_bytes method containing for loop which containes printf statement that changes input ascii string to hex format
show_float(fval); // the values of fval is passed in show_int method. Inside this method show_bytes method makes a call containing for loop which containes printf statement that changes input ascii string to hex format
show_double(dval); // the values of dval is passed in show_int method. Inside this method show_bytes method makes a call containing for loop which containes printf statement that changes input ascii string to hex format
show_pointer(pval); // the values of pval is passed in show_int method. pval is pointer for ival. Inside this method show_bytes method makes a call containing for loop which containes printf statement that changes input ascii string to hex format
show_bytes(s,strlen(s)); // the value of s is passed in show_int method. s is pointer to the string. Inside this method show_bytes method makes a call containing for loop which containes printf statement that changes input ascii string to hex format. For this method strlen is a library function so includes the header file that contains it, string.h.
For-
I=I+10;
printf("2147483645+10= %d",I); // the value of l is out of range of integer data type and keeps on swinging in a range of integer (-32768 to 32767). After a number of oscillations within this range, in the remaining value of l a value of 10 will be added and the result will be printed.