Implenting sizeOf() operator in c

thambuzk used Ask the Experts™
Can any one help me in implementing the same sizeof() fuction in C.It should take the input like of
Sizeof(2)---should return 2 bytes(Integer)
sizeof(2.5)--should return 8 bytes(Double)
sizeof(2.5f)--should return 4 bytes(float)
and also it should take variable names declared in the program.
if x is an integer or float or a struct variable it should give me the proper results.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

 you can directly use 'sizeof' macro.
It does take variable, data type or actual data  as an argument and returns the size.

The sizeof macro only applies to compile time variables, types and constants.

To determine the size of a program variable, type or constant, use the macro directly, as suggested by Rohan.

To print the size of input values is another matter. A simple solution for the problem as stated:

       if input is a decimal number followed by an "f"
           print sizeof(float)
       if input is a decimal number
           print sizeof(double)
       if input is an integer
           print sizeof(integer)

In other words determine the type based on the input string and use sizeof() to display the size of that type.


1)see i am not intrested in the build in sizeof in c,i want my own implementation for that.
2)if condition won't work out with program variables
3)sizeof function accepts constants too.

wht i think is if we can get into the actual memory area  and declare a pointer for the same, increment the pointer if it is integer then it will increment by 2 bytes and if it is float then by 4bytes and so on...But the problem comes i don't know wht the user is gona input integer,or float or struct variable so i have to use a void pointer,which won't work out.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

What are you trying to accomplish? Where you write "user input", do you mean input from the command line or stdin? Or do you intend to call your sizeof function at arbitrary points in your program?

If you are trying to determine size of types for user (CLI) input, then my suggestion is valid for that use. You parse the input to determine the type, and then call the built-in sizeof for that type. Note that you are converting an input string to its numeric equivalent. You only need to ensure that the input buffer is sufficiently sized.


By constant, do you mean a constant in input text or a binary pattern in memory?  If the user input is


just look for the . If there is one, the answer is 8.  If an f follows the answer is 4.  If neither then the answer is 2.

If it is a binary pattern in memory then you'll have a hard time distinguishing between a char and an unsigned long long.  At run-time, all the program sees is just a bunch of binary patterns.  If you take the address of the first global variable, then you can get at all the other global stuff but you can't get to the items on the stack.  You'd probably have to examine the varargs macro closely to figure that one out.  Note that it is different for every compiler.
#define sizeof(x) &(x+1) - &(x)
#define sizeof(x) (&x+1) - &x

This will work only for variables

You can't roll your own sizeof(). sizeof() can take an arbitrary expression of arbitrary type, so it certainly can't be a C function.  The pointer arithmetic version won't even work for all variables; what if the variable has the register storage class? For that matter, a type is a valid operand of sizeof; a macro can't distinguish between the name of a (possibly typedef) type and the name of a variable; it doesn't have access to the required symbol table information.
Top Expert 2006

No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade and 30:40:30 split between nrohan:ewest:helloprak

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.


EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial