write a program for size of a data type without using
sizeof() operator?
Answers were Sorted based on User's Feedback
Answer / desh deepak
void main()
{
char *Ptr1,*Ptr2;
float fl;
ptr1 = &fl;
ptr2 = (&fl+1);
printf("%u",ptr2-ptr1);
}
Is This Answer Correct ? | 18 Yes | 18 No |
Answer / lalit kumar
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
int main()
{
int a;
printf("%u\n",(int)(&a+1)-(int)(&a));
getch();
return 0;
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / fanish
#include <stdio.h>
int main(){
printf("%d\n",((int*)0 + 1));
}
We can replace int with any other type
(Built-in/user-defined ) to get the size.
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / core_coder
#include <stdio.h>
struct node {
int x;
int y;
};
unsigned int find_size ( void* p1, void* p2 )
{
return ( (char*)p2 - (char*)p1 );
}
int main ( int argc, char* argv [] )
{
struct node data_node;
int x = 0;
printf ( "\n The size :%d",
find_size ( (void*) &data_node,
(void*) ( &data_node +
1 ) ) );
printf ( "\n The size :%d", find_size ( (void*) &x,
(void*) ( &x + 1 ) ) );
}
this will work for any data type
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / p sahana upadhya
#include<stdio.h>
#include<math.h>
main()
{
float size;
printf("The Size of Integer Data Type is %d
Bytes\n",(sizeof(int)));
size=pow(2,(sizeof(int)*8));
printf("The Range of Integer Data Type is -%.0f to
%.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Long Integer Data Type is %d
Bytes\n",(sizeof(long int)));
size=pow(2,(sizeof(long int)*8));
printf("The Range of Long Integer Data Type is -%.0f
to %.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Unsigned Long Data Type is %d
Bytes\n",(sizeof(long int)));
size=pow(2,(sizeof(long int)*8));
printf("The Range of Unsigned Long Data Type is 0 to
%.0f\n\n",size);
printf("The Size of Float Data Type is %d Bytes\n",
(sizeof(float)));
size=pow(2,(sizeof(float)*8));
printf("The Range of Float Data Type is -%.0f to
%.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Double Data Type is %d Bytes\n",
(sizeof(double)));
size=pow(2,(sizeof(double)*8));
printf("The Range of Double Data Type is -%.0f to
%.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Long Double Data Type is %d
Bytes\n",(sizeof(long double)));
size=pow(2,(sizeof(long double)*8));
printf("The Range of Long Double Data Type is -%.0f
to %.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Char Data Type is %d Bytes\n",
(sizeof(char)));
size=pow(2,(sizeof(char)*8));
printf("The Range of Character Data Type is -%.0f to
%.0f\n\n",(size/2),((size/2)-1));
printf("The Size of Unsigned Char Data Type is %d
Bytes\n",(sizeof(char)));
size=pow(2,(sizeof(char)*8));
printf("The Range of Unsigned Char Data Type is 0 to
%.0f\n\n",size);
getch();
return 0;
}
This program may seems long, but it definitely works!!!!!
Is This Answer Correct ? | 3 Yes | 3 No |
Answer / neel patwa
//Nice way to find size of any data type...
#include<iostream.h>
void main(){
int *p=0;
p++;
cout<<"\nSize of int="<<(int)p;
}
Note:U can replace int by other datatpe,
to find size of any data type..
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / tishu
#include<stdio.h>
void main()
{
long int i,j,k;
printf("Enter a number");
scanf("%d",&i);
if(-32768<i && i<32767)
j=i*i;
k=j/(j/2);
printf("%d",k);
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / prasad
I think this code is execute in turbo c/c++ perfectly.
#inclde<stdio.h>
#include<conio.h>
void main()
{
int a,b;
float x,y;
clrscr();
a=(int)&x;
b=(int)&y;
printf("%d",b-a);
getch();
}
Is This Answer Correct ? | 4 Yes | 5 No |
Answer / akash patil
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2];
printf("size is %d",(int)&a[1]-(int)&a[0]);
getch();
}
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / learner
#include<stdio.h>
void main()
{
char *ptr1,*ptr2;
char fl;//float,double,int
ptr1 = &fl;//it will take the address of f1
ptr2 = (&fl+1);//it wil increment the address with according thr memory size
printf("%u",(char *)ptr2-(char *)ptr1);//explicit type casting for pointers
}
Is This Answer Correct ? | 0 Yes | 1 No |
what does keyword ‘extern’ mean in a function declaration?
how does printf function work
any C program contains only one function, it must be a) void () b) main () c) message () d) abc ()
please send me papers for Dy. manager IT , PNB. it would be a great help for me.
what is the return type of printf
what is the meaning of 'c' language
What are the advantages and disadvantages of c language?
why u join this call center?
Why is conio.h not required when we save a file as .c and use clrscr() or getch() ?
what is pointer
Bit swapping
without using arithmatic operator solve which number is greater??????????