• edited by
20,805 views
68 68 votes

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

5 Answers

Best answer
85 85 votes
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
10 10 votes
1+26+51+100+52

=230
2 2 votes

Solution: 

About static variables: 

-Static variables are initialized only once. Subsequent calls to the function do not reinitialize them

 

x += f1() + f2 () + f3() + f2(); <=> x+= ((f1()+f2())+f3())+f4();  [ + operator is left to right associative]

=> f1() call: retruns 26
   f2() call: returns 51 ---- here local x is static int , stored in data segment 
   f3() call: returns 100
   f2() call: retuurn 52 ---- x is not initialized again, x = 51; x++; =>x=52
=> x+=26+51+100+52;
then "+ x"
=> x = 1+229;

=> x = 230

Order of function call is not determined:
for example, the compiler may choose the order:
f1 f2 f3 f2 
or f3 f1 f2 f2
or ....

 

About "int f(void);":

- void => function takes no arguments

 

More Details about Static variables:

-A normal local Variable(without static) is strored inside the stack => when function is done executing it is removed from the stack = > the variable is also removed

-An initialized Static variable is stored in Data segment  => Present in data segment till the end of program execution

-An uninitialized Static variable is stored in BSS

-Although a local static variable is not stored on the stack, its scope is still limited to the function in which it is declared. Only that function can access it by name.

0 0 votes

The difference between int x and static int x is the location in memory is dynamic and static respectively throughout the program depending upon the call.
Inside main function the value of x =1,
f1()=26
f2() first call = 51 now the value of x=50

f3()= 50*2 =100

f2() second call =52 because it has already return 51 now the value of x is assigned 51 after previous call going inside the function.
Therefore x=1+26+51+100+52 = 230

1 flag:
✌ Edit necessary (aditisingh1105)
Answer:
Position:
Show:

Related questions

125 125 votes
2 answers 2 answers
36.4k
36.4k views
go_editor asked Feb 16, 2015
36,399 views
Consider the following C program:#include<stdio.h int main() { int i, j, k = 0; j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; k-= j; for (i=0; i<5; i++) { switch(i+k) { case 1: case 2: ...
95 95 votes
12 answers 12 answers
37.7k
37.7k views
go_editor asked Feb 15, 2015
37,669 views
Consider the following C program#include<stdio.h int main() { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int ptr = p; ptr++; print...
80 80 votes
9 answers 9 answers
29.7k
29.7k views
go_editor asked Feb 14, 2015
29,735 views
Consider the following C program segment.# include <stdio.h int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); }What will be printed by the pro...
21 21 votes
3 answers 3 answers
7.9k
7.9k views
go_editor asked Feb 14, 2015
7,938 views
Choose the most appropriate equation for the function drawn as thick line, in the plot below.$x=y-|y|$$x=-(y-|y|)$$x=y+|y|$$x=-(y+|y|)$