Search code examples
cscopevariable-assignment

How to permanently store an integer variable in C without using constant?


So the problem is that I have an integer, but I need to reuse this a lot and it would be a mess if I call a different function to do different mathematical operation (Luhn's Alogrithem). So is there a way to do that?

while(num != 0)
{
    array[length] = (num % 10);
    num = num/10;      //here its clear that number will become 0
    length --;  
}

int secondArrayLength = 0;

for( int secondlength = countDigits(num) - 1; secondlength > 0; secondlength = secondlength - 2)
{
    if (array[secondlength] > 0)
    {
        secondArrayLength ++;
    }
}

So its clear in the code that by doing the num = num/10 I will eventually get zero. This is then unusable in the for( int secondlength = countDigits(num) - 1.

Is there a way around this without the need to declare another variable to temporary store number?

(edit: number is asked by a user so I can not declare it as a constant)


Solution

  • C has no "magic" that can/will make the value of num after the while the same as the value before the while loop.

    You need to write code so that it happens. And that can be done in several ways.

    1: Use a function

    Putting the while loop code into a function is the closest you get to the desired "magic" behavior. In reality there is no "magic". Behind you back a copy of num will be made.

    Anyway - If you pass num to a function then the changes made to num inside the function will not change num in the caller code. For instance:

    int doX(int num, int length, int array[])
    {
        while(num != 0)
        {
            array[length] = (num % 10);
            num = num/10;      //here its clear that number will become 0
            length--;  
        }
        return length;
    }
    

    and then do:

    length = doX(num, length, array);
    
    int secondArrayLength = 0;
    
    for( int secondlength = countDigits(num) - 1; secondlength > 0; secondlength = secondlength - 2)
    {
        if (array[secondlength] > 0)
        {
            secondArrayLength ++;
        }
    }
    

    A variant would be to pass length as a pointer so that it can be changed in the function, i.e. then the return and assignment could be removed.

    (notice: The changes made to the array by the function will be remain when the function returns. Arrays are handled different than e.g. plain integers).

    2: Use save and restore

    Use an extra variable to save the value of num before the loop and restore it after the loop.

    saved_num = num;
    while(num != 0)
    {
        ...
    }
    num = saved_num;
    
    int secondArrayLength = 0;
    
    ...
    

    A variant of this solution is to make the while loop code operate on saved_num instead of num. Then you don't need the restore part as num isn't changed. Or as suggested by user Fe2O3 you could implement this variant as a for loop like the below:

    for (int x = num; x != 0; x = x / 10)
    {
        array[length] = x % 10;
        length--;  
    }