-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6th April 2022
25 lines (22 loc) · 893 Bytes
/
6th April 2022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Q.) What will be the output of the following code snippet?
#include <stdio.h>
void fun() {
int a = 15, b = 25;
int c = a + b;
{
int c = b - a;
printf("%d ", c);
}
printf("%d", c);
}
int main() {
fun();
return 0;
}
Ans: 10 40
Option B
Explanation- First the statement (int c=b-a) will be executed and will give output 10 then the next statement(int c=a+b) will be executed and will give output 40 as local variable(c=a-b) is given first priority over the global variable (c=a+b)
This question is based on the concept of scopes.
For the 1st printf statement, the value of local variable 'c' (b-a) will be printed since the statement is inside a block, i.e. 10 will be printed.
For the 2nd printf statement, the value of the global variable 'c' (a+b) will be printed since it is outside the block, i.e. 40 will be printed.
Thus the output will be -> 10 40