What is printed by this C code: void inc(int x) { x++; } int a = 5; inc(a); printf(“%d”, a);?
Correct answer C. 5
Explanation
C passes every argument by value: inc receives its own copy of a in the parameter x, and incrementing x has no effect on the caller's variable. So a is still 5. To let a function change the caller's variable you pass its address, as in void inc(int *x) { (*x)++; } called with inc(&a). Arrays only look different because they decay to pointers.
Answer checked How we write and check questions
Discussion