www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/12/01:46:11

From: "DM" <dragonmagikus AT no DOT no>
Newsgroups: comp.os.msdos.djgpp
Subject: A very basic question about C programming... diary of a newbie Part 1
Lines: 83
Message-ID: <SS9A1.434$vq2.881313@newse2.tampabay.rr.com>
Date: Wed, 12 Aug 1998 01:47:12 -0000
NNTP-Posting-Host: 24.92.217.33
NNTP-Posting-Date: Wed, 12 Aug 1998 01:37:54 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello,

I have an excellent tutorial on C that is helping me learn C very well, but
I can't seem to understand its definition of "prototyping".

Here is a clip from my tutorial:

"WHAT IS PROTOTYPING?

A prototype is a model of a real thing and when programming in ANSI-C, you
have the ability to define a model of each function for the compiler. The
compiler can then use the model to check each of your calls to the function
and determine if you have used the correct number of arguments in the
function call and if they are of the correct type. By using prototypes, you
let the compiler do some additional error checking for you. "

I understand the basic idea portrayed... here is more that confuses me:

"Returning to lines 3, 4, and 5 in SCOPE.C, we have the prototypes for the
three functions contained within the program. The first void in each line
tells the compiler that these particular functions do not return a value, so
that the compiler would flag the statement index = head1(); as an error
because nothing is returned to assign to the variable named index. The word
void within the parentheses tells the compiler that this function requires
no parameters and if a variable were included, it would be an error and the
compiler would issue a warning message. If you wrote the statement
head1(index);, it would be a error. This allows you to use type checking
when programming in C"

Here is a clip of the program called SCOPE.C that the article is refering
to:
                                /* Chapter 5 - Program 4 - SCOPE.C */
#include <stdio.h>      /* Prototypes for Input/Output             */
void head1(void);       /* Prototype for head1                     */
void head2(void);       /* Prototype for head2                     */
void head3(void);       /* Prototype for head3                     */

int count;              /* This is a global variable               */

int main()
{
register int index; /* This variable is available only in main */

   head1();
   head2();
   head3();
                      /* main "for" loop of this program */
   for (index = 8 ; index > 0 ; index--)
   {
      int stuff;      /* This var is only available in these braces*/
      for (stuff = 0 ; stuff <= 6 ; stuff++)
         printf("%d ", stuff);
      printf(" index is now %d\n", index);
   }

   return 0;
}

So my question is... writing the word "void" before a function is how you
prototype???

Or, if you "declare" the function before the main() is that prototyping?

If the void is it, what if the function has to return a value? Can you not
prototype it then?

I have the general idea, but I think the exact definition of exactly what to
type is slighty vague in the tutorial.

Can anyone enlighten me with a good definition of prototyping?

- DM











- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019