PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > private functions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
private functions

Réponse
 
LinkBack Outils de la discussion
Vieux 31/05/2008, 16h19   #1
Ronald Bruck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut private functions

Sigh. It's been awhile since I've programmed in C, but I'm SURE that
you can have a function whose scope is purely within another function.
Yet here I have a program which compiles without a peep under gcc4.2.1
(with -ansi -Wall, no less), AND runs correctly, but which icc
(10.1.015) won't touch with a ten-foot-pole:

=====begin sample program=====
#include <stdio.h>

int main()
{
int sub1()
{
printf("1");
return(0);
}
int sub2()
{
printf("2\n");
return(0);
}
/* start of main */
sub1();
sub2();
}
=======end sample program========

I don't see anything wrong with this. Nevertheless, icc aborts the
compilation with the fatal error

testit.c(6): error: expected a ";"
{
^

It doesn't like the opening left-brace of "sub1".

Now, I've read this group before, and I know I'm going to get reamed
for such a simple question, but: what's wrong? And why does one
compiler pass it and the other doesn't?

This is in openSuse 10.3 on a 2.5GHz core 2 duo with 3GB RAM (though
none of that matters).

Thanks in advance,

--
Ron Bruck
  Réponse avec citation
Vieux 31/05/2008, 16h21   #2
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Ronald Bruck wrote:
> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
> you can have a function whose scope is purely within another function.

You simply can't do this in C.

> Yet here I have a program which compiles without a peep under gcc4.2.1
> (with -ansi -Wall, no less), AND runs correctly, but which icc
> (10.1.015) won't touch with a ten-foot-pole:
>
> =====begin sample program=====
> #include <stdio.h>
>
> int main()
> {
> int sub1()
> {
> printf("1");
> return(0);
> }
> int sub2()
> {
> printf("2\n");
> return(0);
> }
> /* start of main */
> sub1();
> sub2();
> }
> =======end sample program========
>
> I don't see anything wrong with this. Nevertheless, icc aborts the
> compilation with the fatal error
>
> testit.c(6): error: expected a ";"
> {
> ^
>
> It doesn't like the opening left-brace of "sub1".
>
> Now, I've read this group before, and I know I'm going to get reamed
> for such a simple question, but: what's wrong? And why does one
> compiler pass it and the other doesn't?

Which one takes it? Must be a non-conforming one then.

> This is in openSuse 10.3 on a 2.5GHz core 2 duo with 3GB RAM (though
> none of that matters).
>
> Thanks in advance,


Bye, Jojo


  Réponse avec citation
Vieux 31/05/2008, 16h24   #3
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Joachim Schmitz wrote:
> Ronald Bruck wrote:
>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>> you can have a function whose scope is purely within another
>> function. You simply can't do this in C.

>
>> Yet here I have a program which compiles without a peep under
>> gcc4.2.1 (with -ansi -Wall, no less), AND runs correctly, but which
>> icc (10.1.015) won't touch with a ten-foot-pole:
>>
>> =====begin sample program=====
>> #include <stdio.h>
>>
>> int main()
>> {
>> int sub1()
>> {
>> printf("1");
>> return(0);
>> }
>> int sub2()
>> {
>> printf("2\n");
>> return(0);
>> }
>> /* start of main */
>> sub1();
>> sub2();
>> }
>> =======end sample program========
>>
>> I don't see anything wrong with this. Nevertheless, icc aborts the
>> compilation with the fatal error
>>
>> testit.c(6): error: expected a ";"
>> {
>> ^
>>
>> It doesn't like the opening left-brace of "sub1".
>>
>> Now, I've read this group before, and I know I'm going to get reamed
>> for such a simple question, but: what's wrong? And why does one
>> compiler pass it and the other doesn't?

> Which one takes it? Must be a non-conforming one then.

Ah, I see, gcc. Add -pedantic to switch off that non-standard extension

>> This is in openSuse 10.3 on a 2.5GHz core 2 duo with 3GB RAM (though
>> none of that matters).
>>
>> Thanks in advance,


Bye, Jojo


  Réponse avec citation
Vieux 31/05/2008, 16h33   #4
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Ronald Bruck <bruck@math.usc.edu> wrote:
> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
> you can have a function whose scope is purely within another function.


Only as a compiler specific extension. I guess you're mixing that
up with the posibility to declare (but not define) a function
within another function.

> Yet here I have a program which compiles without a peep under gcc4.2.1
> (with -ansi -Wall, no less),


Add '-pedantic' to the mix and it will tell you

z6.c:6: warning: ISO C forbids nested functions
z6.c:11: warning: ISO C forbids nested functions

> AND runs correctly, but which icc
> (10.1.015) won't touch with a ten-foot-pole:


So icc does not support this extension (at least not per
default as gcc does).

> =====begin sample program=====
> #include <stdio.h>


> int main()
> {
> int sub1()
> {
> printf("1");
> return(0);
> }
> int sub2()
> {
> printf("2\n");
> return(0);
> }
> /* start of main */
> sub1();
> sub2();
> }
> =======end sample program========


> I don't see anything wrong with this. Nevertheless, icc aborts the
> compilation with the fatal error


> testit.c(6): error: expected a ";"
> {
> ^


> It doesn't like the opening left-brace of "sub1".


Also gcc starts complaining about that line if you make it
really standard compliant with '-pedantic'. All allowed at
this place is a ';' to end the declaration of the function.
But the '{' starts a function definition instead.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
  Réponse avec citation
Vieux 31/05/2008, 17h49   #5
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Ronald Bruck wrote:
>
> Sigh. It's been awhile since I've programmed in C, but I'm SURE
> that you can have a function whose scope is purely within another
> function. Yet here I have a program which compiles without a peep
> under gcc4.2.1 (with -ansi -Wall, no less), AND runs correctly,
> but which icc (10.1.015) won't touch with a ten-foot-pole:
>

.... snip ...
>
> Now, I've read this group before, and I know I'm going to get
> reamed for such a simple question, but: what's wrong? And why
> does one compiler pass it and the other doesn't?


Because you were using a non-standard extension of gcc. Local
functions are forbidden in standard C. If you run gcc properly
(with gcc -W -Wall -ansi -pedantic) so that it restricts itself to
standard C, it will reject that program too.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 31/05/2008, 18h33   #6
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:

> Ronald Bruck wrote:
>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>> you can have a function whose scope is purely within another function.

> You simply can't do this in C.


Nonsense. Of course you can do this in C. Non "standard" extensions are
still "C" I am afraid.
  Réponse avec citation
Vieux 31/05/2008, 18h40   #7
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Richard wrote:
> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
>
>> Ronald Bruck wrote:
>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>>> you can have a function whose scope is purely within another function.

>> You simply can't do this in C.

>
> Nonsense. Of course you can do this in C. Non "standard" extensions are
> still "C" I am afraid.


Nonsense. And if you're afraid, cower.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 31/05/2008, 19h18   #8
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Eric Sosman <esosman@ieee-dot-org.invalid> writes:

> Richard wrote:
>> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
>>
>>> Ronald Bruck wrote:
>>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>>>> you can have a function whose scope is purely within another function.
>>> You simply can't do this in C.

>>
>> Nonsense. Of course you can do this in C. Non "standard" extensions are
>> still "C" I am afraid.

>
> Nonsense. And if you're afraid, cower.


So people using nested functions are not writing C?

Don't be so ridiculous.

Oh, and tell IBM.

http://publib.boulder.ibm.com/infoce..._functions.htm
  Réponse avec citation
Vieux 31/05/2008, 19h51   #9
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

On Sat, 31 May 2008 12:49:01 -0400, CBFalconer wrote:
> Because you were using a non-standard extension of gcc. Local functions
> are forbidden in standard C. If you run gcc properly (with gcc -W -Wall
> -ansi -pedantic) so that it restricts itself to standard C, it will
> reject that program too.


That's just plain wrong. If you had actually bothered to try it, you would
have found that gcc with your recommended options accepts the original
code (as permitted).

$ gcc -W -Wall -ansi -pedantic testit.c -o testit
testit.c: In function ‘main’:
testit.c:5: warning: ISO C forbids nested functions
testit.c:10: warning: ISO C forbids nested functions
testit.c:18: warning: control reaches end of non-void function
$ ./testit
12

Implementations aren't required to reject code with syntax errors or
constraint violations. GCC with your recommended options doesn't. This is
intentional, conforming, and documented.
  Réponse avec citation
Vieux 31/05/2008, 20h35   #10
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> Richard wrote:
>>> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
>>>
>>>> Ronald Bruck wrote:
>>>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>>>>> you can have a function whose scope is purely within another function.
>>>> You simply can't do this in C.
>>> Nonsense. Of course you can do this in C. Non "standard" extensions are
>>> still "C" I am afraid.

>> Nonsense. And if you're afraid, cower.

>
> So people using nested functions are not writing C?


No, no more than those who write `GOTO 10' are writing C.

> Don't be so ridiculous.


Don't be daft.

> Oh, and tell IBM.
>
> http://publib.boulder.ibm.com/infoce..._functions.htm


Quoting from the above: "The language feature is an
extension to C89 and C99, implemented to facilitate porting
programs developed with GNU C."

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 31/05/2008, 21h19   #11
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Eric Sosman <esosman@ieee-dot-org.invalid> writes:

> Richard wrote:
>> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>>
>>> Richard wrote:
>>>> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
>>>>
>>>>> Ronald Bruck wrote:
>>>>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>>>>>> you can have a function whose scope is purely within another function.
>>>>> You simply can't do this in C.
>>>> Nonsense. Of course you can do this in C. Non "standard" extensions are
>>>> still "C" I am afraid.
>>> Nonsense. And if you're afraid, cower.

>>
>> So people using nested functions are not writing C?

>
> No, no more than those who write `GOTO 10' are writing C.
>
>> Don't be so ridiculous.

>
> Don't be daft.
>
>> Oh, and tell IBM.
>>
>> http://publib.boulder.ibm.com/infoce..._functions.htm

>
> Quoting from the above: "The language feature is an
> extension to C89 and C99, implemented to facilitate porting
> programs developed with GNU C."


I realise from your posting history that you like to be picky and
obstructive, but I am afraid they ARE writing C. It might not be "ISO
compliant C" (or whatever) but it is still C.

So Gnu supports them. IBM compilers them. Hey! Ring them up and tell
them it is "not C". It is C. Their C. Live with it.


  Réponse avec citation
Vieux 31/05/2008, 22h34   #12
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> Richard wrote:
>>> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>>>
>>>> Richard wrote:
>>>>> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
>>>>>
>>>>>> Ronald Bruck wrote:
>>>>>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
>>>>>>> you can have a function whose scope is purely within another function.
>>>>>> You simply can't do this in C.
>>>>> Nonsense. Of course you can do this in C. Non "standard" extensions are
>>>>> still "C" I am afraid.
>>>> Nonsense. And if you're afraid, cower.
>>> So people using nested functions are not writing C?

>> No, no more than those who write `GOTO 10' are writing C.
>>
>>> Don't be so ridiculous.

>> Don't be daft.
>>
>>> Oh, and tell IBM.
>>>
>>> http://publib.boulder.ibm.com/infoce..._functions.htm

>> Quoting from the above: "The language feature is an
>> extension to C89 and C99, implemented to facilitate porting
>> programs developed with GNU C."

>
> I realise from your posting history that you like to be picky and
> obstructive, but I am afraid they ARE writing C. It might not be "ISO
> compliant C" (or whatever) but it is still C.
>
> So Gnu supports them. IBM compilers them. Hey! Ring them up and tell
> them it is "not C". It is C. Their C. Live with it.


The programming language C is defined by an International
Standard. Implementations behave as that Standard requires;
when they don't, it's a bug.

Gadgets like nested functions are defined with varying
degrees of specificity and completeness, by the whims of the
implementors who choose to provide them. If implementations
differ incompatibly concerning the details of how such a
gadget behaves, each of the various providers will tell you
it's a feature.

The IBM quote you dug up explicitly describes the feature
as quote an extension to C89 and C99 endquote, not as part of
the C language. Re-read it -- and, in your words, live with it.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 31/05/2008, 22h55   #13
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> Richard wrote:
> > Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> >
> >> Richard wrote:
> >>> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
> >>>
> >>>> Ronald Bruck wrote:
> >>>>> Sigh. It's been awhile since I've programmed in C, but I'm SURE that
> >>>>> you can have a function whose scope is purely within another function.
> >>>> You simply can't do this in C.
> >>> Nonsense. Of course you can do this in C. Non "standard" extensions are
> >>> still "C" I am afraid.
> >> Nonsense. And if you're afraid, cower.

> >
> > So people using nested functions are not writing C?


> No, no more than those who write `GOTO 10' are writing C.


> > Don't be so ridiculous.


> Don't be daft.


I don't think it will if you ask this "Richard" character
not to be. He is and obviously can't it. Just whistle a
few bars of the Queen's song "Trolls will be trolls" and just
disregard him otherwise;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
  Réponse avec citation
Vieux 01/06/2008, 01h21   #14
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Harald van D?k wrote:
> CBFalconer wrote:
>
>> Because you were using a non-standard extension of gcc. Local
>> functions are forbidden in standard C. If you run gcc properly
>> (with gcc -W -Wall -ansi -pedantic) so that it restricts itself
>> to standard C, it will reject that program too.

>
> That's just plain wrong. If you had actually bothered to try it,
> you would have found that gcc with your recommended options
> accepts the original code (as permitted).
>
> $ gcc -W -Wall -ansi -pedantic testit.c -o testit
> testit.c: In function ‘main’:
> testit.c:5: warning: ISO C forbids nested functions
> testit.c:10: warning: ISO C forbids nested functions
> testit.c:18: warning: control reaches end of non-void function


I'm amused you consider that an acceptable result. Please flag all
your code so we know to avoid it.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 01/06/2008, 02h17   #15
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

CBFalconer <cbfalconer@yahoo.com> writes:
> Harald van D?k wrote:
>> CBFalconer wrote:
>>
>>> Because you were using a non-standard extension of gcc. Local
>>> functions are forbidden in standard C. If you run gcc properly
>>> (with gcc -W -Wall -ansi -pedantic) so that it restricts itself
>>> to standard C, it will reject that program too.

>>
>> That's just plain wrong. If you had actually bothered to try it,
>> you would have found that gcc with your recommended options
>> accepts the original code (as permitted).
>>
>> $ gcc -W -Wall -ansi -pedantic testit.c -o testit
>> testit.c: In function ‘main’:
>> testit.c:5: warning: ISO C forbids nested functions
>> testit.c:10: warning: ISO C forbids nested functions
>> testit.c:18: warning: control reaches end of non-void function

>
> I'm amused you consider that an acceptable result. Please flag all
> your code so we know to avoid it.


The point is that a conforming compiler is merely required to issue a
diagnostic; it's not required to reject the program, which is what you
asserted. By printing a warning, gcc fullfilled the requirements of
the standard.

Harald didn't say he considered it "an acceptable result", merely that
it doesn't violate the standard.

A conforming C implementation is not required to *reject* any program
that doesn't use "#error".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 01/06/2008, 02h34   #16
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Joachim Schmitz said:

<snip>

> Which [compiler] takes [nested functions]? Must be a non-conforming
> one then.


Conforming implementations are required to diagnose programs that contain
at least one syntax error or constraint violation (and nested functions
certainly qualify), but they are not required to reject them. Nested
functions are a perfectly legal extension.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 01/06/2008, 06h16   #17
jacob navia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

Richard Heathfield wrote:
> Joachim Schmitz said:
>
> <snip>
>
>> Which [compiler] takes [nested functions]? Must be a non-conforming
>> one then.

>
> Conforming implementations are required to diagnose programs that contain
> at least one syntax error or constraint violation (and nested functions
> certainly qualify), but they are not required to reject them. Nested
> functions are a perfectly legal extension.
>


Yes, when gcc is concerned.

When lcc-win does something similar the polemic never ends.


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
  Réponse avec citation
Vieux 01/06/2008, 07h42   #18
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: private functions

jacob navia said:

> Richard Heathfield wrote:
>> Joachim Schmitz said:
>>
>> <snip>
>>
>>> Which [compiler] takes [nested functions]? Must be a non-conforming
>>> one then.

>>
>> Conforming implementations are required to diagnose programs that
>> contain at least one syntax error or constraint violation (and nested
>> functions certainly qualify), but they are not required to reject them.
>> Nested functions are a perfectly legal extension.

>
> Yes, when gcc is concerned.


Where *any* conforming implementation is concerned.

> When lcc-win does something similar the polemic never ends.


Any conforming implementation may offer extensions, provided that they
don't break strictly conforming programs and provided that any syntax
errors or constraint violations result in at least one diagnostic message.
If lcc-win conforms to ISO C, it may offer extensions. And if it doesn't,
it is not bound by the requirements of the Standard in any case, so it's
hard to see what you're complaining about. It's equally hard to see why
you're complaining about it *here*. I suggest you take up the matter in
comp.compilers.lcc, where discussion of the lcc compiler is topical.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,29566 seconds with 26 queries