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 can I dynamically allocate arrays?
Give me the code of in-order recursive and non-recursive.
which of 'arrays' or 'pointers' are faster?
Write code for initializing one dimentional and two dimentional array in a C Program?
5 Answers Deshaw, Edutech, GMD,
What is main function in c?
At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these names could be kept in that shelf as in this example: bottom of shelf ---> [AAAJKRDFDEWAAYFYYKK]-----Top of shelf. All these packets are to be loaded on cars. The cars are lined in order, so that the packet could be loaded on them. The cars are also named [A, B, C, D, E,………….]. Each Car will load the packet with the same alphabet. So, for example, car ‘A’ will load all the packets with name ‘A’. Each particular car will come at the loading point only once. The cars will come at the loading point in alphabetical order. So, car ‘B’ will come and take all the packets with name ‘B’ from the shelf, then car ‘C’ will come. No matter how deep in the shelf any packet ‘B’ is, all of the ‘B’ packets will be displaced before the ‘C’ car arrives. For that purpose, some additional shelves are provided. The packets which are after the packet B, are kept in those shelves. Any one of these shelves contains only packets, having the same name. For example, if any particular shelf is used and if a packet with name X is in it, then only the packets having names X will be kept in it. That shelf will look like [XXXXXXX]. If any shelf is used once, then it could be used again only if it is vacant. Packets from the initial shelf could be unloaded from top only. Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.
How is null defined in c?
What's the difference between calloc() and malloc()?
what is c?
What are volatile variables in c?
Is it possible to run a c program without using main?If yes HOW??
what is a stack