473,406 Members | 2,378 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,406 software developers and data experts.

Scope/use of prototypes Qs

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:

<snip>

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!
--
==============
Not a pedant
==============
Mar 29 '06 #1
9 1368

pemo wrote:

<snip>
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.

--
BR, Vladimir

Mar 29 '06 #2
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:

(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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '06 #3
Mark McIntyre wrote:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:

(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.


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?

--
==============
Not a pedant
==============
Mar 29 '06 #4
On Wed, 29 Mar 2006 13:06:53 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:
Mark McIntyre wrote:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:

(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.


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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '06 #5
Mark McIntyre wrote:
On Wed, 29 Mar 2006 13:06:53 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:
Mark McIntyre wrote:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:

(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.


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*.


Seemed a logical conclusion - oh well, must be just me then.

--
==============
Not a pedant
==============
Mar 29 '06 #6
On 2006-03-29, Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 29 Mar 2006 12:36:26 +0100, in comp.lang.c , "pemo"
<us***********@gmail.com> wrote:

(of locally declared functions)
What do others think? Should there be warnings/errors?
Its a nonstandard extension, AFAIK.


No. You may be thinking of nested function _definitions_.
Make sure you compiled in ultra pedantic ANSI mode. Distrust any
compiler that doesn't consider this an error in that mode.

Mark McIntyre

Mar 29 '06 #7
Mark McIntyre wrote:
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.


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/>
Mar 29 '06 #8
Mark McIntyre wrote:
(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.


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.

Mar 30 '06 #9

In article <44***************@yahoo.com>, CBFalconer <cb********@yahoo.com> writes:
Mark McIntyre wrote:
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.


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 mi************@microfocus.com

"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_
Mar 31 '06 #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...
4
by: zmcelrath87 | last post by:
I am having a problem involving the scope of timeouts and intervals. Since timeouts and intervals execute in the global scope, dynamically generated local interval/timeout declarations do not work,...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: fctk | last post by:
are the following rules correct in C89/C90? ---- SCOPE RULES 1) the scope of an identifier declared inside a block is the block in which it is declared; 2) when you have nested blocks...
6
by: Frank Silvermann | last post by:
I have taken an extraordinary leap into the modern world by purchasing webspace. In addition to my private concerns, I would like to make a part to which others, e.g. my nieces and ex-wife, can...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
2
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
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...
21
by: Thomas Pornin | last post by:
Hello, with gcc-4.2.3, this does not compile: typedef int foo; int f(foo) int foo; { return foo; }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
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...

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.