473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Can function prototypes appear anywhere?

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;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype
which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?

I suppose the better way to write that code would be:

int *bar();
int main(void)
{
int *foo;
foo = bar(0);
return 0;
}

Would you agree?

--
Regards, Grumble
Nov 14 '05 #1
9 1890
Grumble wrote:
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;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype
which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?
No. They are declarations, and declarations can't appear "anywhere".
But they can appear at block start, even in C89.
I suppose the better way to write that code would be:

int *bar();
int main(void)
{
int *foo;
foo = bar(0);
return 0;
}

Would you agree?


I would agree, but that's a constraint of style, not language.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #2
Chris Dollin wrote:
Grumble wrote:
I've come across some strange code. Here it is, stripped down:

int main(void)
{
int *foo;
int *bar();
foo = bar(0);
return 0;
}

If I understand correctly, line 2 in main() is a function prototype
which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?


No. They are declarations, and declarations can't appear "anywhere".
But they can appear at block start, even in C89.
I suppose the better way to write that code would be:

int *bar();
int main(void)
{
int *foo;
foo = bar(0);
return 0;
}

Would you agree?


I would agree, but that's a constraint of style, not language.


In the first example, the function declaration only has block scope.

Thus, the following code is incorrect:

int main(void)
{
int *foo;
int *bar(int,int);
foo = bar(1,2);
return 0;
}

int *baz(void) { return bar(3,4); }

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O1 toto2.c
toto2.c: In function `baz':
toto2.c:9: warning: implicit declaration of function `bar'

Do people actually use local function declarations?

--
Regards, Grumble
Nov 14 '05 #3
On Wed, 19 Jan 2005 15:32:10 +0100, Grumble <de*****@kma.eu.org>
wrote:
Chris Dollin wrote:
Grumble wrote:
I've come across some strange code. Here it is, stripped down:


Do people actually use local function declarations?


Apparently they do, but don't ask me why. I have seen this often in a
particular body of legacy code I'm working with. A function handling a
file will often declare

FILE *fp, *fopen();

(That's before I clean them up, of course ;-)

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #4

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin <ke**@hpl.hp.com> writes:
Grumble wrote:
I've come across some strange code. Here it is, stripped down:

int main(void)
{
int *foo;
int *bar();
foo = bar(0);
return 0;
}

If I understand correctly, line 2 in main() is a function prototype
which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?


No. They are declarations, and declarations can't appear "anywhere".
But they can appear at block start, even in C89.


Not if they're declarations of functions with static linkage. See
ISO 9899-1990 6.5.1:

The declaration of an identifier for a function that has block
scope shall have no explicit storage-class specifier other than
extern.

(This is part of the semantics of storage-class specifiers, not a
constraint, so it's not required to produce a diagnostic.)

This means that you cannot prototype a static function at block
scope.

I agree with Chris that even for functions with external linkage,
declarations at block scope are a poor idea; they accomplish little
(in practice, it's rarely useful to restrict the scope of a function
identifier), and since the same can't be done for static-linkage
functions, you'd end up with inconsistent placement of prototypes,
which can't help readability.

--
Michael Wojcik mi************@microfocus.com

Viewers are bugs for famous brands.
-- unknown subtitler, Jackie Chan's _Thunderbolt_
Nov 14 '05 #5
On Wed, 19 Jan 2005 14:09:55 +0100, Grumble wrote:
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;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype
It is a declaration but it isn't a prototype. Prototypes are a later
addition to the C language derived from C++. In a prototype form of
declaration you specify type information within the ()'s of the parameter
list. So in your code above int main(void) is a prototype (definitions can
be prototypes) but int *bar(); isn't.

Lawrence


which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?

I suppose the better way to write that code would be:

int *bar();
int main(void)
{
int *foo;
foo = bar(0);
return 0;
}

Would you agree?


Nov 14 '05 #6
Lawrence Kirby wrote:
On Wed, 19 Jan 2005 14:09:55 +0100, Grumble wrote:
I've come across some strange code. Here it is, stripped down:

int main(void)
{
int *foo;
int *bar();
foo = bar(0);
return 0;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype


It is a declaration but it isn't a prototype. Prototypes are a later
addition to the C language derived from C++. In a prototype form of
declaration you specify type information within the ()'s of the parameter
list. So in your code above int main(void) is a prototype (definitions can
be prototypes) but int *bar(); isn't.


Point taken.

Would you write

int main(void)
{
int *foo;
int *bar(int);
foo = bar(0);
return 0;
}

or would you declare bar() outside main() ?

--
Regards, Grumble
Nov 14 '05 #7
Lawrence Kirby wrote:
On Wed, 19 Jan 2005 14:09:55 +0100, Grumble wrote:
I've come across some strange code. Here it is, stripped down:

int main(void)
{
int *foo;
int *bar();
foo = bar(0);
return 0;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype


It is a declaration but it isn't a prototype. Prototypes are a later
addition to the C language derived from C++. In a prototype form of
declaration you specify type information within the ()'s of the
parameter list. So in your code above int main(void) is a prototype
(definitions can be prototypes) but int *bar(); isn't.


gcc has prepared mini.o which is awaiting linking with a run time
library and something that contines the bar function. If you had
omitted the -c you would have gotten an error.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #8
On Thu, 20 Jan 2005 15:39:44 +0100, Grumble wrote:

....
Would you write

int main(void)
{
int *foo;
int *bar(int);
foo = bar(0);
return 0;
}

or would you declare bar() outside main() ?


Outside main(), either static if bar() is defined in the same source file
or in a header otherwise.

Lawrence

Nov 14 '05 #9
you can't declare function prototype any where in the code , just like
the variables are declared before their use in the same way function
prototype has to be in the beginnning only . both the prototypes are
correct but the difference is in their scope.
Grumble wrote:
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;
}

$ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
/* NO WARNING */

If I understand correctly, line 2 in main() is a function prototype
which declares 'bar' as a function taking an unspecified number of
parameters, and returning a pointer to int?

Can function prototypes really appear anywhere in the code?

I suppose the better way to write that code would be:

int *bar();
int main(void)
{
int *foo;
foo = bar(0);
return 0;
}

Would you agree?

--
Regards, Grumble


Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
7
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...
6
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: ...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
9
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same...
73
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...
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.