Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I. Lab4 Description: Write 6 procedures: show_bytes (), show_int (),show_pointer ( ), show_float (), show_long(), show_double () by using the C code by modifying your

I. Lab4 Description: Write 6 procedures: show_bytes (), show_int (),show_pointer ( ), show_float (),

show_long(), show_double () by using the C code by modifying your Textbook Page 45 (Figure 2.4).

Requirement: Your Lab 4 will print the byte representations of C objects of types: int, long, float, double and

void *, respectively. Observe that they simply pass show_bytes a pointer &x to their argument x, casting the

pointer to be of type unsigned char *. This cast indicates to the compiler that the program should consider the

pointer to be to a sequence of bytes rather than to an object of the original data type. This pointer will then be to

the lowest byte address occupied by the object (little endian machine like our Linux).

II. Hint:

//Lab4

//Name:

#include

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer , size_t );

void show_int(int );

void show_pointer(void *);

void show_float(float );

void show_long(long );

void show_double(double);

int main(void){

//int

int ival=12345;

printf ("ival variable's memory address is %p ", &ival);

printf("ival variable's int value is %i ", ival);

printf("ival variable's HEX value =Ox%x ", ival);

int *pval=&ival;

show_int(ival);

show_pointer(pval);

//float

float fval=(float) ival;

printf ("fval variable's memory address is %p ", &fval);

printf("fval variable's float value is %f ", fval);

printf("fval variable's HEX value=0x%x ", *(unsigned int *) &fval);

show_float(fval);

//long

long lval=(long) ival;

printf ("lval variable's memory address is %p ", &lval);

printf("lval variable's long int value is %ld ", lval);

printf("lval variable's HEX value=0x%.8x ", *(unsigned int *) &lval);

show_long(lval);

//double

double dval=(double) lval;

printf ("dval variable's memory address is %p ", &dval);

printf("dval variable's double value is %f ", dval);

show_double(dval);

return 0;

}//ending main

III. Output:

Step 1: gcc Lab4_First_LastName.c o Lab4.exe Hint: to compile your Lab4_First_LastName.c

source code

Step 2: $ ./Lab4.exe Hint: to run the result and you must produce the

similar outputs like below:

ival variable's memory address is 0x7ffe1f169ef8

ival variable's int value is 12345

ival variable's HEX value =Ox3039

show_int() function displays variable byte representation:

int x memory address is 0x7ffe1f169ecc

0x7ffe1f169ecc 0x39

0x7ffe1f169ecd 0x30

0x7ffe1f169ece 0x00

0x7ffe1f169ecf 0x00

show_pointer() function displays byte representation

0x7ffe1f169ec8 0xf8

0x7ffe1f169ec9 0x9e

0x7ffe1f169eca 0x16

0x7ffe1f169ecb 0x1f

0x7ffe1f169ecc 0xfe

0x7ffe1f169ecd 0x7f

0x7ffe1f169ece 0x00

0x7ffe1f169ecf 0x00

fval variable's memory address is 0x7ffe1f169efc

fval variable's float value is 12345.000000

fval variable's HEX value=0x4640e400

show_float() function displays variable byte representation:

float x memory address is 0x7ffe1f169ecc

0x7ffe1f169ecc 0x00

0x7ffe1f169ecd 0xe4

0x7ffe1f169ece 0x40

0x7ffe1f169ecf 0x46

lval variable's memory address is 0x7ffe1f169f00

lval variable's long int value is 12345

lval variable's HEX value=0x00003039

show_long() function displays variable byte representation:

long x memory address is 0x7ffe1f169ec8

0x7ffe1f169ec8 0x39

0x7ffe1f169ec9 0x30

0x7ffe1f169eca 0x00

0x7ffe1f169ecb 0x00

0x7ffe1f169ecc 0x00

0x7ffe1f169ecd 0x00

0x7ffe1f169ece 0x00

0x7ffe1f169ecf 0x00

dval variable's memory address is 0x7ffe1f169f08

dval variable's double value is 12345.000000

show_double() function displays variable byte representation:

double x memory address is 0x7ffe1f169ec8

0x7ffe1f169ec8 0x00

0x7ffe1f169ec9 0x00

0x7ffe1f169eca 0x00

0x7ffe1f169ecb 0x00

0x7ffe1f169ecc 0x80

0x7ffe1f169ecd 0x1c

0x7ffe1f169ece 0xc8

0x7ffe1f169ecf 0x40

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions