Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Prototype a Function in C Programming

What happens when you don’t prototype? As with anything in C programming, when you goof up, the compiler or linker lets you know with an error message — or the program just doesn’t run properly. It’s not the end of the world — no, not like programming a military robot or designing genetic code for a new species of Venus flytrap.

BASIC FUNCTION; NO RETURN

#include <stdio.h>
void prompt(); /* function prototype */
int main()
{
 int loop;
 char input[32];
 loop=0;
 while(loop<5)
 {
 prompt();
 fgets(input,31,stdin);
 loop=loop+1;
 }
 return(0);
}
/* Display prompt */
void prompt()
{
 printf("C:\\DOS> ");
}

Exercise 1: Modify the source code from Basic Function; No Return. Comment out the prototype from Line 3. Build the result.

Compiler errors are wonderful things, delightfully accurate yet entirely cryptic. Here is the error message generated by Code::Blocks, although only the relevant parts of the message:

13 Warning: implicit declaration of function 'prompt'
23 Warning: conflicting types for 'prompt'
13 Warning: previous implicit declaration of 'prompt' was here

The first warning occurs at Line 13 in the source code file, where the prompt() function is used inside the main() function. The compiler is telling you that you’re using a function without a prototype. As the error message says, you’re implicitly declaring a function. That’s a no-no, but not a full-on error.

The second warning occurs where the prompt() function dwells in the program. In the source code, it’s at Line 23. The warning states that prompt() was already declared (at Line 11) and that the second use may conflict with the first.

The final warning is a reference back to where the function was called, again at Line 13.

To put it succinctly: The compiler has no idea what’s up with the prompt() function. Your code compiles, but running it is risky.

You may draw the conclusion that prototyping is an absolute necessity in your C code. That’s not entirely true. You can avoid prototyping by reordering the functions in your source code. As long as a function is listed before it’s used, you don’t need a prototype.

Exercise 2: Edit your source code from Exercise 10-3. Remove the function prototype that was commented out at Line 3. Cut and paste (move) the prompt() function from the bottom of the source code Listing to the top, above the main() function. Save, build, and run.

Avoiding the Function Prototype shows the solution for Exercise 2.

AVOIDING THE FUNCTION PROTOTYPE

#include <stdio.h>
/* Display prompt */
void prompt(void)
{
 printf("C:\\DOS> ");
}
int main()
{
 int loop;
 char input[32];
 loop=0;
 while(loop<5)
 {
 prompt();
 fgets(input,31,stdin);
 loop=loop+1;
 }
 return(0);
}

Writing the main() function first, followed by other functions allows for better readability, although you’re free to put your own functions first to avoid prototyping. And if you don’t, keep in mind that other programmers may do it that way, so don’t be surprised when you see it.

Compiler error messages in Code::Blocks have parentheses after them. The parenthetical comments refer to the switch, or traditional command-line option, that enables checking for a particular warning. For example, the error messages from Exercise 1 read in full:

11 Warning: implicit declaration of function 'prompt' (-Wimplicit-function-declaration)
20 Warning: conflicting types for 'prompt' (enabled by default)
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.