Header Ads

12 - WAP to Implement own Sizeof operator using Macro in C



This program performs a Size of operation without using a "sizeof" operator.

  • The following  code prints the Size of each passed Data typesto the defined Macro to get the exact Size of the type. 
//Program for Implementing a Sizeof Macro.
#include 
//Sizeof Macro..
#define SIZE(x) (char *)(&x+1)-(char *)(&x)
//Main Program Function..
int main()
{
 //Declaring Datatypes
 int x;
 char r;
 float f;
 double d;
 short s;
 long l;
 long long ll;
 //Getting the Size by calling the Size of Macro.
 printf("Size of Int : %d Byte\n", SIZE(x));
 printf("Size of Char : %d Byte\n", SIZE(r));
 printf("Size of Float : %d Byte\n", SIZE(f));
 printf("Size of Double : %d Byte\n", SIZE(d));
 printf("Size of Short : %d Byte\n", SIZE(s));
 printf("Size of Long : %d Byte\n", SIZE(l));  
 printf("Size of Long Long : %d Byte\n", SIZE(ll));
 return 0;
}

Program Output:

No comments