2

In a book, I saw the following code. I do not think the 19th line is necessary. It is possible to compile without the line. What is the roll of char *tmp_name(void) ? If it is a function prototype, I do not understand why it is there.

#include <stdio.h>
#include <string.h>

char *tmp_name(void)  {
    static char name[30];
    static int sequence = 0;

    ++sequence;

    strcpy(name, "tmp");

    name[3] = sequence + '0';
    name[4] = '\0';

    return(name);
}

int main(void) {
    char *tmp_name(void);

    printf("Name: %s\n", tmp_name());

    return(0);
}
q2ven
  • 187
  • 9
  • yes, it's redundant. Perhaps the body of the future functions is assumed to be moved to a separate file. – BLUEPIXY Jun 11 '15 at 04:44
  • @BLUEPIXY: perhaps, but then the correct approach is to create a header to hold the function declaration, and to include that header in both source files — thus ensuring cross-file consistency. There are essentially no circumstances under which it is good style to declare a function within the scope of another. – Jonathan Leffler Jun 11 '15 at 05:39
  • @JonathanLeffler i agree. – BLUEPIXY Jun 11 '15 at 05:41

0 Answers0