Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Can function prototypes appear anywhere?

Reply
Thread Tools

Re: Can function prototypes appear anywhere?

 
 
Chris Dollin
Guest
Posts: n/a
 
      01-19-2005
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
 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      01-19-2005
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
 
Reply With Quote
 
 
 
 
Alan Balmer
Guest
Posts: n/a
 
      01-19-2005
On Wed, 19 Jan 2005 15:32:10 +0100, Grumble <>
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

 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      01-19-2005

In article <cslopn$hs0$>, Chris Dollin <> 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

Viewers are bugs for famous brands.
-- unknown subtitler, Jackie Chan's _Thunderbolt_
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function prototypes and function addresses Syren Baran C Programming 6 01-09-2008 06:34 PM
Some complex function prototypes like signal() lovecreatesbeauty C Programming 2 01-28-2005 07:53 PM
Re: Can function prototypes appear anywhere? Lawrence Kirby C Programming 2 01-24-2005 04:56 PM
order of const in function prototypes Denis Remezov C++ 7 06-30-2004 11:41 AM
New to Python; what about #include, extern and function prototypes Bo Jacobsen Python 6 03-07-2004 02:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57