edited by
10,943 views
18 votes
18 votes

The integer value printed by the $\textsf{ANSI-C}$ program given below is _______________

#include<stdio.h>

int funcp(){
    static int x = 1;
    x++;
    return x;
}

int main(){
    int x,y;
    x = funcp();
    y = funcp()+x;
    printf("%d\n", (x+y));
    return 0;
}
edited by

4 Answers

12 votes
12 votes
when $x=funcp()$ is called first time it will go to the function funcp() where local variable $x=1$ is define, increment it and return  value $2$ to local variable $x$ inside main().

when $y=funcp()+x$ is called it will again go to the function funcp(). Since $x$ is a static variable here so new value of $x$ is $2$ itself.increment it and return $3$.

So $y=funcp()+x$ became $y=3+2=5$.

$\therefore x=2,y=5$

$print(x+y)=5+2=7$

the correct answer is $7.$
edited by
9 votes
9 votes

So to answer these type of question I like to take some help from Stack Area and Static Area of Main Memory.

We have 3 areas in Main Memory:

  • Stack Area: For storing Local variable.
  • Static Area: For storing Global and Static variables.
  • Heap Area: For Dynamic Memory Allocation.

In these type of question we are using Stack and Static Area.

After analyzing the code very carefully put Static and Global variables in the Static Area and and auto variables in Stack area.

For the following code:

 

Now in the main function we are calling funcp() function, so activation record of funcp() will be created i.e. a new block named funcp() will be added on the top of main() block in stack. And control will be transfered to funcp().

Now in funcp() function Stack Area’s x will be incremented by one and 2 will be returned by funcp() function to the main(). After that activation record of funcp() will be deleted from stack and control will be transfered to main(). Then 2 will be assigned to x(local to main).

Now again funcp() will be called for y=funcp() + x in the main and activation record of funcp() will be created in stack and the control will be transfered to funcp(). After that stack area’s x will be incremented by one and its value will be 3 and this 3 will be returned to main() and y=funcp() + x becomes y = 3 + 2 (funcp() = 3, x = 2). Then activation record will be deleted for funcp() and control is transfered to main().

Now we are in main and here x(local to main)=2 and y=5, now remember this when global or static & local variables has same name priority is always given to local variable. So in printf("%d\n", (x+y)) we are accessing local x and local y. Hence output will be x+y = 2+5 = 7

Answer will be 7.

edited by
1 votes
1 votes

In main(),

x = funcp()  → makes a call to funcp() → static int x = 1, x++ changes the value of static x to 2 and returns 2 => In main, x=2 (x is assigned with the value 2)

y = funcp() + x  =>     funcp() + 2                                                                                                                                                                                                                                                                                                                |---------  makes a call to funcp() → static int x = 1, will not get executed again and again => x = 2 (from previous operations on static int x), so x++ changes the                                                        value of static int x from 2 to 3 and returns 3 to the caller

=> In main, y = 3 + 2 = 5 

=> x=2 and y=5   

=> printf(“%d\n”, (x+y)) prints the decimal value of x+y = 2+5 =7

 

Therefore, the output is 7

0 votes
0 votes

Answer:7

Let's break down the code step by step:

  1. The program includes the standard input-output library stdio.h.

  2. It defines a function funcp() which returns an integer. Inside funcp(), there's a static variable x initialized to 1. Each time funcp() is called, x is incremented by 1 and then returned.

  3. In the main() function:

    • It declares two integer variables x and y.
    • Calls funcp() and assigns its return value to x.
    • Calls funcp() again, adds its return value with x, and assigns the result to y.
    • Prints the sum of x and y.

Let's trace the execution:

  • First call to funcp(): x becomes 2.
  • Second call to funcp(): x becomes 3 (since it's static, its value persists across function calls), and y becomes 3 + 2 = 5.

So, the final output will be the sum of x and y, which is 2 + 5 = 7.

Answer:

Related questions

9 votes
9 votes
1 answer
1
9 votes
9 votes
3 answers
2
3 votes
3 votes
3 answers
3