Search code examples
cmemorywhile-loopprogramming-languages

How 'while loop' memory works in C programming?


I'm trying to find out if my understanding of the way this simple program works is correct. It works I just want to make sure I understand why.

    #include <stdio.h>

    int main(){
        int x, y = 0, z;
        x = 123;

        while(x != 0){
            z = x % 10;
            y = y * 10 + z;
            x /= 10;
        }

        printf(">>> %d", y);
        return 0;
    }

    output
    >>> 321

I think it goes like this.

So "x" was originally declared with a value of 123 is taken by the loop along with the condition that states while x is not equal to 0 the program continues.

Loop starts and from the original declaration "x" takes what is in memory 123 and "z" is assigned value: the remainder of the following operations; 123 % 10 = 12.3 leaving "z" containing 3

This is how the last number becomes the first number memory now contains 12.

"y" takes what is in memory 12 and is multiplied by 10 totaling 120 added to "z" which is 3 so memory now contains 123.

Then "x" takes what is in memory 123 / 10
totaling 12.3 = 12. So memory now contains 12

Loop starts from previous iteration "x" takes what is in memory 12 and "z" is assigned value: the remainder following operations; 12 % 10 = 2.

This is where the second to last number becomes the second number. So memory now contains 1.

Then "y" takes what is in memory 1 * 10 = 10 2 which is "z" so memory now contains 12.

Then "x" takes what is in memory 12 / 10 = 1.2 and returns only the quotient of 1. So memory now contains 1.

Loop starts from previous iteration "x" takes what is in memory 1 and "z" is assigned value: the remainder of the following operations; 1 % 10 = 0.1 leaving z containing 1.

This is where the first number becomes the last number. So memory now contains 0.

"y" takes what is in memory 0 * 10 = 0 + 1 which is "z". so memory now contains 1.

"x" takes what is in memory 1 / 10 = 0.1 division operator returns only the quotient. So memory now contains 0.

Now "x" = 0 sothe condition has been met and the program breaks.

So if any one can tell me if I'm right or explain to me where I went wrong and help me see the concepts correctly, I would really appreciate their insights.

Thank you.


Solution

  • Your logic is off (rather badly off). The section '…leaving behind the quotient so memory now contains 12. Then y takes what is in memory 12…' is singularly weird. The division is done using integer arithmetic, not floating point. There's a moderate chance there's a register somewhere containing 12, but it isn't multiplied by 10 or anything, and the presence or absence of the 12 is immaterial to the rest of the code (unless the compiler manages to use it instead of executing a second division for x /= 10).

    More accurate version of the logic

    The actual logic is more like:

    • x == 123 means x != 0 so the code goes through the loop:

      • z = 123 % 10 means z == 3.
      • y = 0 * 10 + 3 means y == 3.
      • x /= 10 means x == 12.
    • x == 12 means x != 0 so the code goes through the loop:

      • z = 12 % 10 means z == 2.
      • y = 3 * 10 + 2 means y == 32.
      • x /= 10 means x == 1.
    • x == 1 means x != 0 so the code goes through the loop:

      • z = 1 % 10 means z == 1.
      • y = 32 * 10 + 1 means y == 321.
      • x /= 10 means x == 0
    • x == 0 means the loop terminates:

      • the value of y is 321.

    Using print statements to see how the code runs

    Note that you could have spared yourself some angst by adding suitable printing to the code. For example:

    #include <stdio.h>
    
    int main(){
        int x, y = 0, z;
        x = 123;
    
        printf("x = %d\n", x);
        while(x != 0){
            z = x % 10;
            printf("z = %d\n", z);
            y = y * 10 + z;
            printf("y = %d\n", y);
            x /= 10;
            printf("x = %d\n", x);
        }
        printf(">>> %d\n", y);
        return 0;
    }
    

    You could also learn to step through the code with a debugger to see the values in x, y, and z as it goes through the loop. I find using printf() statements helpful — the coding I do usually lends itself to that sort of debugging. If you develop GUI programs, it is probably less helpful, or requires more care in the setup (where the information is printed) to be helpful.