Search Placementyogi.com
 
1

What is the output of:

#include<stdio.h>
int main()
{
	int x=40;
	{
		int x=20;
		printf("%d",x);
	}
	printf("%d",x);
	return 0;
}
A.

40 40

B.

20 40

C.

40 20

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

The variable declared inside the  inner block replaces the x declared in the outer block, hence it prints 20 at 1st printf.

When the inner block ends, the scope of inner x also ends and hence the value of x becomes 40 in the outer block.

 

Yogesh 6/12/2013 11:08:15 PM
Hello sir :P - YP
mani 6/19/2014 12:05:00 PM

is it possible to have curley brace with the variable declaration

Anoop 9/9/2014 8:34:34 PM

Yes it is possible. 

Preview

Thanks for posting your comment! It will be visible soon!
Post Another Comment

2
+
8
=

Name:
Email :
Banner to LogicGuns.com

Hide Page Information