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 are bitwise shift operators in c programming?
How would you find a cycle in a linked list?
1. Write a function to display the sum of two numbers in the following ways: By using (i) pass by value (ii) pass by address a. function with argument and with return value b. function with argument and without return value c. without argument , with return value d. without argument , without return value Note: Use pass by address.
Can you define which header file to include at compile time?
main() { int i; printf("%d",i^i); }
Write a program that his output * *** *****
I have a function which accepts, and is supposed to initialize,a pointer, but the pointer in the caller remains unchanged.
What is a constant and types of constants in c?
Is there any book to know about Basics of C Language?
question-how to run a c programme.
sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x?
What is pre-emptive data structure and explain it with example?