answersLogoWhite

0


Best Answer

Yes. Because a variable of the more local scope is what is used instead of any other variables with that name, the data types of the other variables that use that name are irrelevant.

It is, however bad practice to do this because it is confusing and often leads to unintentional function behaviour (particularly in weakly typed languages). If the variable in question is meant to be used in different functions and with a different purpose, there is probably no need to give it the same name as another.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Yes.

const int value {1}; // global value

void foo()

{ // new scope...

char value=2; // local variable (masks the global value)

value += (char) ::value; // use scope resolution to access the global value

{ // new scope...

double value=3.0; // local variable (masks the character value)

value += (double) ::value; // use scope resolution to access the global value

} // end of scope -- makes the character value visible

} // end of scope -- makes the global value visible

Note that it is not possible to access the local character value when the name is masked by another local (the double value). There is no scope resolution within nested scopes.

Of course, you would never deliberately write code such as this since it makes it that much harder to determine which instance of the value you are actually referring to. Using unique names makes it abundantly clear which value is being referred to.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you declare a variable in different scopes with different data types elaborate with examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you declare variables and constants.explain with example?

Depends on the programming language you are using. I will give two simple examples. In Command Prompt and when creating a batch file, you declare a variable by entering the "set" command. You can use different switches to change the type of variable you are declaring. /p makes the variable able to accept user input. /a makes the variable a numerical expression. In Python, you declare a variable just by stating the name of the variable and its value. x = value.


Is possible to declare a variable in a method and declare it a main?

The same identifier (variable name) may be used for at most one variable in each scope. Each method has its own scope, in addition to the global scope which is accessible from all others. However, each scope would have a different variable than every other scope despite using the same name for it.


Can you declare a variable with abstract keyword?

no it is not possible


How do you declare an integer?

datatype variable name;


What is variable declarations?

A variable declaration is a math or programming term. A variable is an unnamed component. In the problem the wording will declare what the value of the variable is.


How you declare a variable in c language?

you have to give a statement in the following syntax datatype variable;


Can you declare a static variable in a header file?

Yes.


Are you required to initiate a variable when you first declare it in php?

no


Is it possible to declare a variable with abstract keyword?

yes


Why all variables are declared with in the function?

It is not necessary to to declare variables inside the function in C. If you declare a variable inside a function, the variable becomes local for the function and another variable of same name can be declared in any other function, but you can not use the variable declared in other function. When you declare any variable outside the function body then the variable becomes global and can be used in any function of the program. Note: errno is an example for a variable declared outside any function.


How you can declare local variable?

variable exit within a function and curly braces is local variable int main() { int x; }


Can you declare any variable above preprocessor directive?

Yes.