Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:28:02+00:00 2026-06-13T21:28:02+00:00

Possible Duplicate: Learning C by K&R, error when trying to compile programs from book

  • 0

Possible Duplicate:
Learning C by K&R, error when trying to compile programs from book with arrays and function calls

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:

/* read a set of text lines and print the longest */

#include <stdio.h>
#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 < lim-1 && (c=getchar())!=EOF && 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)) > 0)
    if (len > max) { 
        max = len;
        copy(longest, line); 
    }
    if (max>0) /* there was a line */ 
        printf("%s", longest);
    return 0; 
}

There are two main errors:

  1. error: conflicting types for ‘getline’
  2. error: previous declaration of ‘getline’ was here

The complete error list is here:

/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

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:

int getline(char line[], int maxline); 
void copy(char to[], char from[]);

is problematic? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:28:03+00:00Added an answer on June 13, 2026 at 9:28 pm

    http://www.kernel.org/doc/man-pages/online/pages/man3/getline.3.html

    getline already exists in stdio.h. That is why you are getting the error. Change the function name to something else like getline_my.

    Also, you are comparing a character with a string in line 16. It should be

    if(c == '\n')

    NOT


    if(c == "\n")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to stop C++ console application from exiting immediately? So im learning
Possible Duplicate: Which IDE for Scala 2.8? I'm learning Scala by reading 'Programming in
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Struct initialization of the C/C++ programming language? I'm re-learning C and asking
Possible Duplicate: MVVM: Tutorial from start to finish? i just started learning WPF. i
Possible Duplicate: What is the colon operator in Ruby? While learning Ruby I've come
Possible Duplicate: Generating random integer from a range I just started learning C++ and
Possible Duplicate: Are Javascript arrays sparse? I am learning JavaScript at the moment and
Possible Duplicate: Workarounds for JavaScript parseInt octal bug I was learning the parseInt() function
Possible Duplicate: event handles & visible I am new to learning WebRequest.This is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.