4

I've taken a Java course and am trying to teach myself C with K&R. So far so good but I don't understand the purpose of prototypes. See the 2 // comments in the code below:

#include <stdio.h>

float convert(int); **//Why is this needed...**

main()
{
    int i;

    for(i = 0; i <= 300; i += 20)
        printf("F: %3d C: %6.1f\n",i,convert(i));

    system("Pause");
    return 0;
}

float convert(int f) **//When we already have this?**
{
    float c = (5.0/9.0) * (f-32.0);
    return c;
}

In Java, you'd declare a function something like public static float convert(int f) and not need a prototype at all. That seems much simpler to me. Why the difference?

ericgrosse
  • 1,490
  • 20
  • 37

4 Answers4

5

This is essentially a decision made for a language system.

Note that both the Java and C compilers need to know the function signature so that they can do type checking, and compile the code.

Also note that languages like C need you to supply this signature / prototype separately (in a declaration), when in fact the function definition has the exact same information. So, it is basically a repetition of the information. Why is this? Essentially, this is so that code can be compiled in the absence of the actual source code that contains the definition. So, if a library is supplied as binary code, then having the headers which contain the prototypes is enough to allow the compilation of other code that uses the code from the library.

More modern languages like Java and C# do away with the need to repeat this prototype information. How do they then compile code, when they do need the prototype? What they do is store the prototype information along with the binary code, at the time they process the definition. So, really, the prototype information is just auto generated by the compiler itself.

Ziffusion
  • 8,779
  • 4
  • 29
  • 57
1

The Java compiler can find a class via it's name and package and check the source directly. Note: if the Java compiler cannot do this it won't compile.

In C, there is no restriction on what definitions you can place where and so you have to first let it know what you might be defining later.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

In C an identifier usually has to be declared before it can be used. A function prototype serves as a declaration for a function. C is an old language and forcing the programmer to declare function identifiers helped the programming of the compiler / linker, particularly when functions are used and defined in different translation units.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

Both C and Java check at compile time that the function call matches the function signature.

A C compiler always relies on a function declaration/prototype in source code. The function declaration must appear before the call.

A Java compiler may obtain the function declaration from:

  • Anywhere in the same top-level class. The function definition does not have to be placed above all calls.
  • From another's class's source code or a compiled *.class file. The fully qualified class name allows the *.class file to be found in the classpath.
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151