title: Char Array trucates itself(K&R1-16&1-17)
body: <p>In the following code, the char array prints up to 100 characters at location 1 and 2 but at location 3 it only prints 22. What is the reason for this behavior?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  /* print the longest input line */
  /*Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.*/
  
  #define MAXLENGTH 100
  
  int mygetline(char s[], int limit);
  void charcopy(char to[], char from[]);
  
  int main(){
    char current[MAXLENGTH];
    char longest[MAXLENGTH];
    int curlen;
    int maxlen;
  
    maxlen = 0;
    while( (curlen = mygetline(current, MAXLENGTH)) &gt; 0 ){
      if (curlen &gt; 80)
        printf("\nvery long:%d; %s\n", curlen, current);//#1# prints 100 digits
      if(curlen&gt;maxlen){
        maxlen=curlen;
        charcopy(longest, current);
        printf("\nlonger:%d; %s\n", maxlen, longest);//#2# prints 100 digits
      }
    }
    if (maxlen)//char array seems to truncates itself at scope boundry.
      printf("\nlongest:%d; %s\n", maxlen, longest);//#3# prints 22 digits
    printf("\nall done!\n");
    return 0;
  }
  
  int mygetline(char s[], int limit){
    int i, c;
    for(i=0; i &lt; limit-1 &amp;&amp; ((c=getchar()) != EOF) &amp;&amp; c != '\n'; ++i)
      s[i]=c;
    if(c=='\n'){
      s[i]=c;
      ++i;}
    else
      if(i &gt;= limit-1)
        while (((c=getchar()) != EOF) &amp;&amp; c != '\n')
      ++i;
    s[i]='\0';
    return i-1;
  }
  
  
  void charcopy(char to[], char from[]){
    int i;
    i=0;
    while( (to[i] = from[i])  != '\0'){
      ++i;}
  }
  </code></pre>
  
  <p>Its the location marked 3 in comment that prints only 22 characters instead of the full 100. Its very weird.</p>
  
  <p>Edit:
  As per Scotts answer, i have changed mygetline to this:</p>
  
  <pre><code>int mygetline(char s[], int limit){
    int i, c, k;
    for(i=0; i &lt; limit-1 &amp;&amp; ((c=getchar()) != EOF) &amp;&amp; c != '\n'; ++i)
      s[i]=c;
    if((c=='\n') &amp;&amp; (i &lt; limit -1)){
      s[i]=c;
      ++i;}
    else{//if we are over the limit, just store the num of char entered without storing chars
      k = 0;
      while (((c=getchar()) != EOF) &amp;&amp; c != '\n')
        ++k;}
    s[i]='\0';
    return i+k;
  }
  </code></pre>
  
  <p>As can be seen, if input overshoots limit, then num of characters entered is stored in entirely new variable, k which does not touch the array. I still get truncation of the last printed line and i get weird 32770 as line lengths.Why? As can be seen, array is being baby sat and coddled and fed just the precise amount of char and no more.</p>
  
  <p>Edit: The problem with the first listing was, as pointed out by Scott was that i was overshooting arrays. The problem with the second mygetline was that <code>k=0;</code> was initialized way way inside the if else nest. Moving the initialization upwards and making it global to the whole function, seems to solve the second issue.  </p>
  
  <p>working mygetline as follows:</p>
  
  <pre><code>int mygetline(char s[], int limit){
    int i, c, k;
    k=0;
    for(i=0; i &lt; limit-1 &amp;&amp; ((c=getchar()) != EOF) &amp;&amp; c != '\n'; ++i)
      s[i]=c;
    if((c=='\n') &amp;&amp; (i &lt; limit -1)){
      s[i]=c;
      ++i;}
    else{//if we are over the limit, just add the num of char entered without storing chars
      while (((c=getchar()) != EOF) &amp;&amp; c != '\n')
        ++k;}
    s[i]='\0';
    return i+k;
  }
  </code></pre>