How can I unset a variable in C to allow usage of the same name with different datatype later on?

AssemblerGuy picture AssemblerGuy · Dec 7, 2010 · Viewed 8.5k times · Source

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 ?

Answer

Justin Spahr-Summers picture Justin Spahr-Summers · Dec 7, 2010

You can't. The closest you can get is creating separate scopes and using the same variable name in them:

 {
     int val;

     // do something with 'val'
 }

 {
     double val;

     // do something with 'val'
 }