edited by
11,725 views
41 votes
41 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 ______.

edited by

4 Answers

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

=230
0 votes
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

Answer:

Related questions

62 votes
62 votes
11 answers
2
47 votes
47 votes
8 answers
3
go_editor asked Feb 14, 2015
15,861 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...