Why the output is different when we add 'int' tag twice in a program when using 'for' loop?

前端 未结 4 863
滥情空心
滥情空心 2021-01-15 12:26

I am a learner and new to C language. While I was creating a function which will give power of two numbers using for loop I encounter that using int declaration before loop

4条回答
  •  一个人的身影
    2021-01-15 12:54

    For the first piece of code, r = r * x refers to the r defined outside the for loop, which means it works as expected. For the second piece of code, when you do int r = r * x, you're declaring another r scoped to the for loop, and the changes are not reflected in the outer r. Then, outside the for loop, you switch back to the outer r, which is still 1.

提交回复
热议问题