question on main( )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    question on main( )

    Suppose the return type of main is void.

    void main(void)
    {
    ...

    return;
    }

    Is the compiler supposed to generate warning or error according to
    Standard C.

    I use return type as int only. But for knowledge sake I am asking this
    question.

  • santosh

    #2
    Re: question on main( )

    subramanian100i n@yahoo.com, India wrote:
    Suppose the return type of main is void.
    >
    void main(void)
    {
    ...
    >
    return;
    }
    >
    Is the compiler supposed to generate warning or error according to
    Standard C.
    Yes. A diagnostic is required.

    Comment

    • Keith Thompson

      #3
      Re: question on main( )

      "santosh" <santosh.k83@gm ail.comwrites:
      subramanian100i n@yahoo.com, India wrote:
      >Suppose the return type of main is void.
      >>
      >void main(void)
      >{
      > ...
      >>
      > return;
      >}
      >>
      >Is the compiler supposed to generate warning or error according to
      >Standard C.
      >
      Yes. A diagnostic is required.
      I don't believe so. C99 allows the main function to be defined in one
      of the two normal ways "or in some other implementation-defined
      manner". Even if the implementation doesn't define another manner for
      declaring main, the requirement is not a constraint or a syntax rule,
      so no diagnostic is required; it invokes undefined behavior.

      The rules are similar for C90, except that the older standard doesn't
      explicitly allow implementation-defined forms.

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

      Comment

      • Flash Gordon

        #4
        Re: question on main( )

        santosh wrote, On 07/03/07 03:49:
        subramanian100i n@yahoo.com, India wrote:
        >Suppose the return type of main is void.
        >>
        >void main(void)
        >{
        > ...
        >>
        > return;
        >}
        >>
        >Is the compiler supposed to generate warning or error according to
        >Standard C.
        >
        Yes. A diagnostic is required.
        C&V? I know the standard specifies exactly two variants for main (and
        allows the implementation to define more) but I don't believe that it is
        in a constraint, so I think it is undefined behaviour rather than
        requiring a diagnostic.
        --
        Flash Gordon

        Comment

        • santosh

          #5
          Re: question on main( )


          Flash Gordon wrote:
          santosh wrote, On 07/03/07 03:49:
          subramanian100i n@yahoo.com, India wrote:
          Suppose the return type of main is void.
          >
          void main(void)
          {
          ...
          >
          return;
          }
          >
          Is the compiler supposed to generate warning or error according to
          Standard C.
          Yes. A diagnostic is required.
          >
          C&V? I know the standard specifies exactly two variants for main (and
          allows the implementation to define more) but I don't believe that it is
          in a constraint, so I think it is undefined behaviour rather than
          requiring a diagnostic.
          Flash, (and Keith), thanks for the correction. That's what happens
          when you work too much with a single compiler, (gcc), and not read the
          Standard carefully.

          Comment

          • Richard Heathfield

            #6
            Re: question on main( )

            subramanian100i n@yahoo.com, India said:
            Suppose the return type of main is void.
            The return type of main is int. Supposing it is void is like supposing
            the earth is a cube. It simply ain't so.
            void main(void)
            {
            ...
            >
            return;
            }
            >
            Is the compiler supposed to generate warning or error according to
            Standard C.
            No, the compiler is under no obligation to diagnose an incorrect return
            type, any more than it is obliged to diagnose i=i++ or fflush(stdin),
            but void main is just as wrong as either of these.

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at the above domain, - www.

            Comment

            Working...