The Gateway to Computer Science Excellence
+28 votes

Consider the following C program:

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 ______.

in Programming by
edited by | 4.3k views

2 Answers

+48 votes
Best answer
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$
edited by
why does f(1)  return 1?

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
What about increment operator in f3() function?
there is no such operator made easy book it is given wrong
will f3() permanently change the global variable x to 100? or it'll make x=100 just within its scope?
If the concept of dynamic scoping was used, then what would have been the output to a single call of f3() ?

yes f3 will change the global value, means next time if someone uses global x, the value will be 100.   check program here


 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


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

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?
Doubt is it  function calling is done based on + is left to right associative that why f1 calling first then f2 then f3.

@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?

C follows static scoping by default.

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

@ 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.

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

int x; --------- is global then x=0 automatically
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
Because inside main, int x=1 is defined as a global variable and so x will use its value =1.
+7 votes

In main, x is declared and initialized and x is declared globally in the program, that means global variable will get priority during execution ?

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,504 answers
95,336 users