printf changing its value after it should have already been printed
c, printf
Solution
Just care to write :
int num = 0, place = 0;
And you will be surely done.
That's why I recommend declaring one variable by line.
Problem
So I have this code here, that (for now) just turns a base1 number in to a base 10 number ``` /** * This function is supposed to convert 'number' from base1 to a number in base2. * It isn't fully implemented yet, and I've only converted base1 to a base 10 number. */ void base1_base2(int base1, int base2, int number) { int num, place = 0; int rem = number; int i; for (i = 0; i < num_digits(number); i++) { int mod = rem % 10; rem = floor(rem / 10); int powerResult = pow(base1, place++); num = num + mod * powerResult; } int base10_num = num; printf("The number %i(base%i) in base 10 is: %i\n", number, base1, base10_num); } ``` And this calculates the correct base10 number, i.e. the function call base1_convert(2, 5, 100) would return 4, as expected: `The number 100(base2) in base 10 is: 4` The problem arises when I add this code directly beneath the last printf in that function: ``` int base2_num = 0; printf("The number %i(base%i) is: %d\n", number, base1, base2_num); ``` If the above code is added, it completely changes the result of the first printf, making it return: ``` The number 100(base2) in base 10 is: 4196784. The number 100(base2) is: 0 ``` I can not for the life of me figure out why this is happening. I'm guessing it has to do with some pointers, but I haven't really used any, and I have no idea why adding a seemingly independent variable `int base2_num = 0;` would change all of that. Here is the complete modified code after the above lines are added. ``` void base1_base2(int base1, int base2, int number) { int num, place = 0; int rem = number; int i; for (i = 0; i < num_digits(number); i++) { int mod = rem % 10; rem = floor(rem / 10); int powerResult = pow(base1, place++); num = num + mod * powerResult; } int base10_num = num; printf("The number %i(base%i) in base 10 is: %i\n", number, base1, base10_num); int base2_num = 0; printf("The number %i(base%i) is: %d\n", number, base1, base2_num); return; } ``` Edit: And here is the only other function I used, num_digits. It returns the number of digits in an integer. ``` int num_digits(int integer) { char int_string[100]; int str_length; sprintf(int_string, "%i", integer); str_length = strlen(int_string); return str_length; } ``` And here is where I call the function: ``` int main() { printf("Hello World\n"); base1_base2(2, 5, 100); return 0; } ```