0

Why is this program not printing all wanted values?

I have coded this program that gets a number and prints it in base 2. https://www.sololearn.com/compiler-playground/c202dhtnyFTV But I have two problems. 1.when I run the *.exe file on my PC, the program doesn't properly work for some numbers like 77, but when I input 77 in an online compiler like Sololearn, it has no problem and works properly. The only message that CMD turns out after not printing the number in base 2 is this : Process exited after 2.492 seconds with return value 3221225477 While the return value should be 0. 2. When I enter big numbers like 785, the output is half of the expected value, like : what program returns : 00010001 what it should returns : 1100010001 I tried to solve this problem by writing "long int" instead of "int", but the result became worse : 0001 Could you please help me to solve these two problems? Thanks in advance.

3rd Dec 2022, 6:55 PM
Root
Root - avatar
3 Antworten
+ 2
The Geek you're welcome. Since the input value is an int type, the size of x[] should be made to match the number of bits in an int. That number depends on the compiler, as int has no standard size in C. So here is how you can let the compiler determine the correct size: int x[8*sizeof n];
3rd Dec 2022, 11:03 PM
Brian
Brian - avatar
+ 1
Here is the working link for app users: https://code.sololearn.com/c202dhtnyFTV/?ref=app The problem I found was that the x[] array is getting declared to size 1. Increase the initial value of i from 1 to at least 8. Otherwise the code is writing out of bounds of the array and breaking things.
3rd Dec 2022, 7:19 PM
Brian
Brian - avatar
+ 1
Brian Thannnnnk you so much. both problems are solved. It does work now.
3rd Dec 2022, 7:32 PM
Root
Root - avatar