Scope/use of prototypes Qs

P

pemo

Decided to re-post this as a new /thread/.

My comments/question below came up via the 'Seg fault, please help!' post
recently made by BT.

BT wrote:

int main()
{
int DATASIZE = MAXSIZE;
int *narray;

void getdata(int *d, int size);
int largest(int *d, int size);

getdata(narray, DATASIZE);

exit(0);
}

<snip>

When I first looked at the code above, I initially 'mentally parsed' it as:

'Wow, he's declaring a variable as a parameter in a function call!'.

But, I then saw that this was a function prototype living in non
file-scope, i.e., not placed outside of any function definitions, but within
one.

It's quite unusal to see this, and I never do such a thing - but I think
that's more through habit, than reason, and maybe there's quite some sense
behind using this to restrict scope - if the scope of the prototype is
restricted to the block/function in which it is declared [which the std
seems to suggest is the case?], and, given that we don't yet have nested
functions in std c, it could be quite useful.

So, irrespective of what the current std says: time to hit the compiler and
see what it/they think! Here's some code then...

#include <stdio.h>

int main(void)
{
void func2(void);

{
void func1(void);

func1();
}

func2();

// See below - problems here?: not happy about func1().
//
func1();

return 0;;
}


void func1(void)
{
// See below - problems here?: not happy about func2().
//
func2();

return;
}


void func2(void)
{
return;
}

---

So, with a few different compilers ...


gcc [v4.0.2]:

[Warning] implicit declaration of func-1/2 (2 from func1(), 1 from main)

[Error] incompatible declaration of func-1/2 (2 from func1(), 1 from
main)

---

bcc 5 [v5.5] [Borland CBuilder]:

[Error] type mismatch of func-1/2

---

lcc [v3.8]

Warnings only

---

MSVC6 and MSVC7:

Compiles ok.


So, the compilers differ rather a lot in their opinions about this!

What do others think? Should there be warnings/errors? If 'yes' to either,
should 'style' [as this is often thought of as being 'bad style'] be
re-thought - seems to me that it could be a reasonable habit to adopt.

P.S. Would be interesting to hear what other compilers make of it too!
 
V

Vladimir S. Oka

pemo wrote:

said:
It's quite unusal to see this, and I never do such a thing - but I think
that's more through habit, than reason, and maybe there's quite some sense
behind using this to restrict scope - if the scope of the prototype is
restricted to the block/function in which it is declared [which the std
seems to suggest is the case?], and, given that we don't yet have nested
functions in std c, it could be quite useful.

So, irrespective of what the current std says: time to hit the compiler and
see what it/they think! Here's some code then...

#include <stdio.h>

int main(void)
{
void func2(void);

{
void func1(void);

func1();
}

func2();

// See below - problems here?: not happy about func1().
//
func1();

return 0;;
}


void func1(void)
{
// See below - problems here?: not happy about func2().
//
func2();

return;
}


void func2(void)
{
return;
}

---

So, with a few different compilers ...


gcc [v4.0.2]:

[Warning] implicit declaration of func-1/2 (2 from func1(), 1 from main)

[Error] incompatible declaration of func-1/2 (2 from func1(), 1 from
main)

---

bcc 5 [v5.5] [Borland CBuilder]:

[Error] type mismatch of func-1/2

---

lcc [v3.8]

Warnings only

---

MSVC6 and MSVC7:

Compiles ok.


So, the compilers differ rather a lot in their opinions about this!

What do others think? Should there be warnings/errors? If 'yes' to either,
should 'style' [as this is often thought of as being 'bad style'] be
re-thought - seems to me that it could be a reasonable habit to adopt.

P.S. Would be interesting to hear what other compilers make of it too!

gcc 3.4.2 (mingw): pedantic mode turned all the way up
warnings in both places about implicit function declaration

This agrees with what I, FWIW, think should happen.
 
M

Mark McIntyre

On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"

(of locally declared functions)
What do others think? Should there be warnings/errors?

Its a nonstandard extension, AFAIK. Make sure you compiled in ultra
pedantic ANSI mode. Distrust any compiler that doesn't consider this
an error in that mode.

Mark McIntyre
 
P

pemo

Mark said:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"

(of locally declared functions)


Its a nonstandard extension, AFAIK. Make sure you compiled in ultra
pedantic ANSI mode. Distrust any compiler that doesn't consider this
an error in that mode.

So, as a means of restricting the scope of a function that is in a
translation unit as a helper function to just a subset of the total number
of functions in the same tr, you'd consider it a useful thing?
 
M

Mark McIntyre

So, as a means of restricting the scope of a function that is in a
translation unit as a helper function to just a subset of the total number
of functions in the same tr, you'd consider it a useful thing?

How'd you figure that? I wouldn't consider it useful *at all*.
Mark McIntyre
 
C

CBFalconer

Mark said:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"

(of locally declared functions)


Its a nonstandard extension, AFAIK. Make sure you compiled in
ultra pedantic ANSI mode. Distrust any compiler that doesn't
consider this an error in that mode.

He's not using local functions, just declaring them in a local
scope. Thus it is not an extension at all. The only real question
is does the compiler limit the declaration scope as intended.
Without local functions it doesn't really matter how the compiler
treats it.

--
"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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Jordan Abel

On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"

(of locally declared functions)


Its a nonstandard extension, AFAIK.

No. You may be thinking of nested function _definitions_.
 
O

Old Wolf

Mark said:
(of locally declared functions)


Its a nonstandard extension, AFAIK. Make sure you compiled in ultra
pedantic ANSI mode. Distrust any compiler that doesn't consider this
an error in that mode.

In C89 no diagnostic is required if a function is called without a
prototype in scope; the compiler assumes that the function
returns an int and any arguments undergo default argument
promotions. If this doesn't match what the actual function is
expecting then you get UB when the function is called.
 
M

Michael Wojcik

He's not using local functions, just declaring them in a local
scope. Thus it is not an extension at all.

True. Note, however, that the "static" sc-specifier cannot be used
in this context. C99 6.7.1 #5: "The declaration of an identifier for
a function that has block scope shall have no explicit storage-class
specifier other than extern". This is a constraint, so a conforming
implementation must issue a diagnostic if it encounters a declaration
of the form:

{
static int foo();
}

and need not translate successfully.

That means that it's impossible to correctly declare functions with
internal linkage at anything other than file scope, which greatly
limits the utility of this feature.

--
Michael Wojcik (e-mail address removed)

"Well, we're not getting a girl," said Marilla, as if poisoning wells were
a purely feminine accomplishment and not to be dreaded in the case of a boy.
-- L. M. Montgomery, _Anne of Green Gables_
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top