简体   繁体   中英

Why is my C program not compiling?

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

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.

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

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.

更改函数名称,getline已经存在

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM