Write a program to determine the addresses of memory locations allocated to various variables as follow:
a. declare two short int variables, two int variables, and two long int variables and then output the address of each variable.
b. calculate the size of the memory location allocated to each type by subtracting the address of the first variable from the address of the second variable of the same type. repeat the problem above but with two float variables, two double variables and two long double variables (including a and b sections).

Respuesta :

Here you go,

PROGRAM


#include<stdio.h>

#include<stdlib.h>

int main()

{

   short int a,b;

   int c,d;

   long int e,f;

   short int size_short,size_int,size_long_int;

//printing address of variable using void pointer

   printf("\nAddress of first short int variable : %p", (void*)&a);

   printf("\nAddress of second short int variable : %p", (void*)&b);

   printf("\n\nAddress of first int variable : %p", (void*)&c);

   printf("\nAddress of second int variable : %p", (void*)&d);

   printf("\n\nAddress of first long int variable : %p", (void*)&e);

   printf("\nAddress of second long int variable : %p", (void*)&f);

   //calculating size by subtracting memory address

   size_short = (short int)((void*)&a - (void*)&b);

   size_int = (short int)((void*)&c - (void*)&d);

   size_long_int = (short int)((void*)&e - (void*)&f);

   printf("\n\nsize of short variable by subtracting address : %u", size_short);

   printf("\n size of short variable by using sizeof() : %u", sizeof(a));

   printf("\n\nsize of int variable by subtracting address : %u", size_int);

   printf("\n size of int variable by using sizeof() : %u", sizeof(c));

   printf("\n\nsize of long int variable by subtracting address : %u", size_long_int);

   printf("\n size of long variable by using sizeof() : %u", sizeof(e));

   return 0;

}

ACCESS MORE