Binary to decimal conversion C code - Problem in satisfying particular test cases

后端 未结 1 1432
温柔的废话
温柔的废话 2021-01-29 15:55

C program to convert input binary number to decimal number

The code works fine for the input 10001000, 101100 for which the outputs are 136 and 44 respectively, but it f

1条回答
  •  無奈伤痛
    2021-01-29 16:09

    Among several other issues with your code - you're trying to interpret the string "11111111111" (11 times '1') as an integer. However, the integer type on your machine uses 4 bytes, and the highest number it can represent is 2^31 - 1. The number 11,111,111,111 is higher than 2^33. So - you get signed integer overflow behavior.

    Try parsing your input as a string, not as a huge number...

    But - next time, please:

    1. Use proper indentation.
    2. Use meaningful variable names (e.g. number_of_conversions, not t).
    3. Use a minimal example. For example, we didn't need to have the external loop over t - you could have demonstrated your issue with just a single conversion.
    4. Check the results of your library calls! scanf() can fail, you know.

    0 讨论(0)
提交回复
热议问题