The following source code can determine the size of any data type in C without using the sizeof() operator.
The basic concept used in this program is that any operation when performed on the pointers follow the pointer arithmetic, which means that any increments or decrements performed on the pointer follow pointer arithmetic which is based on the scaling factor of that data-type.
The following image clearly shows how pointer arithmetic is performed in C.
Source code:-
/******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int *i_ptr=0;
char *c_ptr=0;
long *l_ptr=0;
float *f_ptr=0;
double *d_ptr=0;
clrscr();
i_ptr++;
c_ptr++;
l_ptr++;
f_ptr++;
d_ptr++;
printf("SIZE OF INTEGER = %d\n",i_ptr);
printf("SIZE OF CHARACTER = %d\n",c_ptr);
printf("SIZE OF LONG = %d\n",l_ptr);
printf("SIZE OF FLOAT = %d\n",f_ptr);
printf("SIZE OF DOUBLE = %d\n",d_ptr);
getch();
return 1;
}
/****************************************************************************/
//here is the program to evaluate the range of a data type
ReplyDelete#include
#include
void main()
{
long j;
int i;
int *i_ptr=0; clrscr();
i=0;
while(i>=0)
{
j=i;
i++;
}
i_ptr++;
printf("range of an integer is from %d to %d ", i , j );
printf("\nsize of int= %d" , i_ptr);
getch();
}