Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.