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 21/10/2007, 11h57   #1 (permalink)
Srinu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Function call before main.

Hi all,

Can we assign return value of a function to a global variable? As we
know, main() will be the first function to be executed. but if the
above is true, then we have a function call before main. Please
me calarifying this. The code may be of the form.

int f();
int x = f();

int main()
{
printf("%d", x);
}

int f()
{
x=9;
}

In Turbo C++ compiler, it gives x = 9; how is this possible?

Srinu.

  Réponse avec citation
Vieux 21/10/2007, 12h10   #2 (permalink)
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Srinu wrote:

> Hi all,
>
> Can we assign return value of a function to a global variable? As we
> know, main() will be the first function to be executed. but if the
> above is true, then we have a function call before main. Please
> me calarifying this. The code may be of the form.
>
> int f();


To state explicitly that the function takes no parameters use the `void`
keyword.

int f(void);

> int x = f();
>
> int main()
> {
> printf("%d", x);


Include <stdio.h> for the prototype for printf. Without it, you are
invoking undefined behaviour.

> }


Since main is declared as returning an int, return a value. Use 0 or
EXIT_SUCCESS for sucessful termination and EXIT_FAILURE for abnormal
termination. The macros are defined in <stdlib.h>

> int f()
> {
> x=9;


f() is declared as returning an int and you return nothing here. This is
disallowed under the latest C Standard and can lead to unpredictable
behaviour if you attempt to use the return value of f(), as you've done
so.

If you don't want a function to return a value specify:

void f() ...

> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?


By sheer luck.

  Réponse avec citation
Vieux 21/10/2007, 12h27   #3 (permalink)
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Srinu wrote:
>
> Hi all,
>
> Can we assign return value of a function to a global variable?


It's undefined.

> As we
> know, main() will be the first function to be executed. but if the
> above is true, then we have a function call before main. Please
> me calarifying this. The code may be of the form.
>
> int f();
> int x = f();
>
> int main()
> {
> printf("%d", x);
> }
>
> int f()
> {
> x=9;
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?


The code is undefined.

--
pete
  Réponse avec citation
Vieux 21/10/2007, 13h02   #4 (permalink)
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.


"Srinu" <sinu.nayak2001@gmail.com> wrote in message
> Can we assign return value of a function to a global variable? As we
> know, main() will be the first function to be executed. but if the
> above is true, then we have a function call before main. Please
> me calarifying this. The code may be of the form.
>
> int f();
> int x = f();
>
> int main()
> {
> printf("%d", x);
> }
>
> int f()
> {
> x=9;
> }
>
> In Turbo C++ compiler, it gives x = 9; how is this possible?
>

C++ allows you to define global objects, and will call their constructors
before calling main(). The C++ standard was a little bit weak in this
respect last time I checked, with the order in which objects are constructed
not properly defined.

C doesn't allow any functions to be called excpet from main(), but is it
quite possible that your compiler will allow it as an extension. A lot of C
compilers are developed alongside C++ compilers after all.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  Réponse avec citation
Vieux 21/10/2007, 14h49   #5 (permalink)
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

santosh <santosh.k83@gmail.com> writes:

> Srinu wrote:
>
>> Hi all,
>>
>> Can we assign return value of a function to a global variable? As we
>> know, main() will be the first function to be executed. but if the
>> above is true, then we have a function call before main. Please
>> me calarifying this. The code may be of the form.
>>
>> int f();

>
> To state explicitly that the function takes no parameters use the `void`
> keyword.


What does "f()" state?
  Réponse avec citation
Vieux 21/10/2007, 15h12   #6 (permalink)
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Richard wrote:
> santosh <santosh.k83@gmail.com> writes:
>
>> Srinu wrote:
>>
>>> Hi all,
>>>
>>> Can we assign return value of a function to a global variable? As we
>>> know, main() will be the first function to be executed. but if the
>>> above is true, then we have a function call before main. Please
>>> me calarifying this. The code may be of the form.
>>>
>>> int f();

>> To state explicitly that the function takes no parameters use the `void`
>> keyword.

>
> What does "f()" state?


Do you truly not know? I thought you'd been on this
newsgroup long enough to have seen this mentioned half a
dozen times, but perhaps that's a different "Richard."

It states that the function f takes some fixed number
of arguments, but does not state what that number is nor
what the types of the arguments are.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 21/10/2007, 16h26   #7 (permalink)
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

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

> Richard wrote:
>> santosh <santosh.k83@gmail.com> writes:
>>
>>> Srinu wrote:
>>>
>>>> Hi all,
>>>>
>>>> Can we assign return value of a function to a global variable? As we
>>>> know, main() will be the first function to be executed. but if the
>>>> above is true, then we have a function call before main. Please
>>>> me calarifying this. The code may be of the form.
>>>>
>>>> int f();
>>> To state explicitly that the function takes no parameters use the `void`
>>> keyword.

>>
>> What does "f()" state?

>
> Do you truly not know? I thought you'd been on this
> newsgroup long enough to have seen this mentioned half a
> dozen times, but perhaps that's a different "Richard."


Nope. Probably me. And I never knew that. I had always assumed it to be
a lazy definition of f(void), but since I have never used it (I cant
remember the last time I wrote a function without at least one
parameter) then I wasn't sure.

>
> It states that the function f takes some fixed number


Or doesn't state :-;

> of arguments, but does not state what that number is nor
> what the types of the arguments are.


Which is used where?
  Réponse avec citation
Vieux 21/10/2007, 16h45   #8 (permalink)
James Kuyper Jr.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function call before main.

Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> Richard wrote:
>>> santosh <santosh.k83@gmail.com> writes:
>>>
>>>> Srinu wrote:

....
>>>>> int f();
>>>> To state explicitly that the function takes no parameters use the `void`
>>>> keyword.
>>> What does "f()" state?

>> Do you truly not know? I thought you'd been on this
>> newsgroup long enough to have seen this mentioned half a
>> dozen times, but perhaps that's a different "Richard."

>
> Nope. Probably me. And I never knew that. I had always assumed it to be
> a lazy definition of f(void), but since I have never used it (I cant
> remember the last time I wrote a function without at least one
> parameter) then I wasn't sure.
>
>> It states that the function f takes some fixed number

>
> Or doesn't state :-;


Well, it is undefined behavior to call f() with a different number of
arguments than the number specified in the definition of f(), or if the
definition of f() makes it a variadic function. So this declaration does
indeed state that the number is fixed.

>> of arguments, but does not state what that number is nor
>> what the types of the arguments are.

>
> Which is used where?


It's only supported to allow compilation of code written before the
invention of proper function prototypes. It serves no good purpose that
is not better served by a function prototype. It can be used to
obfuscate code, if that's your desire.
  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 11h33.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26037 seconds with 16 queries