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

浪尽此生 提交于 2020-08-19 08:42:13

问题


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 starts and also in the loop changes the output of my program. Why is this so?

Code using 'int' declaration once with correct output;

# include<stdio.h>

int main()
{
    int x, y;
    int r = 1;
    int k;
    printf("Enter base number:");
    scanf("%d", &x);
    printf("Enter power");
    scanf("%d", &y);
    for (k = 1; k <= y; k++)
    {
       r = r * x;
    }
    printf("Power is %d", r);
    return 0;
}

Correct output:

Enter base number:7
Enter power3
Power is 343

Incorrect code 2:

# include<stdio.h>

int main()
{
    int x, y;
    int r = 1;
    int k;
    printf("Enter base number:");
    scanf("%d", &x);
    printf("Enter power");
    scanf("%d", &y);
    for (k = 1; k <= y; k++)
    {
       int r = r * x;
    }
    printf("Power is %d", r);
    return 0;
}

Incorrect output:

Enter base number:7
Enter power3
Power is 1

Can you please tell me why is this so.


回答1:


for (k = 1; k <= y; k++)
{
   int r = r * x;
}

int r inside of the for loop declares a separate and unique variable which only is visible in the scope of the for loop.

This r doesn't refer to the r outside of the for loop.

But inside of the initialization part of the loop's (inner) r, the inner r doesn't shadow the outer r because the declaration of the inner r isn't fulfilled at this moment of time.

So, the r in r * x refers to the outer r, not the inner one.

Had you wrote:

for (k = 1; k <= y; k++)
{
   int r = 2; 
   r = r * x;
}

then all r's would refer to the inner r and would completely shadow the outer one.


Thereafter, When you use

printf("Power is %d", r);

it prints the value for the outer r variable, which remains unchanged. This is proven with the output of

Power is 1

Side Note:

  • It doesn't really much sense to declare variables inside of loop's body as you instruct to declare a new variable at each iteration. A compiler can optimize this to only one definition, but better place declarations inside of the initialization part of the for loop or declare them before the loop.



回答2:


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.




回答3:


In your second code snippet, the int r = r * x; declares a new variable called r that exists only for the duration of each iteration of the for loop. Further, this declaration hides (or shadows) the earlier variable of the same name from the scope of that loop - so any changes made to r in the loop do not affect the 'original' r variable.

Enabling compiler warnings will generally reveal such problems. For example, for your second code snippet, the clang-cl compiler gives this:

warning : declaration shadows a local variable [-Wshadow]




回答4:


Because the 'scope' of r in the second code is inside the loop, when you leave the loop the variable is not longer valid.

  for (k = 1; k <= y; k++)
  {
       int r = r * x; //here you create the variable
  } //here it is destroyed

In c/c++ the variables are local to a scope, when you leave the scope you loose the variable. Everything inside {} is a scope (internal to the outside scope). Inside scopes can see variables of outside scope.

void func()
{ // begin scope of a function

   // can't see any variables defined inside the scope of the while
   // they don't exists yet
   while ()
   { // begin scope of a while
     // can see variables defined inside the scope of the function
   }

   // can't see any variables defined inside the scope of the while
   // they don't exists anymore
}


来源:https://stackoverflow.com/questions/63217900/why-the-output-is-different-when-we-add-int-tag-twice-in-a-program-when-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!