178k views
0 votes
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).

User As As
by
5.4k points

1 Answer

4 votes

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("\\Address of first short int variable : %p", (void*)&a);

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

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

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

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

printf("\\Address 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("\\\\size of short variable by subtracting address : %u", size_short);

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

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

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

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

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

return 0;

}

User Allen Lavoie
by
6.0k points