2

i am reading the C program book written by Denis and i was practicing his examples. I tried this example on my own and then copy pasted the same example from book. I get the followin error.

Code :

#include <stdio.h>
#define MAXLINE 1000    /* maximum input line size */

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

/* print longest input line */
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;
}

/* getline:  read a line into 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′;
    return i;
}

/* copy:  copy ′from′ into ′to′; assume to is big enough */
void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != ′\0′)
        ++i;
}

Error:

ex16.c:4:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
    ^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
        ^
ex16.c:8:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
ex16.c:16:40: error: too few arguments to function call, expected 3, have 2
    while ((len = getline(line, MAXLINE)) > 0)
                  ~~~~~~~              ^
/usr/include/stdio.h:440:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
^
ex16.c:27:5: error: conflicting types for 'getline'
int getline(char s[], int lim)
    ^
/usr/include/stdio.h:440:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_...
        ^
1 warning and 3 errors generated.

I am using function prototype correctly only i guess. Refered other internet sources also. I am not sure if its because of compiler. I am using Gcc version 4.3 i guess. OS - mac maverics.

Can you please help me ?

Thanks.

5
  • yea its there already right ? function prototype is to pre declare what functions we will be using. Can you please share it with an example ? please. Commented May 27, 2014 at 6:11
  • you mean "getline" is already declared in stdio.h ? hmm let me try renaming it. Commented May 27, 2014 at 6:14
  • 1
    Thanks . I never knew that getline was already declared in the stdio.h. THanks a lot. It solves the problem. Commented May 27, 2014 at 6:15
  • @Bala No, there is no such function in stdio.h. See my answer. Commented May 27, 2014 at 7:04
  • @Bala use -std=c89 for programs from this book. Commented May 27, 2014 at 9:03

2 Answers 2

4

Just call your function getlineMy() in the places where you declare, define and use it.

getline() is already declared in stdio.h (link) and its implementation will be linked to your program so you cannot use that name unless you do tricks.

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

6 Comments

There is no such function in stdio.h and the compiler isn't allowed to add one. If it does, it is not conforming to the C standard.
Also your reference is irrelevant. You should cite the C standard, which you can't, because there is no such function in it.
@Lundin if my understanding of section 4.6 of the C standard is correct, an implementation containing getline in stdio.h can still qualify as a "conforming freestanding implementation" (provided it respects C semantics everywhere else of course).
@Virgile As discussed here, the implementation isn't allowed to define any identifiers except those explicitly listed by the standard. If it adds non-standard extensions, it must use an underscore prefix. As we can see from this example, the compiler's non-standard getline function affects the behavior of a strictly conforming program, so the compiler is not C compliant. (Most likely compiled without the gcc -std option).
@Lundin, yes but a freestanding implementation (as opposed to an "hosted" one) needs only to provide a (very restricted) subset of the standard headers, and stdio.h is not part of this subset. I'd thus tend to say that such an implementation can do whatever it pleases it with stdio.h (except that POSIX compliance probably implies that stdio.h is a strict superset of what is stated in C standard itself).
|
3

You have to set GCC to compile your code as C.

gcc -std=c99 -pedantic-errors -Wall

You will then get one error:

8:1: error: return type defaults to 'int'

This is because you are using the wrong definition for main(). Correct it to int main().

The name getline is just fine to use, there is no such function in stdio.h and the compiler is not allowed to add functions that are non-standard extensions inside that header, without naming them with a _ prefix.

4 Comments

Since he is compiling code from K&R2, -std=c89 would be the preferred option
@MattMcNabb C89/C90 has been obsolete for 15 years. And there is no reason to use a 25 years old standard, unless perhaps if you are using a 25 years old computer.
He is compiling code which was written against the C89 standard.
@MattMcNabb Yes, I am aware. Still there is no reason to do so, and they shouldn't be learning C by studying obsolete books. The only difference between C90 and C99 for this case is the explicit int of main(). But the primary issue regarding stdio.h and getline doesn't depend on C standard version.

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.