The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
90 views
void print(int i){
    static int x=4;
    if(i!=0){
        print(--x);
    }
    printf("%d",x);
}

What will be output printed for print(10)?


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

asked in Programming by Veteran (112k points) | 90 views
+1

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.

0

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?

+1

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 !

0

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.
answered by (323 points)
0
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
49,811 questions
54,540 answers
188,429 comments
75,596 users