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 |
How do we select the big element or any other operation from array which is read dynamically. user need to give the elements only no need to mention the size.
What is the difference between fread and fwrite function?
Why are all header files not declared in every c program?
Explain what will be the outcome of the following conditional statement if the value of variable s is 10?
Why c is called object oriented language?
Tell us the difference between these two : #include"stdio.h" #include<stdio.h> define in detial.
what is the difference between declaration and definition of a variable or function ?
Can u return two values using return keyword? If yes, how? If no, why?
What is wrong with this program statement?
How is pointer initialized in c?
Is sizeof a keyword in c?
what is the c source code for the below output? 5555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 5555555555