3.5k views

Consider the following C program:

#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x=10;
int main()
{
int x=1;
x += f1() + f2 () + f3() + f2();
printf("%d", x);
return 0;
}
int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;}

The output of the program is ______.

edited | 3.5k views

The variable $x$ is initialized to $1$. First and only call to $f1()$ returns $26$. First call to $f2()$ returns $51$. First and only call to $f3()$ returns $100$. Second call to $f2()$ returns $52$ (The value of local static variable $x$ in $f2()$ retains its previous value $51$ and is incremented by $1$).

$$x=1+26+51+100+52 = 230$$

Answer: $230$
by Active (1.6k points)
edited by
+2
why does f(1)  return 1?
+8

its not F(1) its value of x.' f(1) = 26

x = x+ f1() + f2 () + f3() + f2();
f1()= 26
f2 ()=51
f3()= 100 because = x= x*10 // global x= 10
f2()= 52
–1
What about increment operator in f3() function?
0
there is no such operator bro.........in made easy book it is given wrong
0
will f3() permanently change the global variable x to 100? or it'll make x=100 just within its scope?
0
If the concept of dynamic scoping was used, then what would have been the output to a single call of f3() ?
+1

yes f3 will change the global value, means next time if someone uses global x, the value will be 100.   check program here http://ideone.com/L5U3ao

0

Ayush Upadhyaya if we use dynamic scoping , as f3() called from main() and in f3() there is no memory allocated for x so it look for memory in main() , in main() , x=1 so f3() access it and return 1*10=10

0

when f2() is called second time then why

static int x = 50 is not executed and it continued from previous value 51

and yes i know that static variables are saved in data section so they hold their value but why again it is not initilized to 50

0
in the above program, how many times memory is allocated for x?

i feel that memory allocation is done two times i.e one for the global variable and the other one is for static variable. please clear my doubt?
0
Doubt is it  function calling is done based on + is left to right associative that why f1 calling first then f2 then f3.
0

@Gate Ranker18 @Shaik Masthan @Arjun  Sir pls explain how to know whether static/dynamic scoping is used?

why f3 takes the value for x as 10, and not 1 as it is called from main?

+1
C follows static scoping by default.

So, F3 is taken from the value of it's ancestor ===> Global value of x
0
Thanks :) now it's clear.
0

@ sir    sir

In static scoping free variables are resolved by global variable & In dynamic scoping free variables are resolved by previous function's local variable.

Now in case of static scoping if global variable is not initialized, only declared then will free variables take garbage value????

I mean in global variable declaration

int x=10;

Instead of this, If it is int x;  only then what would happen.

+2
global values initialize to their default values but not contain the garbage values !

int x; --------- is global then x=0 automatically
0
And after f3() executed successfully , global x value changes to 100. Because global x only work when in any function , there is no declaration of that variable and also changes for any operation for that variable in that function