Skip to content
Advertisement

Python vs C: different outputs

I have a small program that converts a base 10 number into a kind of a base 36 number. Here’s the Python code, and it works as expected.

JavaScript

The output is

JavaScript

“4 45 46” correctly represents 0, and “4 9 24” correctly represents 200.

But the issue is, it stops working after I convert it to C code.

JavaScript

Now the output is

JavaScript

I don’t understand why for an input of 200, the C code gives me the wrong output, but the python code gives the correct output. I have a feeling it has something to do with the modulus, but I can’t figure it out. Please help! Thanks!

Advertisement

Answer

Problem is you’re using char types in the void largeNumToKeyboard(int num) function, and the values you’re testing can potentially overflow a char. You’ll need to change at least the first three to int

JavaScript

…then it prints the output…

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement