C Program to show memory representation of datatypes


#include <stdio.h>
#include<conio.h>
typedef unsigned char *byte_ptr;

void showbytes(byte_ptr start, int len) //show bytes takes byte pointer as an argument and prints memory
{                                                           //contents from byte_ptr  to byte_ptr + len
     int i;
     for (i = 0; i < len; i++)
           printf(" %.2x", start[i]);
     printf("\n");
}

void showint(int x)
{
     showbytes((byte_ptr) &x, sizeof(int));
}

void showfloat(float x)
{
     showbytes((byte_ptr) &x, sizeof(float));
}

int main()
{
    int i = 1;
    float f = 1.0;
    showfloat(f);
    showint(i);
      getch();
}

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Programs and Puzzles in technical interviews i faced