If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167 http://counter.li.org 13 1460
DevarajA ha scritto: If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes?
I've just read that they're used for type checking when calling external
functions. Is that true?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167 http://counter.li.org
"DevarajA" <no@spam.com> wrote in message
news:Om******** *************@n ews4.tin.it... DevarajA ha scritto: If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes?
I've just read that they're used for type checking when calling external functions. Is that true?
The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.
osmium ha scritto: "DevarajA" <no@spam.com> wrote in message news:Om******** *************@n ews4.tin.it...
DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes?
I've just read that they're used for type checking when calling external functions. Is that true?
The thing described by the OP (who,oddly enough, has the same nym as you) is not an external function. He said it was *visible*. C does compiling in a forward direction and it must know the argument types when it compiles. So if you do the physical definition prior to the first usage you don't need a prototype. This is sometimes called "Pascal style". The prototypes for external functions are usually in a visible header file.
so why this works and only gives "implicit declaration" warnings?
int main()
{
a(8,9);
b(9,9.5);
c(9.5,10);
return 0;
}
int a(int x, int y){return 0;}
int b(int x, double y){return 0;}
int c(double x, int y){return 0;}
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167 http://counter.li.org
"DevarajA" writes: so why this works and only gives "implicit declaration" warnings?
int main() { a(8,9); b(9,9.5); c(9.5,10); return 0; } int a(int x, int y){return 0;} int b(int x, double y){return 0;} int c(double x, int y){return 0;}
There was a time when something called K&R C was a more or less de facto
standard. Before there was a real standard. It assumed that everything was
int. Your compiler is in a mode such that it accepts the old dialect.
osmium ha scritto: "DevarajA" writes:
so why this works and only gives "implicit declaration" warnings?
int main() { a(8,9); b(9,9.5); c(9.5,10); return 0; } int a(int x, int y){return 0;} int b(int x, double y){return 0;} int c(double x, int y){return 0;}
There was a time when something called K&R C was a more or less de facto standard. Before there was a real standard. It assumed that everything was int. Your compiler is in a mode such that it accepts the old dialect.
So for that old standard a call to a undeclared function f1 is an
implicit declaration (as int f1(...))?
--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167 http://counter.li.org
DevarajA wrote: If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes? -- Devaraja (Xdevaraja87^gm ail^c0mX) Linux Registerd User #338167 http://counter.li.org
You need to have a prototype declaration in scope if the function is
being called before it has been defined. If you have all your
functions in the same file, and you define each function before it is
used, you don't need a prototype declaration.
Example:
double foo(int x, float y)
{
...
}
char *bar(void)
{
...
double g = foo(1, 2.0);
}
int main(void)
{
char *msg = bar();
...
}
Since the functions are defined (using prototype syntax) before they
are called, the compiler is able to verify that the return types and
the number and types of arguments match between the definition and
call.
Now, suppose the order were reversed:
int main (void)
{
char *msg = bar();
...
}
char *bar(void)
{
double g = foo(1, 2.0);
...
}
double foo(int x, float y);
{
...
}
When the compiler sees the call to bar(), it has no prior definition or
declaration of bar in scope, so it assumes bar() returns int, and does
no parameter checking. In this case, the compiler should issue a
diagnostic since it thinks you're trying to assign an int value to a
char *. Similarly, when the compiler first sees the call to foo(), it
assumes that foo() returns int, does no checking on the parameters, and
applies the default type promotions (IIRC, without a definition or
declaration in scope, 2.0 will be passed as a double instead of a
float). Again, you'll get a warning about trying to assign an int to a
double.
osmium wrote: "DevarajA" <no@spam.com> wrote in message news:Om******** *************@n ews4.tin.it...
DevarajA ha scritto:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes?
I've just read that they're used for type checking when calling external functions. Is that true?
The thing described by the OP (who,oddly enough, has the same nym as you) is not an external function. He said it was *visible*. C does compiling in a forward direction and it must know the argument types when it compiles. So if you do the physical definition prior to the first usage you don't need a prototype. This is sometimes called "Pascal style". The prototypes for external functions are usually in a visible header file.
There's some evidence of confusion here, so to clear
the air: A prototype is the parenthesis-enclosed list of
types or type/argument pairs that appears in a function
declaration. It is possible to declare a function with
a prototype
double func(double, int, const char*);
or without a prototype
double func();
Equally, it is possible to define a function with a
prototype
double func(double x, int i, const char *s) {
return x + i * strlen(s);
}
or without a prototype
double func(x, i, s)
double x;
int i;
const char *s;
{
return x + i * strlen(s);
}
DevarajA and osmium seem to be saying "prototype" when
what they actually mean is "declaratio n." osmium's point
is that a function definition is also a declaration (though
the opposite is clearly not true), and is really not about
prototypes at all.
-- Er*********@sun .com
John Bode wrote: DevarajA wrote:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes? -- Devaraja (Xdevaraja87^gm ail^c0mX) Linux Registerd User #338167 http://counter.li.org
You need to have a prototype declaration in scope if the function is being called before it has been defined. [...]
No: You need a declaration, but the declaration is not
required to provide a prototype. (Under C90 rules, even
the declaration is sometimes optional.)
Prototypes are a Good Thing; I cannot think of a case
where a prototype-less declaration wouldn't be improved by
adding a prototype. However, the only time a prototype is
actually required is when the function has a variable-length
argument list. All fixed-length prototypes can be omitted,
if you've got masochistic tendencies and are willing to suffer
opprobrium from others who work with your awful code.
-- Er*********@sun .com
Eric Sosman wrote: John Bode wrote: DevarajA wrote:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes? -- Devaraja (Xdevaraja87^gm ail^c0mX) Linux Registerd User #338167 http://counter.li.org
You need to have a prototype declaration in scope if the function is being called before it has been defined. [...]
No: You need a declaration, but the declaration is not required to provide a prototype. (Under C90 rules, even the declaration is sometimes optional.)
Prototypes are a Good Thing; I cannot think of a case where a prototype-less declaration wouldn't be improved by adding a prototype. However, the only time a prototype is actually required is when the function has a variable-length argument list. All fixed-length prototypes can be omitted, if you've got masochistic tendencies and are willing to suffer opprobrium from others who work with your awful code.
-- Er*********@sun .com
You're right. That first sentence should be amended to, "you need to
have a declaration in scope (preferably using prototype syntax in favor
of the old K&R-style syntax) ..." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David MacQuigg |
last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating
classes in Python. I'm trying to figure out what fundamental benefit
there is to having classes. Is all this complexity unecessary?
Here is an example of a Python class with all three types of methods
(instance, static, and class methods).
# Example from Ch.23, p.381-2 of Learning Python, 2nd ed.
class Multi:
|
by: Michele Simionato |
last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self
or other prototype-based languages. Still, from the discussion on the
mailing list, I have got the strong impression that you do not actually
need to fork Python in order to implement prototypes. It seems to me
that Python metaclasses + descriptors are more than powerful enough to
implementing prototypes in pure Python.
I wrote a module that implements part of what...
|
by: fb |
last post by:
Does the C language require you to prototype functions? If it's not
required, is it recommended?
|
by: Grumble |
last post by:
Hello everyone,
I've come across some strange code. Here it is, stripped down:
int main(void)
{
int *foo;
int *bar();
foo = bar(0);
return 0;
|
by: junky_fellow |
last post by:
Can a function have two different prototypes ? If not , then how can
main() have
two different prototypes ?
int main(void)
and
int main argc(int argc, char *argv)
I mean to say, if I declare main in either of the above mentioned ways
my compiler
does not give any warning. How can it accept two different prototypes
?
| |
by: petermichaux |
last post by:
Hi,
I have searched the archives but didn't find the questions and answers
I am looking for.
I have been looking at Prototype.js quite a bit lately as I need to
create a very small library of similar functionality to a subset of
Prototype.js. This is for use with Ruby on Rails.
About Prototype.js Rob G wrote:
|
by: Ari Krupnik |
last post by:
scripts can add methods to the prototypes of builtin objects in
JaavScript. I can assign functions to String.prototype.*, for
instance. I want to add a method to Node, but when I try to execute
the following IE says "'Node' is undefined." Mozilla works as I
expected it to. Is Node called something else in IE? Does IE not allow
manipulating the prototypes of some builtin objects?
Node.prototype.nt = function() {
return this.nodeType;
}
|
by: Steph Barklay |
last post by:
Hi, I'm currently taking a data structures course in C, and my teacher
said that function prototypes are not allowed in any of our code. He
also said that no professional programmers use function prototypes. This
kind of bugged me, because from other people's code that I've seen in
the past, almost all of them use function prototypes. The following also
bugged me. Let's say you have a file called main.c with only the main
function, and...
|
by: robtyketto |
last post by:
Greetings,
I’m writing a research report on justifying disposable prototypes.
The problem being there is a wealth of material regarding failures in the IT press and some of these put partial blame on prototypes.
There are lots of books and website promoting the use of prototypes and all state the wonders of prototyping.
However I can’t find (on initial googling session of a few hours) a single whitepaper, new item, statistics or...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |