TCS
Company
Programming
Technical
Predict the output of following code:
main()
{
int var=20;
// scope of the local variable is within function or block
printf(“%d,”,var); //outer block
{
int var=30;
//Inner block
printf(“%d”,var);
}
}
1. Error
2. 20,30
3. 20,20
4. Garbage value
Read Solution (Total 3)
-
- 20,30 first var contain 20 so first printf will print 20 after that second printf will print 30 because that var is more local than var containing 20 value
- 6 years agoHelpfull: Yes(8) No(0)
- ans will be option c.any local variable and block variable has its scope in which it is defined.out variable named var has scope till the end of main function.but inner variable var has its scope only in its block.any two variable having same name inner variable will hide the oute variable.so in this block var =30;
refer www.codingfox.com and see storage class concept or scope and life time of variable - 6 years agoHelpfull: Yes(0) No(4)
- output will be 20,30
- 5 years agoHelpfull: Yes(0) No(0)
TCS Other Question