initializing a local variable using a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iu2

    initializing a local variable using a function

    Hi all,

    This code compiles:

    int func1()
    {
    return 3;
    }

    void func2()
    {
    int a = func1();
    }

    In func2 the initialization of 'a' is done by calling to func1().
    Is this ok accroding to ANSI C? (I thougt this is legal only in C+
    +...)
    I need to know it because the project I work on will have to be ported
    to a different compiler (other than gcc), and I need to use ANSI C.

    Thanks
  • Chris Dollin

    #2
    Re: initializing a local variable using a function

    iu2 wrote:
    Hi all,
    >
    This code compiles:
    >
    int func1()
    {
    return 3;
    }
    >
    void func2()
    {
    int a = func1();
    }
    >
    In func2 the initialization of 'a' is done by calling to func1().
    Is this ok accroding to ANSI C? (I thougt this is legal only in C+
    +...)
    Initialising automatic variables can be done with arbitrary
    expressions, including function calls.

    Initialising /static/ variables -- those explicitly declarared
    static, and variables declared outside functions -- requires a
    compile-time constant [1]. Maybe that's what you're thinking of?
    I need to know it because the project I work on will have to be ported
    to a different compiler (other than gcc), and I need to use ANSI C.
    Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear
    to use

    -Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin

    is your friend.

    [1] And for pointer values, something which is essentially "address of
    static possibly plus a constant".

    --
    "Only he had not reckoned with the ways of Estcarp." /Witch World/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

    Comment

    • viza

      #3
      Re: [OT] initializing a local variable using a function

      Hi

      On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
      Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear to
      use
      >
      -Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin
      Does -Wall really not include, um, all warnings? FFS gcc.

      Comment

      • Richard Heathfield

        #4
        Re: [OT] initializing a local variable using a function

        viza said:
        Hi
        >
        On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
        >
        >Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear to
        >use
        >>
        > -Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin
        >
        Does -Wall really not include, um, all warnings?
        Yes, it enables ALL the warnings... that the gcc team think are worth
        bothering with.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google Groups users, please read: <http://improve-usenet.org/>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • CBFalconer

          #5
          Re: [OT] initializing a local variable using a function

          viza wrote:
          Chris Dollin wrote:
          >
          >Then `-ansi -pedantic` and a whole bunch of other stuff -- I
          >appear to use
          >>
          > -Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin
          >
          Does -Wall really not include, um, all warnings? FFS gcc.
          No. To discover standardization problems with gcc use at least:

          -W -Wall -ansi -pedantic

          and I also recommend including -Wwrite-strings. For C99 (as far as
          gcc has gone on implementation) replace -ansi with -std=c99.

          Aha - you seem to be including all those, which I missed because of
          the unusual (to me) ordering of the options.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.


          Comment

          • Keith Thompson

            #6
            Re: [OT] initializing a local variable using a function

            viza <tom.viza@gmil. comwrites:
            On Wed, 02 Jul 2008 10:01:01 +0100, Chris Dollin wrote:
            >
            Then `-ansi -pedantic` and a whole bunch of other stuff -- I appear to
            use

            -Wall -Wno-unused -Wwrite-strings -W -Wmissing-prototypes -fno-builtin
            >
            Does -Wall really not include, um, all warnings? FFS gcc.
            No, -Wall doesn't enable all warnings. See the gcc documentation.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            Working...