The Gateway to Computer Science Excellence
0 votes
void print(int i){
    static int x=4;

What will be output printed for print(10)?

Will it print value as call by value or call by reference?

in Programming by Veteran (116k points) | 99 views

Will it print value as call by value or call by reference?

if it is C program, then it is call by value only due to there is no concept of call by reference in C.

if it is not C program, questioner have to mentioned is it as call by value or call by reference.


output printed for print(10)? or output printed for print(11)? or output printed for print(12)? or in general output printed for print(k)?

there is same output due to "o/p depends upon x value"

Finally the o/p is four zero's.


that is my question , why all print last value of X as like call by reference

will static store the value of X

Is call by value also check memory location of X

I mean it is decrementing one by one

but will show value it last store it's memory location?


actually, after calling sequence end, we have to print x for every call.

But x is static variable, so it have the updated values in the calling sequence too.

So at the last iteration, when x assigned to zero, from then onwards it is accessing updated x.


Is call by value also check memory location of X

when you want to print something then it's memory location checked !


output: 00000

sequence of Five zero's will be printed.

1 Answer

0 votes
print(10) -> print(3) -> print(2) -> print(1) -> print(0).

Above mentioned sequence is the function call sequence, as the variable 'x' is declared as static, changes that you make to the variable will persist between function calls. The final value of the variable will be '0'.

Hence the output will be 00000, as we five function calls, x will be printed 5 times.

It's call by value(but the variable we are printing is stored in static memory), and not call by reference.
by (333 points)
I think it would print four zeros instead of five and by call by reference or call by value it should output same as x that is static variable is involved in function calling sequence correct if I am wrong

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
50,666 questions
56,131 answers
93,307 users