answersLogoWhite

0


Best Answer

Yes, so long as they are declared in separate scopes. But just because it CAN be done, it doesn't mean it SHOULD be done. When you need to access both variables within the same scope, you need to have some way of differentiating them. You could use namespaces to explicitly declare their scope, but your code will be that much easier to read if you simply give each variable an unique name to begin with. This could be as simple as prefixing variables with "g_" for global scope, "f_" for file scope and "m_" for class scope. That way, local function variables don't require prefixes.

The following example demonstrates redeclaration of variables x and y within the same function (within nested scopes), thus making it impossible to access the previous declarations until the new declarations fall from scope. While this can be useful to protect the original variables, there are far better, less ambiguous ways of achieving this: consistent use of the const keyword along with unique identifiers; or simply passing the values (by value) to another function altogether. Ultimately, the fewer ambiguities in your code the easier it will be to read and maintain.

void foo( int x )

{

printf( "x (function level) = %d\r\n", x );

int y = x;

printf( "y (function level) = %d\r\n", y );

if( y )

{

int x=y; // new declaration -- protects previous declaration.

++x;

printf( "x (code block 1) = %d\r\n", x );

int y=x; // new declaration -- protects previous declaration.

printf( "y (code block 1) = %d\r\n", y );

if( y )

{

int x=y; // new declaration -- protects previous declaration.

++x;

printf( "x (code block 2) = %d\r\n", x );

}

printf( "x (code block 1) = %d\r\n", x );

}

printf( "x (function level) = %d\r\n", x );

printf( "y (function level) = %d\r\n", y );

}

int main()

{

foo( 1 );

return( 0 );

}

User Avatar

Wiki User

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

Wiki User

11y ago

Of course

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can two different variables in the same program may have the same name?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


What are all the variables?

There are 'constant variables' , 'independant variables' and 'dependent variables' Constant Variable- things in the experimment that should be kept the same Independant variables- something that can be varied in an experiment Dependant variable- something that can be affected


Define briefly about procedures and macros in microprocessor?

Procedures-Procedure is the set of codes written in other module used 2 solve a specific task and can be included in the main program when an instruction CALL is used in the program. Macro is also the set of codes used in the 'main program' or same module used to solve a specific task. Macros can be called in the program with the syntax using syntax-MACRO_NAME(actual variables). But the macros should be included in the program with the syntax MACRO_NAME(dummy variables). Dummy and actual variables should match.


When does a name clash occur in java programming?

A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.


What is type equivalence in programming language?

Type equivalence occurs when two variables are of the same type. For example, if both variables are int (integer variables), they are of equal types. Equivalence can also occur with two different types that are compatible with each other.

Related questions

The language like 'c' allows the use of same name for two different variables Is it possible for lexical analyser to distinguish between such variables?

Using the same name for two different variables in languages like C is possible only within two different scopes, such as outside a block and inside a block... int somevariable; /* outside scope */ { ... int somevariable; /* inside scope */ } In order for a lexical analyzer to distinguish those two variables, it must be able to parse the program and resolve scope the same way the compiler does.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


How are constant variables different from independent variables?

Constants stays the same independent variables is the variable that is being manipulated


How do you use variable in an addition or subtraction?

well, if you want to add apples to bananas you can't do that it's the same with variables, the coefficients can be different but to add and subtract the variables have to be the same for ex: you can't do 4x+3y because they have different variables you can do 4x+3x because they have the same variables all you do is add the coefficients and keep the variable so you would get 7x


How do you subtract like variables with different exponents?

You can't. You can only subtract like terms. Like terms must have exactly the same variables and exponents on the variables.


What are constants and how they are different from variables?

Constants are fixed numbers that never change, they always stay the same. Variables are number that can change, they 'Vary'.


Can you store the two variables with different values but having same name in session object?

Yes you can store it but the value which is assigned at the last will be accessed when the value is displayed.


What are all the variables?

There are 'constant variables' , 'independant variables' and 'dependent variables' Constant Variable- things in the experimment that should be kept the same Independant variables- something that can be varied in an experiment Dependant variable- something that can be affected


What is a Characteristic for like terms?

They have the same form for any variables, but the numerical coefficients can be different.


How do you solve an equation with the same variable on both sides?

The variables may have different values.


What is the difference between a constructor and a thread?

These concepts are two different things. A 'Constructor' is a special method with the same class name and with no return type definition, invoked only when we create a new object (when we instantiate our object with the 'new' operator). An is used to initialize our object state (our instance variables). A thread is a separate process which runs in parallel (at the same time) in a program.


What is a term whose variables and exponents are the same?

These terms are called like terms.For example: x and 2x are like terms.But: x3 and 4x2 are not like termsbecause although the variables are the same, the exponents are different.