Functions :: Prototypes

Functions :: Prototypes

  • In the introduction, we touched on function prototypes. To recap, what are function prototypes? Function prototypes are abstract function interfaces. These function declarations have no bodies; they just have their interfaces.
  • Function prototypes are usually declared at the top of a C source file, or in a separate header file (see Appendix: Creating Libraries).
  • For example, if you wanted to grab command line parameters for your program, you would most likely use the function getopt. But since this function is not part of ANSI C, you must declare the function prototype, or you will get implicit declaration warnings when compiling with our flags. So you can simply prototype getopt(3) from the man pages:
      /* This section of our program is for Function Prototypes */
    int getopt(int argc, char * const argv[], const char *optstring);
    extern char *optarg;
    extern int optind, opterr, optopt;
    So if we declared this function prototype in our program, we would be telling the compiler explicitly what getopt returns and it's parameter list. What are those extern variables? Recall that extern creates a reference to variables across files, or in other words, it creates file global scope for those variables in that particular C source file. That way we can access these variables that getopt modifies directly. More on getopt on the next section about Input/Output.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post