+ 1

If same name variable is declared both data type it shows an error? Explanation?

Example: int a; float a;

3rd Dec 2016, 3:23 PM
Nikhil Sharma
Nikhil Sharma - avatar
3 ответов
+ 6
You can't declare the same variable with different declaration types, if you set it to an integer first it will be used like that, and if you try to change it you will get an error because of what I said in the beginning, you can't declare a with both int and float
3rd Dec 2016, 3:27 PM
Filip
Filip - avatar
+ 5
But for example you can use cast opetator to change the data type. In your case when a is an integer you can do smth like this if((double)a>3.00)
3rd Dec 2016, 3:33 PM
Filip
Filip - avatar
+ 5
because if you use a somewhere then the compiler does not know what to do. It can be either the int-data-member or float data type. So the compiler is not able to seperate both kind. Also when you declare some variable it take a memory block in ram. When you use it compiler go to that address and use it as your program mention. But two variable with same name how it distinguish between them. If you say that it is two different data type, but compiler does not give you that permission to use it like that. Hope you like it
3rd Dec 2016, 3:36 PM
Aditya kumar pandey
Aditya kumar pandey - avatar