Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a good chance that you're doing this to a string that you have obtained with <code>fgets</code> or a similar input function. In that case, it may well have the newline at the end still.</p> <p>If you change your code temporarily to:</p> <pre><code>void xyz (char *number) { int i = 0, length = strlen (number); while (i &lt; length) printf ("Number[%d]: %c (%d)", i, number[i], number[i]); i++; } } </code></pre> <p>that should also show the numeric codes for all characters.</p> <p>The problem with encoding something like that <code>- 2</code> in your function is that it will not work with:</p> <pre><code>xyz ("123"); </code></pre> <p>since it will stop early, printing out only <code>12</code>. The <em>caller</em> should be calling with valid data, meaning that it should adjust the value to be a numeric string before calling.</p> <hr> <p>You can see this happening in the following program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void xyz (char *number) { int i = 0, length = strlen(number) - 2; while(i &lt;= length) { printf("Number[%d]: %c (%d)\n",i, number[i], number[i]); i++; } puts ("==="); } void xyz2 (char *number) { int i = 0, length = strlen(number); while(i &lt; length) { printf("Number[%d]: %c (%d)\n",i, number[i], number[i]); i++; } puts ("==="); } int main (void) { char buff[100]; printf ("Enter number: "); fgets (buff, sizeof (buff), stdin); xyz (buff); xyz ("12345"); xyz2 (buff); xyz2 ("12345"); return 0; } </code></pre> <p>The (annoted) output of this, if you enter <code>98765</code>, is:</p> <pre><code>Enter number: 98765 Number[0]: 9 (57) Number[1]: 8 (56) Number[2]: 7 (55) # Your adjustment works here because of the newline. Number[3]: 6 (54) Number[4]: 5 (53) === Number[0]: 1 (49) Number[1]: 2 (50) Number[2]: 3 (51) # But not here, since it skips last character. Number[3]: 4 (52) === Number[0]: 9 (57) Number[1]: 8 (56) Number[2]: 7 (55) # Here you can see the newline (code 10). Number[3]: 6 (54) Number[4]: 5 (53) Number[5]: (10) === Number[0]: 1 (49) Number[1]: 2 (50) Number[2]: 3 (51) # And proper numeric strings work okay. Number[3]: 4 (52) Number[4]: 5 (53) === </code></pre> <hr> <p>If you're looking for a robust user input function that gets around this problem (and avoids dangerous things like unbounded <code>scanf("%s")</code> and <code>gets</code>), I have one elsewhere on SO (right <a href="https://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921">HERE</a>, in fact) drawn from my arsenal.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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