Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,501 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,364 people online right now. Registration is fast and FREE... Join Now!




what is the problem without prototype declaration in C?

 
Reply to this topicStart new topic

what is the problem without prototype declaration in C?

rodge
23 Mar, 2007 - 04:23 AM
Post #1

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 3


My Contributions
t.c:
CODE

/*
* Evaluate a function at three points, displaying results.
*/
void
evaluate(double f(double f_arg), double pt1, double pt2, double pt3)
{
      printf("f(%.5f) = %.5f\n", pt1, f(pt1));
      printf("f(%.5f) = %.5f\n", pt2, f(pt2));
      printf("f(%.5f) = %.5f\n", pt3, f(pt3));
}

ex.c

#include <stdio.h>
#include <math.h>

int main(void)
{
        evaluate(sin, 2,3,4);
        evaluate(sqrt,2,3,4);

        return(0);
}

complie:
$gcc -lm ex.c t.c
$./a.out
f(0.00000) = 0.00000
f(2.17493) = 0.82300
f(-1.99815) = -0.91007
f(0.00000) = 0.00000
f(2.17493) = 1.47476
f(-1.99815) = nan

if change ex.c, add the prototype of function evaluate

ex.c

#include <stdio.h>
#include <math.h>

void
evaluate(double f(double f_arg), double pt1, double pt2, double pt3);

int main(void)
{
        evaluate(sin, 2,3,4);
        evaluate(sqrt,2,3,4);

        return(0);
}

compile:
$gcc -lm ex.c t.c
$./a.out
f(2.00000) = 0.90930
f(3.00000) = 0.14112
f(4.00000) = -0.75680
f(2.00000) = 1.41421
f(3.00000) = 1.73205
f(4.00000) = 2.00000

Anything wrong in the first case?
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 04:42 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you're gonna have to describe the problem a little better, because right now i haven't a clue what this means, what you wanted the output to be or why this output is wrong.

*Use [code ][ /code] tags (without spaces) around your code, it makes it much easier to discern the code from the text.
User is offlineProfile CardPM
+Quote Post

rodge
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 04:54 AM
Post #3

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 3


My Contributions
QUOTE(William_Wilson @ 23 Mar, 2007 - 05:42 AM) *

you're gonna have to describe the problem a little better, because right now i haven't a clue what this means, what you wanted the output to be or why this output is wrong.

*Use [code ][ /code] tags (without spaces) around your code, it makes it much easier to discern the code from the text.


Ok, here's the explanation:
1. I have a function evaluate defined in t.c
2. in ex.c, I call function evaluate without prototype defined.
using gcc -lm ex.c t.c, I can generate the executable file a.out,
when executing, the result is wrong
3. if I modify ex.c, add the prototype definition, the result is correct.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 05:41 AM
Post #4

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Typically, the way the files are setup is to:

Have one file called something like myLibrary.h which includes the function definitions like "int MyFunction(double i);". This file also carries a few directives to keep from being called more than once. It has two lines at the top and one at the bottom:
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
...
#endif
A second file named something like "myLibrary.c" contains the actual functions. It also contains the line
#include "MyLibrary.h"

Last but not least the file like "main.c" contains the same statement at the top.
#include "MyLibrary.h"

So the way to compile this, with the Gnu Compiler, is to type:
gcc main.c MyLibrary.c -o ExecutableName


User is offlineProfile CardPM
+Quote Post

rodge
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 05:45 AM
Post #5

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 3


My Contributions
QUOTE(ajwsurfer @ 23 Mar, 2007 - 06:41 AM) *

Typically, the way the files are setup is to:

Have one file called something like myLibrary.h which includes the function definitions like "int MyFunction(double i);". This file also carries a few directives to keep from being called more than once. It has two lines at the top and one at the bottom:
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
...
#endif
A second file named something like "myLibrary.c" contains the actual functions. It also contains the line
#include "MyLibrary.h"

Last but not least the file like "main.c" contains the same statement at the top.
#include "MyLibrary.h"

So the way to compile this, with the Gnu Compiler, is to type:
gcc main.c MyLibrary.c -o ExecutableName




Thanks for the reply. I know that, I am just curious about the difference of adding or not adding prototype. In both cases, they can successfully compile and link, but the first case generate the wrong output. I want to know how gcc deal with it?


User is offlineProfile CardPM
+Quote Post

Trogdor
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 06:42 AM
Post #6

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
It might have something to do with the fact that you use not references to functions, but the functions them selves as an argument.
It might be that when prototyped the compiler does different things with this argument becouse it hase more information at compiletime.
But this is a wild guess.

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: What Is The Problem Without Prototype Declaration In C?
23 Mar, 2007 - 06:49 AM
Post #7

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
to be honest, i don't know, it must be gcc allowing it to work at all without the prototype and it is not passing the values properly, or with correct precision.
Most C compilers would not allow you compile without the prototype, with an error of the nature: "missing declaration evaluate, assumed an extern function"

If you were to use a header and include it is likely that you could go without the prototype as the code would not be considered external anymore.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month