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

Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 10 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.

0 Answers   Convergys,


What is the sizeof () a pointer?

0 Answers  


Distinguish between actual and formal arguments.

0 Answers  


How can I find the modification date and time of a file?

0 Answers  


How can I swap two values without using a temporary?

0 Answers  






Which of the following are valid "include" formats? A)#include and #include[file.h] B)#include (file.h) and #include C)#include [file.h] and #include "file.h" D)#include <file.h> and #include "file.h"

15 Answers   Accenture,


What is the collection of communication lines and routers called?

0 Answers  


If one class contains another class as a member, in what order are the two class constructors called a) Constructor for the member class is called first b) Constructor for the member class is called second c) Only one of the constructors is called d) all of the above

0 Answers  


matrix multiplication fails introspect the causes for its failure and write down the possible reasons for its failurein c language.

5 Answers   TCS,


design and implement a program that reads floating-points numbers in a sentinel-controlled loop until the user terminates the program by entering zero.your program should determinate and print the smallest,largest and average of the supplied numbers.

2 Answers  


without a terminator how can we print a message in a printf () function.

7 Answers   NIIT,


Difference between exit() and _exit() function?

0 Answers  


Categories