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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between local variable and global variable in c?

677


Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.

691


When should I declare a function?

611


Are pointers integers in c?

598


how many key words availabel in c a) 28 b) 31 c) 32

623






Which of these functions is safer to use : fgets(), gets()? Why?

625


Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?

657


Why do we need a structure?

574


What is the difference between fread buffer() and fwrite buffer()?

661


What is the use of typedef in c?

574


What is %lu in c?

665


What is an auto variable in c?

740


Can we replace the struct function in tree syntax with a union?

765


How do you write a program which produces its own source code as output?

596


What is the difference between single charater constant and string constant?

612