I want to use the same variable name with a different datatype in C program without casting.
I really wanna do that don't ask why.
So how can I do that ?
And how can I handle the error if this variable doesn't exist while doing prophylactic unsetting ?
If you want the same memory to be referenced with two different types, use a union. Otherwise, know that what follows is a terrible idea.
You can't change the type of a variable in C. You can use a little preprocessor trickery to get the illusion of what you want. i.e.
That being said, I cannot discourage this strongly enough. Don't do it!
When you define a variable with a name that already exists, the new definition "hides" the old one.
You can't. The closest you can get is creating separate scopes and using the same variable name in them:
I don't believe this is possible in C. The only way I can imagine doing this would be to write your program such that the two different variables exist in completely different scopes. Such as when you use them in different functions. Other than that, you're stuck with your first variable, pick a different name.
My suggestion -- if you absolutely require then to exist in the same scope -- would be to prefix the name with a type identifying letter, so:
use a void pointer and cast appropriately ...
by now you should be asking "WTF?" - and yes, that's what you should have been asking yourself when you came up with the idea to re-type your variables :)