473,836 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1385

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**********@s pamcop.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.c om, 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/googlegroupsrep ly/>
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********@yah oo.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
3567
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...
4
1377
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, because the dynamically generated code they have to execute involves variables that have been thrown out by the time of execution. In the first of the two following code examples, my interval declaration includes code that relies on variables...
5
2097
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 function-prototype scope I think(might be wrong):
3
4144
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 (extern block and internal block), an identifier declared in the external block is known also in the internal block, while an identifier declared in the internal block is not known
6
1763
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 ftp. To keep a hold of the reigns, I shall write a program that changes the password, and the brains of this prog will be in C: #def MIN_WORD_LENGTH 9 #def MAX_WORD_LENGTH 15 /* subroutine for random permutation */ void permute(char *, int)
7
2203
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
2296
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
3479
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...
21
3997
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
9820
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10846
Oralloy
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...
1
10594
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,...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9376
agi2029
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...
1
7793
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 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...
0
5650
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...
2
4020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.