Function call before main.

Discussion in 'C Programming' started by Srinu, Oct 21, 2007.

  1. Srinu

    Srinu Guest

    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 help
    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.
     
    Srinu, Oct 21, 2007
    #1
    1. Advertising

  2. Srinu

    santosh Guest

    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 help
    > 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.
     
    santosh, Oct 21, 2007
    #2
    1. Advertising

  3. Srinu

    pete Guest

    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 help
    > 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
     
    pete, Oct 21, 2007
    #3
  4. "Srinu" <> 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 help
    > 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
     
    Malcolm McLean, Oct 21, 2007
    #4
  5. Srinu

    Richard Guest

    santosh <> 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 help
    >> 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?
     
    Richard, Oct 21, 2007
    #5
  6. Srinu

    Eric Sosman Guest

    Richard wrote:
    > santosh <> 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 help
    >>> 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
    lid
     
    Eric Sosman, Oct 21, 2007
    #6
  7. Srinu

    Richard Guest

    Eric Sosman <> writes:

    > Richard wrote:
    >> santosh <> 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 help
    >>>> 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?
     
    Richard, Oct 21, 2007
    #7
  8. Richard wrote:
    > Eric Sosman <> writes:
    >
    >> Richard wrote:
    >>> santosh <> 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.
     
    James Kuyper Jr., Oct 21, 2007
    #8
  9. Srinu

    Eric Sosman Guest

    Richard wrote:
    > Eric Sosman <> writes:
    >
    >> Richard wrote:
    >>> santosh <> 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 help
    >>>>> 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?

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

    >
    > Or doesn't state :-;


    No, "states." If declared this way, the function f must
    not be variadic (if it is, and if you call it, the behavior
    is undefined). So the declaration does in fact state something
    about f's argument list. Not much, but more than nothing.

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

    >
    > Which is used where?


    I do not understand this question. For starters, what is
    the referent of "which?" And what do you mean by "used?"

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 21, 2007
    #9
  10. Srinu

    abhy Guest

    On Oct 21, 3:57 pm, 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 help
    > 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.


    In f() x is a local variable and thus takes priority over global
    variable x.
    Thus when x is assigned a value , it continues to remain as x is also
    a global.
    If x was declared within main , then 9 will never get printed.
     
    abhy, Oct 21, 2007
    #10
  11. abhy <> writes:

    > On Oct 21, 3:57 pm, Srinu <> wrote:

    <snip: "can a file-scope variable be initialised with the
    result of a function call?">
    >>
    >> 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?

    >
    > In f() x is a local variable and thus takes priority over global
    > variable x.


    No it isn't -- x has "file scope".

    The question has been well answered. The only thing that I can might
    be worth adding is that neither int valued function contains a return
    statement.

    --
    Ben.
     
    Ben Bacarisse, Oct 21, 2007
    #11
  12. In article <>,
    Ben Bacarisse <> wrote:
    ....
    >The question has been well answered. The only thing that I can might
    >be worth adding is that neither int valued function contains a return
    >statement.


    You mean you're not going to bitch about the lack of a newline in:

    >>> printf("%d", x);


    ???

    I thought that was CLC SOP.
     
    Kenny McCormack, Oct 21, 2007
    #12
  13. Srinu

    Richard Guest

    Eric Sosman <> writes:

    > Richard wrote:
    >> Eric Sosman <> writes:
    >>
    >>> Richard wrote:
    >>>> santosh <> 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 help
    >>>>>> 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?
    >>>
    >>> It states that the function f takes some fixed number

    >>
    >> Or doesn't state :-;

    >
    > No, "states." If declared this way, the function f must
    > not be variadic (if it is, and if you call it, the behavior
    > is undefined). So the declaration does in fact state something
    > about f's argument list. Not much, but more than nothing.
    >
    >>> of arguments, but does not state what that number is nor
    >>> what the types of the arguments are.

    >>
    >> Which is used where?

    >
    > I do not understand this question. For starters, what is
    > the referent of "which?" And what do you mean by "used?"


    Seems reasonably clear :( Where would you use it in that form where
    another form explicitly stating the arguments would be better?
     
    Richard, Oct 21, 2007
    #13
  14. Srinu

    Eric Sosman Guest

    Richard wrote:
    > Eric Sosman <> writes:
    >
    >> Richard wrote:
    >>> Eric Sosman <> writes:
    >>>
    >>>> Richard wrote:
    >>>>> santosh <> 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 help
    >>>>>>> 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?
    >>>> It states that the function f takes some fixed number
    >>> Or doesn't state :-;

    >> No, "states." If declared this way, the function f must
    >> not be variadic (if it is, and if you call it, the behavior
    >> is undefined). So the declaration does in fact state something
    >> about f's argument list. Not much, but more than nothing.
    >>
    >>>> of arguments, but does not state what that number is nor
    >>>> what the types of the arguments are.
    >>> Which is used where?

    >> I do not understand this question. For starters, what is
    >> the referent of "which?" And what do you mean by "used?"

    >
    > Seems reasonably clear :( Where would you use it in that form where
    > another form explicitly stating the arguments would be better?


    You would use such a declaration in connection with
    pre-Standard code, because "another form" was not always
    available. In particular, if you need to write a declaration
    for an old-style function like

    int f(x)
    enum States x;
    { ... }

    then `int f()' is pretty much your only choice.

    You might also use it when declaring function pointers
    that might point to functions of different types:

    int f(int);
    int g(double);
    int h(const char*);

    int (*fp)() = (int(*)()) g;

    switch (whatIsIt) {
    case INT: x = fp(42); break;
    case DBL: x = fp(42.0); break;
    case STR: x = fp("XLII"); break;
    }

    Even here, though, it might be better (or even necessary)
    to cast the function pointer to get the benefit of prototypes:

    switch (whatIsIt) {
    case INT: x = ((int(*)(int)) fp)(42); break;
    case DBL: x = ((int(*)(double)) fp)(42.0); break;
    case STR: x = ((int(*)(const char*)) fp)("XLII"); break;
    }

    As is often the case with function pointers, a few typedefs
    can remove a lot of visual clutter.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 21, 2007
    #14
  15. On Oct 21, 3:57 am, 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 help
    > me calarifying this. The code may be of the form.


    A "global" object has static duration. The initialization of a static
    object must be a constant expression or a string literal. The return
    value of a function does not qualify. Therefore, the answer to your
    question is no and the subsequent discussion is based on a false
    premise.

    hopelessly incorrect code snipped

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


    Did you invoke the compiler is strictly conforming mode? Many
    compilers default to allowing non-standard extensions.
     
    Barry Schwarz, Oct 21, 2007
    #15
  16. Srinu

    Jack Klein Guest

    On Sun, 21 Oct 2007 10:57:41 -0000, Srinu <>
    wrote in comp.lang.c:

    > 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 help
    > 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?


    Because you used the Turbo C++ compiler to compile this code as C++.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 22, 2007
    #16
  17. Srinu

    Srinu Guest

    Hi all,

    Thanks a lot for all the answers.

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

    #include<stdio.h>
    int f();
    int x = f();

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

    int f()
    {
    return 9;
    }
     
    Srinu, Oct 22, 2007
    #17
  18. On 21 Oct, 12:27, pete <> 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?

    <snip>

    --
    Nick Keighley
     
    Nick Keighley, Oct 22, 2007
    #18
  19. Srinu said:

    > Hi all,
    >
    > Thanks a lot for all the answers.
    >
    > I tried the following...It gives the same value 9 in TCC but showing
    > error: initializer element is not constant in gcc.
    >
    > #include<stdio.h>
    > int f();
    > int x = f();


    This is not valid C code. The rules of C do not give you licence to call a
    function from any part of the code other than inside another function. If
    you try to do so anyway, the compiler is free to reject your code, or to
    translate it in some arbitrary manner which may or may not be predictable
    on a given implementation.

    In other words, you're doing it wrong. Instead of trying to understand why
    you get the output you have shown, decide what you are trying to achieve,
    and try to establish a legal way to achieve it. The way to do that
    depends, of course, and what it is that you are trying to do.

    --
    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
     
    Richard Heathfield, Oct 22, 2007
    #19
  20. Richard Heathfield wrote:
    > Srinu said:
    >
    >> Hi all,
    >>
    >> Thanks a lot for all the answers.
    >>
    >> I tried the following...It gives the same value 9 in TCC but showing
    >> error: initializer element is not constant in gcc.
    >>
    >> #include<stdio.h>
    >> int f();
    >> int x = f();

    >
    > This is not valid C code. The rules of C do not give you licence to call a
    > function from any part of the code other than inside another function. If
    > you try to do so anyway, the compiler is free to reject your code, or to
    > translate it in some arbitrary manner which may or may not be predictable
    > on a given implementation.


    He indicated that he was using a C++ compiler. The rules of C++ do allow
    it, and the results can be predictable, except when they depend upon the
    order of initialization of static objects, which isn't a problem in this
    example. Some pairs of objects do have guaranteed ordering, others do
    not - you have to know the relevant rules if you decide to write that
    kind of code.

    Why he was asking a C group about the result of using a C++ compiler is
    a harder question.
     
    James Kuyper Jr., Oct 22, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manuel
    Replies:
    6
    Views:
    375
    John Isaacks
    Jul 19, 2003
  2. Ravi
    Replies:
    17
    Views:
    943
    Kenneth Brody
    Apr 1, 2006
  3. ravi
    Replies:
    28
    Views:
    1,016
    Richard Heathfield
    Sep 26, 2007
  4. Replies:
    13
    Views:
    6,875
    Kenneth Brody
    Mar 12, 2008
  5. Yohan N. Leder
    Replies:
    19
    Views:
    264
    Yohan N. Leder
    Jul 2, 2006
Loading...

Share This Page