Can U write a C-program to print the size of a data type
without using the sizeof() operator? Explain how it works
inside ?

Answers were Sorted based on User's Feedback



Can U write a C-program to print the size of a data type without using the sizeof() operator? Expla..

Answer / rajesh

#include<stdio.h>
int main()
{
int p;

printf("%d\n",(char*)(&p+1)-(char*)(&p));
return 0;
}

Is This Answer Correct ?    8 Yes 3 No

Can U write a C-program to print the size of a data type without using the sizeof() operator? Expla..

Answer / sanjay bhosale

// SizeOperator.cpp : main project file.
// Program : To implement sizeof operator

#include "stdafx.h"
#include<stdio.h>
#define sizeof_op1(val) ((char *)(&(val) + 1) - (char *)&(val))// for variable
#define sizeof_op2(type) ((type *)0) + 1//((type *) (10) + 1) - (type *) (10) // for type

using namespace System;

int main(array<System::String ^> ^args)
{
int i=0;
char ch = 'a';
float f = 1.00f;

printf("\nSize of int : %d %d",sizeof_op2(int),sizeof(int));
printf("\nSize of char : %d %d",sizeof_op2(char),sizeof(char));
printf("\nSize of float : %d %d",sizeof_op2(float),sizeof(float));
printf("\nSize of long : %d %d",sizeof_op2(long),sizeof(long));
printf("\nSize of short : %d %d",sizeof_op2(short),sizeof(short));
printf("\nSize of double : %d %d",sizeof_op2(double),sizeof(double));
printf("\nSize of long double : %d %d",sizeof_op2(long double),sizeof(long double));

printf("\nsize of int variable :%d %d",sizeof_op1(i),sizeof(i));
printf("\nsize of char variable :%d %d",sizeof_op1(ch),sizeof(ch));
printf("\nsize of float variable :%d %d",sizeof_op1(f),sizeof(f));
//Console::WriteLine(L"Hello World");
getchar();
return 0;
}

Is This Answer Correct ?    0 Yes 0 No

Can U write a C-program to print the size of a data type without using the sizeof() operator? Expla..

Answer / saikat

#include <stdio.h>

int main()
{
float a[2];

int size = (char*)&a[1] - (char*)&a[0];

printf("%d
",size);
return 0;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What are 'near' and 'far' pointers?

0 Answers  


What is the size of empty structure in c?

0 Answers  


What are disadvantages of C language.

0 Answers   iNautix,


What is the difference between procedural and declarative language?

0 Answers  


What tq means in chat?

0 Answers  






Who invented b language?

0 Answers  


main() { int a=4,b=2; a=b<<a + b>>2; printf("%d", a); }

4 Answers   CTS,


write a program to sort the elements in a given array in c language

10 Answers   TCS,


What's the difference between constant char *p and char * constant p?

0 Answers   Celstream,


What is the package for freshers(Non IIT) in amazon(hyderabad). And what is the same for those who are a contract employee.

0 Answers  


macros and function are related in what aspect? a)recursion b)varying no of arguments c)hypochecking d)type declaration

12 Answers   HCL, Infosys, Microsoft,


what is a non volatile key word in c language?

1 Answers  


Categories