"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