Thread: K&R pg. 30 - Why are `getline` and `copy` declared twice?

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    2

    Question K&R pg. 30 - Why are `getline` and `copy` declared twice?

    My question is regarding the program on pg. 29 (Section 1.9, Chapter 1) of The ANSI C Programming Language by Kernighan and Ritchie:

    Code:
    #include <stdio.h>
    #define MAXLINE 1000 /* maximum input line length */
    
    int getline(char line[], int maxline);                   // FIRST
    void copy(char to[], char from[]);                       // FIRST
    
    /* print the 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) // SECOND {
    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[]) // SECOND {
    int i; i = 0; while ((to[i] = from[i]) != '\0')
    ++i;
    }
    The program makes use of two helper functions, `getline` and `copy`, which are declared twice - once at the beginning of the program and again after `main`, when they are defined. (The same thing can also be seen in an alternative version of the program in Section 1.10.)

    What is the purpose of this sort of redeclaration? I'm new to C and this my first reading of the chapter, but it looks like the first declaration is just meant to serve as a sort of documentation for the benefit of the reader, is this correct? When might it be used in practice?


    I haven't found anything about redeclarations in my searches (I'm not even sure if redeclaration is the correct word to describe what's going on here), so would be grateful for any insights from the community.
    Last edited by mmerc; 10-20-2022 at 06:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,651
    The first pair are function prototypes (also called declarations).
    The purpose is to inform the compiler that at some point, there will be a function called getline defined.

    The second pair are the actual function definitions, and these tell the compiler exactly what the function is supposed to do.

    Declaration and definition have two different and specific meanings in C programming.

    If you were to look in stdio.h (it's just a text file), you would see the function prototype for say printf.
    Along with a whole bunch of other standard IO related functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    2
    Thank you I remember "prototypes" being mentioned in Section 1.7 Functions but didn't notice they were separate from the function definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-20-2013, 01:34 PM
  2. Replies: 2
    Last Post: 10-13-2013, 07:36 PM
  3. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  4. getline and copy funcitons
    By guilhermeptrr in forum C Programming
    Replies: 1
    Last Post: 04-01-2011, 03:50 AM
  5. Replies: 3
    Last Post: 10-12-2006, 06:58 AM

Tags for this Thread