title: previous declaration of 'function' was here in C
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8763052/learning-c-by-kr-error-when-trying-to-compile-programs-from-book-with-arrays-a">Learning C by K&amp;R, error when trying to compile programs from book with arrays and function calls</a>  </p>
  </blockquote>
  
  
  
  <p>While learning The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie, I tried the example in section 1.9 Character Arrays. Here are the codes:</p>
  
  <pre><code>/* read a set of text lines and print the longest */
  
  #include &lt;stdio.h&gt;
  #define MAXLINE 1000 /* maximum input line length */
  
  /* declare functions: getline() and copy() */
  int getline(char line[], int maxline); 
  void copy(char to[], char from[]);
  
  /* getline: read a line into array "s", return length */ 
  int getline(char s[], int lim)
  {
      int c, i;
      for (i=0; i &lt; lim-1 &amp;&amp; (c=getchar())!=EOF &amp;&amp; c!='\n'; ++i)
          s[i] = c;
      if (c == "\n"){
          s[i] = c;
          ++i;
      }
      s[i] = '\0';  /* the null character whose value is 0 */
      return i;
  }
  
  /* copy: copy 'from' into 'to'; assume to is big enough */ 
  /* the return type of copy is "void" -- no value is returned */
  void copy(char to[], char from[])
  {
      int i;
      i = 0;
      while ((to[i] = from[i]) != '\0')  /* terminated with a \0 */
          ++i; 
  }
  
  /* print the longest input line */
  int main()
  {
      int len;  /* current line length */ 
      int max;  /* maximum length seen so far */
      char line[MAXLINE];  /* current input line */
      char longest[MAXLINE];  /* longest line saved here */
  
      max = 0;
      while ((len = getline(line, MAXLINE)) &gt; 0)
      if (len &gt; max) { 
          max = len;
          copy(longest, line); 
      }
      if (max&gt;0) /* there was a line */ 
          printf("%s", longest);
      return 0; 
  }
  </code></pre>
  
  <p>There are two main errors:</p>
  
  <ol>
  <li>error: conflicting types for ‘getline’</li>
  <li>error: previous declaration of ‘getline’ was here</li>
  </ol>
  
  <p>The complete error list is here:</p>
  
  <pre><code>/Users/C/Codes/Ritchie/array_char.c:8: error: conflicting types for ‘getline’
  /usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
  /Users/C/Codes/Ritchie/array_char.c:13: error: conflicting types for ‘getline’
  /usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
  /Users/C/Codes/Ritchie/array_char.c: In function ‘getline’:
  /Users//C/Codes/Ritchie/array_char.c:17: warning: comparison between pointer and integer
  /Users/C/Codes/Ritchie/array_char.c:17: warning: comparison with string literal results in unspecified behavior
  </code></pre>
  
  <p>I am not sure what went wrong, since it is exactly the same code from the book. Maybe the declaration of functions at the beginning:</p>
  
  <pre><code>int getline(char line[], int maxline); 
  void copy(char to[], char from[]);
  </code></pre>
  
  <p>is problematic? Thank you!</p>