ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
write a program for size of a data type without using
sizeof() operator?
 Question Submitted By :: Vishnu948923
I also faced this Question!!     Rank Answer Posted By  
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 1
PLS TYPE ANSWER
 
Is This Answer Correct ?    0 Yes 1 No
Puneet
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 2
This code will work in TC with 2 warnings but can get result

void main()
{
  char *Ptr1,*Ptr2;
  float fl;
  ptr1 = &fl;
  ptr2 = (&fl+1);

  printf("%u",ptr2-ptr1);
}

This is a way to get the size of data type...waiting for 
any other way...
 
Is This Answer Correct ?    5 Yes 2 No
G
 
 
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 3
This has been solved in parts. I am not sure if there are
any better method merging it.

case 1. User passes a variable as the parameter.
eg: int n;
sizeof(n);

case 2. User passes a data type as the parameter.
eg: sizeof(int)


Solution
case 1: #define GetSize(x) (char*)(&x + 1) - (char*)&x

case 2:#define GetMySize(x) (char*)((x*)10 + 1) - (char*)10
 
Is This Answer Correct ?    1 Yes 0 No
Sunil
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 4
void main()
{
  char *Ptr1,*Ptr2;
  float fl;
  ptr1 = &fl;
  ptr2 = (&fl+1);

  printf("%u",ptr2-ptr1);
}
 
Is This Answer Correct ?    3 Yes 1 No
Desh Deepak
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 5
#include<stdio.h>
int main()
{
int n;
int x,*p,*p1;/* here u can change the type */
p=&x;
p1=(p+1);

printf("size of x is : %d\n",n=(char *)(p1)-(char *)p);
}
Note:without type cast, it always gives 1.
i.e 1 int(4 chars), 1 float(4 chars),1 double(8
chars)etc...coz p+1 points to the next new location of same
type.
 
Is This Answer Correct ?    0 Yes 0 No
Govind279
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 6
#include <stdio.h>

struct node {
	int x;
	int y;
};

unsigned int find_size ( void* p1, void* p2 )
{
	return ( p2 - 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 ?    2 Yes 0 No
Abdur Rab
 
  Re: write a program for size of a data type without using sizeof() operator?
Answer
# 7
#include<stdio.h>
#include<conio.h>
void main()
{
int integer[2],p;
float floating[2],q;
char character[2],r;
double doubling[2],s;
clrscr();
p=(integer+1)-integer;
q=(floating+1)-floating;
r=(character+1)-character;
s=(doubling+1)-doubling;
printf("the sizeof int is :%d",p);
printf("\nthe size of float is :%d",q);
printf("\nthe size of character is :%d",r);
printf("\nthe size of double is :%d",s);
getch();
}


thank u
 
Is This Answer Correct ?    0 Yes 0 No
Vignesh1988i
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
what is difference between ++(*p) and (*p)++ Accenture15
print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5  5
why r u join this company? give solid resons. Infosys7
How to convert a binary number to Hexa decimal number?? (Note:Do not convert it into binary and to Hexadecimal) Subex1
find largest element in array w/o using sorting techniques. Zycus-Infotech2
Give a fast way to multiply a number by 7 Microsoft8
What is encapsulation?  1
write a program that print itself even if the source file is deleted?  1
HOW DO YOU HANDLE EXCEPTIONS IN C? AppLabs2
#include<stdio.h> main() { char *p1; char *p2; p1=(char *) malloc(25); p2=(char *) malloc(25); strcpy(p1,"Ramco"); strcpy(p2,"Systems"); strcat(p1,p2); printf("%s",p1); } Tell me the output? Ramco5
main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); } what is the output? Ramco4
how to implement stack work as a queue?  2
can we print any string in c language without using semicolon(;)(terminator) in whole program.  6
the format specified for hexa decimal is a.%d b.%o c.%x d.%u TCS4
related to rdbms query .  1
how to display 2-D array elements in spiral  1
C program to find frequency of each character in a text file?  3
What is sparse file?  1
what is a far pointer TCS9
what are the compilation steps? ( i want inside the compiler )  1
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com