1

I'm studying C with K&R book. There is an exercise, here it is: "Write a program to print all input lines that are longer than 80 characters". So I wrote this code:

#include <stdio.h>
#include <stdlib.h>

int getline(char s[], int lim);
#define MINLINE 80
#define MAXLINE 1000
/*
 * 
 */
int main(int argc, char** argv) {
    int len; //current line length
    char line[MAXLINE]; //current input line
    int proceed=0;

    while((len=getline(line, MAXLINE))>0) 
        if(line[len-1]!='\n'){
                printf("%s", line);
                proceed=1;}
        else if(proceed==1){
                printf("%s", line);
                proceed=0;}
        else if(len>MINLINE){
                printf("%s", line);
        }
        return 0;



}

int getline(char s[], int lim){
    int i, c;
    for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++){
        s[i]=c;
    }  
    if(c=='\n'){
        if(i<=lim-1){
        s[i]=c;}
    i++;}
    s[i]='\0';
    return i; 
}

I can't compile it and I have no idea how to fix it. Could you help me?

This is the error message:

main.c:11:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
                 from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c:38:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
                 from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c: In function ‘getline’:
main.c:40:5: error: too few arguments to function ‘getline’
main.c:38:5: note: declared here
make[2]: *** [build/Debug/Cygwin_4.x_1-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
1
  • 6
    The error messages are pretty clear... Change the name of your function getline Commented Sep 12, 2013 at 10:37

4 Answers 4

6

getline() function is already declared in stdio.h header file.if you want to redefine it in your file. just modify as my_getline()

In this for loop you need to use getchar() not getline()

for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++)   

for(i=0; i<lim-1 && (c=getchar())!='*' && c!='\n'; i++)  

You need to use pointer in your function to get the input into line.other wise s is become local to the function.

int my_getline(char *, int); //declaration   

int my_getline(char *s, int lim) //defination
{
//....
}

function call is same

len= my_getline(line, MAXLINE)

Finally use some conditional mechanism to get out of while loop in the main.

Sign up to request clarification or add additional context in comments.

1 Comment

There are already 3 answers stating this, and it should be stdio.h and not stdlib.h
3

The function getline is already declared in stdio.h. Rename your function to something else.

Comments

3

getline is a function of the C standard library defined in stdio.h. The compiler want to use that version instead of yours.

Rename your function, for instance into my_getline and you should be fine.

Comments

1

Change the function name, getline exists already

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.