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 > Function call before main.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Function call before main.

Réponse
 
LinkBack Outils de la discussion
Vieux 22/10/2007, 16h10   #26
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Kenneth Brody wrote On 10/22/07 09:41,:
> pete wrote:
>
>>Srinu wrote:
>>
>>>Hi all,
>>>
>>>Can we assign return value of a function to a global variable?

>>
>>It's undefined.

>
>
> "Undefined" or "illegal"?


"Illegal"[*] because it contravenes a "shall" in a
constraint section and thus requires a diagnostic.

"Undefined" because if the implementation accepts
it anyhow (after issuing the required diagnostic), the
Standard does not define the behavior.
[*]The Standard uses the word "illegal" only once, and
not in reference to a source construct (besides, it's in
a footnote and hence non-normative). But it's fun to imagine
a jurisdiction where the Standard had the force of law ...
"Hands off that keyboard! Step away from the monitor,
keeping your hands in view, that's right. I'm taking you in
on a charge of violating 6.2.2p3, using an identifier with
both internal and external linkage in the same translation
unit. Cuff him, Dennis, and read him his rights. You, my
friend, are going to spend a few years behind ISO-bars."

--
Eric.Sosman@sun.com
  Réponse avec citation
Vieux 22/10/2007, 16h23   #27
jameskuyper@verizon.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Richard Heathfield wrote:
> Kenneth Brody said:
> > santosh wrote:
> >> James Kuyper Jr. wrote:

> <snip>
>
> >> > He indicated that he was using a C++ compiler. [ ... ]
> >>
> >> But in the latest post he said he also compiled it in gcc, which is, or
> >> can be, an ISO C compiler.

> >
> > No, he said compiling it in gcc gave him the (expected) error message
> > "initializer element is not constant".

>
> Er, so is it your position that he did *not* compile it in gcc, *and* that
> compiling it in gcc gave him the error message? Because, if so, you just
> lost me completely.


More precisely, he attempted to compile it in gcc, and the attempt
failed, with an error message.

  Réponse avec citation
Vieux 22/10/2007, 17h31   #28
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Eric Sosman wrote:
[...]
>[*]The Standard uses the word "illegal" only once, and
> not in reference to a source construct (besides, it's in
> a footnote and hence non-normative). But it's fun to imagine
> a jurisdiction where the Standard had the force of law ...
> "Hands off that keyboard! Step away from the monitor,

[...]
> unit. Cuff him, Dennis, and read him his rights. You, my
> friend, are going to spend a few years behind ISO-bars."


The pressure to come up with a followup pun is too great to bear.
I don't think I can weather the consequences of not posting.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>


  Réponse avec citation
Vieux 22/10/2007, 17h39   #29
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Richard Heathfield wrote:
>
> Kenneth Brody said:
> > santosh wrote:
> >> James Kuyper Jr. wrote:

> <snip>
>
> >> > He indicated that he was using a C++ compiler. [ ... ]
> >>
> >> But in the latest post he said he also compiled it in gcc, which is, or
> >> can be, an ISO C compiler.

> >
> > No, he said compiling it in gcc gave him the (expected) error message
> > "initializer element is not constant".

>
> Er, so is it your position that he did *not* compile it in gcc, *and* that
> compiling it in gcc gave him the error message? Because, if so, you just
> lost me completely.


Well, quoting the OP:

>>> I tried the following...It gives the same value 9 in TCC but showing
>>> error: initializer element is not constant in gcc.


I am, of course, assuming that "in gcc" is not actually part of
the error message, but rather a modifier to the verb "showing".

I suppose the correct statement would be that he ran gcc, in an
attempt to compile it, but the attempt failed with the stated
error message.

Does the term "compiling" require that the compilation succeeded?
Isn't it just as valid to say "I was cooking dinner, but I burned
the chicken"? Even though the cooking failed when it enountered
the error, I don't think it's necessary to qualify the statement
by saying "I attempted to cook dinner" instead.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>

  Réponse avec citation
Vieux 22/10/2007, 17h57   #30
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Kenneth Brody said:

> Richard Heathfield wrote:
>>
>> Kenneth Brody said:
>> > santosh wrote:
>> >> James Kuyper Jr. wrote:

>> <snip>
>>
>> >> > He indicated that he was using a C++ compiler. [ ... ]
>> >>
>> >> But in the latest post he said he also compiled it in gcc, which is,
>> >> or can be, an ISO C compiler.
>> >
>> > No, he said compiling it in gcc gave him the (expected) error message
>> > "initializer element is not constant".

>>
>> Er, so is it your position that he did *not* compile it in gcc, *and*
>> that compiling it in gcc gave him the error message? Because, if so, you
>> just lost me completely.

>
> Well, quoting the OP:
>
>>>> I tried the following...It gives the same value 9 in TCC but showing
>>>> error: initializer element is not constant in gcc.

>
> I am, of course, assuming that "in gcc" is not actually part of
> the error message, but rather a modifier to the verb "showing".
>
> I suppose the correct statement would be that he ran gcc, in an
> attempt to compile it, but the attempt failed with the stated
> error message.
>
> Does the term "compiling" require that the compilation succeeded?
> Isn't it just as valid to say "I was cooking dinner, but I burned
> the chicken"? Even though the cooking failed when it enountered
> the error, I don't think it's necessary to qualify the statement
> by saying "I attempted to cook dinner" instead.


My confusion came from the fact that santosh said that the OP had compiled
it in gcc, and you said "No", before going on to agree that he had
compiled it in gcc. This puzzled me somewhat. Anyway, it seems you do now
agree that the OP *did* compile it in gcc, at least for a certain value of
"compile".

--
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 22/10/2007, 18h55   #31
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Richard Heathfield wrote:
>
> Kenneth Brody said:
>
> > Richard Heathfield wrote:
> >>
> >> Kenneth Brody said:
> >> > santosh wrote:
> >> >> James Kuyper Jr. wrote:
> >> <snip>
> >>
> >> >> > He indicated that he was using a C++ compiler. [ ... ]
> >> >>
> >> >> But in the latest post he said he also compiled it in gcc, which is,
> >> >> or can be, an ISO C compiler.
> >> >
> >> > No, he said compiling it in gcc gave him the (expected) error message
> >> > "initializer element is not constant".

[...]
> My confusion came from the fact that santosh said that the OP had compiled
> it in gcc, and you said "No", before going on to agree that he had
> compiled it in gcc. This puzzled me somewhat. Anyway, it seems you do now
> agree that the OP *did* compile it in gcc, at least for a certain value of
> "compile".


Well, I read santosh's post to mean that he thought the OP had
_successfully_ compiled it in gcc, hence my "no". Perhaps I was
mistaken in what santosh meant?

In any case, I think we're all in agreement that initializing a
"global" variable with a function call is not valid C, though it
may be a valid extension in some C compilers. It also appears
that it may be valid C++, but one would have to ask down the hall
for a definitive answer.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>

  Réponse avec citation
Vieux 22/10/2007, 19h24   #32
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Kenneth Brody said:
> Richard Heathfield wrote:

<snip>
>> My confusion came from the fact that santosh said that the OP had
>> compiled it in gcc, and you said "No", before going on to agree that he
>> had compiled it in gcc. This puzzled me somewhat. Anyway, it seems you
>> do now agree that the OP *did* compile it in gcc, at least for a certain
>> value of "compile".

>
> Well, I read santosh's post to mean [...]


Oh deary deary me. :-) Let's just get some whitewash, and paint out this
whole subthread, shall we?

--
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 23/10/2007, 00h12   #33
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Nick Keighley wrote:
> pete <pfil...@mindspring.com> wrote:
>> Srinu wrote:

>
>>> Can we assign return value of a function to a global variable?

>>
>> It's undefined.

>
> is it undefined or a constraint violation. Is the compiler obliged
> to produce a diagnostic?


Of course it is legal. Example follows (untested):

#include <stdio.h>

int value;

int funct(void) {
return 123;
}

int main(void) {

value = 12;
printf("value = %d\n", value);
value = funct();
printf("value = %d\n", value);
return 0;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 23/10/2007, 07h19   #34
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Kenneth Brody <kenbrody@spamcop.net> wrote:

> Eric Sosman wrote:
> [...]
> >[*]The Standard uses the word "illegal" only once, and
> > not in reference to a source construct (besides, it's in
> > a footnote and hence non-normative). But it's fun to imagine
> > a jurisdiction where the Standard had the force of law ...
> > "Hands off that keyboard! Step away from the monitor,

> [...]
> > unit. Cuff him, Dennis, and read him his rights. You, my
> > friend, are going to spend a few years behind ISO-bars."

>
> The pressure to come up with a followup pun is too great to bear.
> I don't think I can weather the consequences of not posting.


Oh, do stop raining on Eric's parade. He was only trying to bring a
bright spot to this group.

Richard
  Réponse avec citation
Vieux 23/10/2007, 07h25   #35
Srinu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Dear All,

I used Turbo C++ compiler but the code was written in a C file and
compiled. So as Richard said, the compiler might have used the inbuilt
C compiler. What I think is, may be the saparation between the inbuilt
C compiler and C++ compiler in Turbo package, not be totally different
and because of that it gives some machine code which will let the user
know that this kind of code is compiler dependent and not standard
defined. But gcc denies to generate the machine code even! It's OK.

To answer friend Richard's question "what it is that you are trying to
do" :

Basically I came across a code in C++ which creates a global variable
as x= new My_Object(); i thought if it has to work, the constructor
must be called before the main(). But how is that possible? I
immediately took my tcc compiler and tried to execute the above code.
To my surprise it gave 9. So I thought of geeting experts suggestion
on whether C standard says anything about it or not. Later I came to
know that C++ allows it. I too couldn't understand, in C++, it is only
the constructors are allowed or any function. But I couldn't dare to
ask that in this C group.

To answer friend James question "Why he was asking a C group about the
result of using a C++ compiler is a harder question." :

The situation is little odd here. I asked this question in C group
only, thinking that anyway the code is in C and I was looking for the
answer on how a C code should behave. but it is wrong as my friend
points, why to discuss about a C++ compiler. Ok, if I would have asked
the question in C++ group, there obviously the question might have
arised "why there is a question about C code in C++ group?". So
finally where to post? I agree it is my fault, I wrote a C code and
compiled in a C++ compiler. But where to go now? As friend Richard
says, now a days mostly/easily available C/C++ compilers incorporate
these two compilers both. even gcc can compile two languages code,
even more than that. So with confusion I had to post it here.I would
like to apologize if I am wrong.


Thanks a lot to all of friends.
With regards.
Srinu

  Réponse avec citation
Vieux 23/10/2007, 08h14   #36
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Srinu said:

<snip>

> To answer friend Richard's question "what it is that you are trying to
> do" :
>
> Basically I came across a code in C++ which creates a global variable
> as x= new My_Object(); i thought if it has to work, the constructor
> must be called before the main(). But how is that possible?


It *has* to be possible in C++, for reasons which you're better off
discovering in comp.lang.c++ if your C++ book doesn't tell you about them.
And because it *has* to be possible, they made it possible.

It doesn't need to be possible in C, and so they didn't make it possible in
C. If your C compiler lets you do it, this is an extension provided by
that compiler, not a legal C construct.

<snip>

--
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 23/10/2007, 13h10   #37
James Kuyper Jr.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Srinu wrote:
> Dear All,
>
> I used Turbo C++ compiler but the code was written in a C file and
> compiled. So as Richard said, the compiler might have used the inbuilt
> C compiler. What I think is, may be the saparation between the inbuilt
> C compiler and C++ compiler in Turbo package, not be totally different


When a compiler handles both C and C++, there's an incentive for the
implementors to minimize the differences between those two languages.
When code has implementation-defined or undefined behavior in at least
one of the two languages, then it sometimes simplifies things for the
implementors if they choose the same behavior in both languages.

> To answer friend Richard's question "what it is that you are trying to
> do" :
>
> Basically I came across a code in C++ which creates a global variable
> as x= new My_Object(); i thought if it has to work, the constructor
> must be called before the main(). But how is that possible? I
> immediately took my tcc compiler and tried to execute the above code.
> To my surprise it gave 9. So I thought of geeting experts suggestion
> on whether C standard says anything about it or not. Later I came to
> know that C++ allows it. I too couldn't understand, in C++, it is only
> the constructors are allowed or any function. But I couldn't dare to
> ask that in this C group.


The initializer for a static object in C++ can be just about any
expression that can be implicitly converted to the declared type of the
object. My copy of the C++ standard is stuck on the hard drive of a
non-functional computer, thanks to a failed Windows update, so I can't
quote you the precise rules. However, the main issue you would have to
worry about is order of initialization. Static objects are initialized
in an order that is only partially constrained by the C++ standard; if
evaluation of the initializer expression for one object requires
retrieval of the value of another static object that hasn't been
initialized yet, the behavior is undefined.

> To answer friend James question "Why he was asking a C group about the
> result of using a C++ compiler is a harder question." :


Well, I considered that your compiler might have allowed you, as an
extension, to do the same thing in C that you can in C++. However, since
it's a constraint violation in C (6.7.8p4), I didn't think that was
likely. Most compilers treat constraint violations as fatal errors,
though all that the standard requires is a diagnostic message, which
need not be fatal.

I think it's more likely that you unknowingly invoked the compiler in
C++ mode. Many C/C++ compilers automatically default to C++ for files
with extensions like .cpp or .c++, unless you explicitly tell them
otherwise. What name did you use for your file?

> The situation is little odd here. I asked this question in C group
> only, thinking that anyway the code is in C and I was looking for the
> answer on how a C code should behave. but it is wrong as my friend
> points, why to discuss about a C++ compiler. Ok, if I would have asked
> the question in C++ group, there obviously the question might have
> arised "why there is a question about C code in C++ group?". So
> finally where to post? I agree it is my fault, I wrote a C code and
> compiled in a C++ compiler. But where to go now? As friend Richard
> says, now a days mostly/easily available C/C++ compilers incorporate
> these two compilers both. even gcc can compile two languages code,
> even more than that. So with confusion I had to post it here.I would
> like to apologize if I am wrong.


You could have cross-posted this question to one of the C++ newsgroups,
if you were aware that this was a C++ specific behavior. However, if you
had known that it was C++-specific, you would not have had to ask your
question in the first place. So don't worry about it.
  Réponse avec citation
Vieux 24/10/2007, 06h52   #38
Srinu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

> I think it's more likely that you unknowingly invoked the compiler in
> C++ mode. Many C/C++ compilers automatically default to C++ for files
> with extensions like .cpp or .c++, unless you explicitly tell them
> otherwise. What name did you use for your file?


I had used .c for my file name.

Srinu.

  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 19h15.


É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,22561 seconds with 21 queries