Question

If a variable is declared both locally and globally, it is treated as if it were...

If a variable is declared both locally and globally, it is treated as if it were two different variables, and the __________ declaration takes precedence.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi There,

Please find my explanation below. Please comment below in case of any concerns or if you have any queries.

When a variable is declared both locally and globally, the LOCAL declaration takes precedence.

I will explain this with a C programming example:

#include <stdio.h>

//global variable declaration
int variable = 20;

void precedence_test(){
//local variable declaration
int variable=10;
printf ("Local value of variable = %d\n", variable);

}
int main () {

printf ("Global value of variable = %d\n", variable);
precedence_test();
return 0;
}

Output:

As we can see here, the Global value of the variable is 20. When the same variable is declared with the value 10 locally in a function, the variable value in local declaration takes the precedence and prints 10 and not 20.

Add a comment
Know the answer?
Add Answer to:
If a variable is declared both locally and globally, it is treated as if it were...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT