function/variable references in c

Discussion in 'C Programming' started by Rahul, Jan 5, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    when a call to a function is done in a file and if the function isn't
    defined, compiler just assumes that it would return a int and that the
    definition would be available at some other compilation unit which
    should be given to the linker to generate the executable file.

    However, why doesn't this extend to a global variable which isn't
    declared? The compiler gives as error immediately...

    Thanks in advance!!!
    Rahul, Jan 5, 2008
    #1
    1. Advertising

  2. Rahul

    jacob navia Guest

    Rahul wrote:
    > Hi Everyone,
    >
    > when a call to a function is done in a file and if the function isn't
    > defined, compiler just assumes that it would return a int and that the
    > definition would be available at some other compilation unit which
    > should be given to the linker to generate the executable file.
    >


    This is surely not good practice and is maintened for backward
    compatibility only.

    > However, why doesn't this extend to a global variable which isn't
    > declared? The compiler gives as error immediately...
    >


    Better that that than at link time isn't it?

    > Thanks in advance!!!



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Jan 5, 2008
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Jan 5, 3:42 pm, jacob navia <> wrote:
    > Rahul wrote:
    > > Hi Everyone,

    >
    > > when a call to a function is done in a file and if the function isn't
    > > defined, compiler just assumes that it would return a int and that the
    > > definition would be available at some other compilation unit which
    > > should be given to the linker to generate the executable file.

    >
    > This is surely not good practice and is maintened for backward
    > compatibility only.
    >


    Do you mean resolution during link time?
    and backward compatibility with what?
    Rahul, Jan 5, 2008
    #3
  4. Rahul

    jacob navia Guest

    Rahul wrote:
    > On Jan 5, 3:42 pm, jacob navia <> wrote:
    >> Rahul wrote:
    >>> Hi Everyone,
    >>> when a call to a function is done in a file and if the function isn't
    >>> defined, compiler just assumes that it would return a int and that the
    >>> definition would be available at some other compilation unit which
    >>> should be given to the linker to generate the executable file.

    >> This is surely not good practice and is maintened for backward
    >> compatibility only.
    >>

    >
    > Do you mean resolution during link time?
    > and backward compatibility with what?
    >


    I mean
    <quote>
    when a call to a function is done in a file and if the function isn't
    defined, compiler just assumes that it would return a int and that the
    definition would be available at some other compilation unit which
    should be given to the linker to generate the executable file.
    <end quote>

    Implicit int is an obsolescent feature.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Jan 5, 2008
    #4
  5. Rahul

    Flash Gordon Guest

    jacob navia wrote, On 05/01/08 19:05:
    > Rahul wrote:
    >> On Jan 5, 3:42 pm, jacob navia <> wrote:
    >>> Rahul wrote:
    >>>> Hi Everyone,
    >>>> when a call to a function is done in a file and if the function isn't
    >>>> defined, compiler just assumes that it would return a int and that the
    >>>> definition would be available at some other compilation unit which
    >>>> should be given to the linker to generate the executable file.
    >>> This is surely not good practice and is maintened for backward
    >>> compatibility only.

    >>
    >> Do you mean resolution during link time?
    >> and backward compatibility with what?

    >
    > I mean
    > <quote>
    > when a call to a function is done in a file and if the function isn't
    > defined, compiler just assumes that it would return a int and that the
    > definition would be available at some other compilation unit which
    > should be given to the linker to generate the executable file.
    > <end quote>
    >
    > Implicit int is an obsolescent feature.


    In the latest version of the standard it is not obsolescent (which means
    that it is still present but may be removed) it is completely gone from
    the language. In the previous standards I believe it was included for
    backwards compatibility with pre-standard code.
    --
    Flash Gordon
    Flash Gordon, Jan 6, 2008
    #5
  6. Rahul

    Jack Klein Guest

    On Sat, 5 Jan 2008 02:35:41 -0800 (PST), Rahul <>
    wrote in comp.lang.c:

    > Hi Everyone,
    >
    > when a call to a function is done in a file and if the function isn't
    > defined, compiler just assumes that it would return a int and that the
    > definition would be available at some other compilation unit which
    > should be given to the linker to generate the executable file.


    Actually, that "feature" was removed from the language in the 1999
    version, and all later versions of the C standard. So don't write
    code like that anymore.

    > However, why doesn't this extend to a global variable which isn't
    > declared? The compiler gives as error immediately...


    Why should it? The call of functions without a declaration should
    never have been in the language in the first place.

    --
    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, Jan 6, 2008
    #6
  7. "Rahul" <> wrote in message
    news:...
    > Hi Everyone,
    >
    > when a call to a function is done in a file and if the function isn't
    > defined, compiler just assumes that it would return a int and that the
    > definition would be available at some other compilation unit which
    > should be given to the linker to generate the executable file.
    >
    > However, why doesn't this extend to a global variable which isn't
    > declared? The compiler gives as error immediately...
    >
    > Thanks in advance!!!


    that error may have crept into the old C because the compiler could do some
    sort of
    code generation for a function call, but not for variables.

    if function f() is not defined (and not declared in its prototype form -
    this is important), the
    compiler assumes the form "int f()" , which to very likely to be incorrect,
    because the actual definition may be
    say void f(int,float). The compiler has generated code at the point of call
    for a function with "int" return and
    no arguments. Whereras the function expects an int and float (maybe on
    stack, or in registers), but does not return anything.
    A runtime disaster, no doubt but mericifully detected by linker

    In contrast, what code can the compiler generate for an undeclared variable
    ? None, because the compiler not
    psychic to know the type of the variable from its usage !
    Ravishankar S, Jan 7, 2008
    #7
    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. Roger Leigh
    Replies:
    8
    Views:
    434
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    447
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    710
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    833
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page