Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a little experimentation, I think that your program is intended to accept a number consisting of 1's and 0's only as a base-10 number (the <code>%d</code> reads a decimal number). For example, given input <code>10</code>, it outputs <code>2</code>; given <code>1010</code>, it outputs <code>10</code>; given <code>10111001</code>, it outputs <code>185</code>.</p> <p>So far, so good. Unfortunately, given <code>1234</code>, it outputs <code>15</code>, which is a little unexpected.</p> <p>If you are running on a machine where <code>int</code> is a 32-bit signed value, then you can't enter a number with more than 10 digits, because you overflow the limit of a 32-bit <code>int</code> (which can handle ±2 billion, in round terms). The <code>scanf()</code> function doesn't handle overflows well.</p> <p>You could help yourself by echoing your inputs; this is a standard debugging technique. Make sure the computer got the value you are expecting.</p> <p>I'm not going to attempt to fix the code because I think you're going about the problem in completely the wrong way. (I'm not even sure whether it's best described as binary to decimal, or decimal to binary, or decimal to binary to decimal!) You would do better to read the input as a string of (up to 31) characters, then validate that each one is either a 0 or a 1. Assuming that's correct, then you can process the string very straight-forwardly to generate a value which can be formatted by <code>printf()</code> as a decimal.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload