print "foo" without using ;

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

    #16
    Re: print "foo&qu ot; without using ;

    akarl wrote:
    [color=blue]
    > OK, I didn't know that. Seems like quite a few people in this group has
    > complained when someone has posted a main function lacking a return
    > statement. Hence my impression of it being mandatory.[/color]

    C89 requires a return; C99 doesn't. Almost everyone posting has a C89
    compiler; almost no one posting here has a C99 compiler. Doesn't it
    make sense to require the return? Consider that there was no good
    reason for the C99 standardization committee to allow this one exception
    to the rule that functions that return values do so explicitly. The
    inexcusable reason is that too many bad programmers had been writing
    code in violation of the standard for the last 10 years.

    Comment

    • Walter Roberson

      #17
      Re: print "foo&qu ot; without using ;

      In article <sjLOe.32247$d5 .187003@newsb.t elia.net>,
      akarl <fusionfive@com hem.se> wrote:[color=blue]
      >OK, I didn't know that. Seems like quite a few people in this group has
      >complained when someone has posted a main function lacking a return
      >statement. Hence my impression of it being mandatory.[/color]

      C89 and C99 differ on this point. C99 -defines- falling out of main
      as an exit with 0 status. C89 says that if you do that then the resulting
      exit status is undefined. An undefined exit status might not matter
      much of the time, but you don't want to get into the bad habit of
      leaving it up to chance.
      --
      Feep if you love VT-52's.

      Comment

      • Randy Howard

        #18
        Re: print &quot;foo&qu ot; without using ;

        Rajesh wrote
        (in article
        <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
        [color=blue]
        > neglect that 'int main()' use 'void main()'[/color]

        I wish more candidates would try "void main()" right away, it
        would shorten those interviews and leave more time for the
        better candidates.


        --
        Randy Howard (2reply remove FOOBAR)

        Comment

        • Mark

          #19
          Re: print &quot;foo&qu ot; without using ;

          "Randy Howard" <randyhoward@FO OverizonBAR.net > wrote in message
          news:0001HW.BF3 1001200F12155F0 407550@news.ver izon.net...[color=blue]
          > Rajesh wrote
          > (in article
          > <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
          >[color=green]
          >> neglect that 'int main()' use 'void main()'[/color]
          >
          > I wish more candidates would try "void main()" right away, it
          > would shorten those interviews and leave more time for the
          > better candidates.[/color]

          You assume that the people conducting the interviews
          know better? I'd wager that many would view void main()
          as being perfectly acceptable.

          The problem stems from the fact that many text books
          teach people to program using void main(); in their
          examples. Unless one is lucky enough to stumble
          across one of the better books (or a forum such as
          comp.lang.c) which points out the (portability) issue
          they'll never know that void main(); is inappropriate.

          Mark


          Comment

          • Lawrence Kirby

            #20
            Re: print &quot;foo&qu ot; without using ;

            On Tue, 23 Aug 2005 21:32:17 +0000, Randy Howard wrote:
            [color=blue]
            > Rajesh wrote
            > (in article
            > <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
            >[color=green]
            >> neglect that 'int main()' use 'void main()'[/color]
            >
            > I wish more candidates would try "void main()" right away, it
            > would shorten those interviews and leave more time for the
            > better candidates.[/color]

            Don't forget this was a "M$ interview". The normal rules of C don't apply,
            which makes this question off topic. :-)

            Lawrence



            Comment

            • Lawrence Kirby

              #21
              Re: print &quot;foo&qu ot; without using ;

              On Tue, 23 Aug 2005 20:16:37 +0000, Martin Ambuhl wrote:
              [color=blue]
              > akarl wrote:
              >[color=green]
              >> OK, I didn't know that. Seems like quite a few people in this group has
              >> complained when someone has posted a main function lacking a return
              >> statement. Hence my impression of it being mandatory.[/color]
              >
              > C89 requires a return; C99 doesn't. Almost everyone posting has a C89
              > compiler; almost no one posting here has a C99 compiler. Doesn't it
              > make sense to require the return? Consider that there was no good
              > reason for the C99 standardization committee to allow this one exception
              > to the rule that functions that return values do so explicitly. The
              > inexcusable reason is that too many bad programmers had been writing
              > code in violation of the standard for the last 10 years.[/color]

              C89 doesn't require a return. If you fall off the end of main() you get an
              undefined termination status but not an invalid program. If the
              program doesn't fall off the end of main() there's no problem at all.

              However an undefined termination status is undesirable in normal
              circumstances so falling off the end of main() is considered a bad thing,
              hence the initial point above.

              Lawrence

              Comment

              • Kenneth Brody

                #22
                Re: print &quot;foo&qu ot; without using ;

                Martin Ambuhl wrote:[color=blue]
                >
                > akarl wrote:
                >[color=green]
                > > OK, I didn't know that. Seems like quite a few people in this group has
                > > complained when someone has posted a main function lacking a return
                > > statement. Hence my impression of it being mandatory.[/color]
                >
                > C89 requires a return; C99 doesn't. Almost everyone posting has a C89
                > compiler; almost no one posting here has a C99 compiler. Doesn't it
                > make sense to require the return? Consider that there was no good
                > reason for the C99 standardization committee to allow this one exception
                > to the rule that functions that return values do so explicitly. The
                > inexcusable reason is that too many bad programmers had been writing
                > code in violation of the standard for the last 10 years.[/color]

                Hmm... Using MSVC (hardly a "standard", I know)...

                int main()
                {
                foo();
                }
                int foo()
                {
                }

                This complains that foo() doesn't have a return, but doesn't complain
                about main() not having a return. (Even with warnings all the way up.)
                So, they appear to allow C99's exception. However, looking at the
                generated assembly, they still follow the older "return from main is
                the value of the last statement". (Basically, in MSVC's case, it means
                that EAX isn't assigned any specific value, and so main will return
                whatever foo returned.)

                --
                +-------------------------+--------------------+-----------------------------+
                | Kenneth J. Brody | www.hvcomputer.com | |
                | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                +-------------------------+--------------------+-----------------------------+
                Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                Comment

                • Randy Howard

                  #23
                  Re: print &quot;foo&qu ot; without using ;

                  Mark wrote
                  (in article <MWMOe.348$hn4. 57079@newshog.n ewsread.com>):
                  [color=blue]
                  > "Randy Howard" <randyhoward@FO OverizonBAR.net > wrote in message
                  > news:0001HW.BF3 1001200F12155F0 407550@news.ver izon.net...[color=green]
                  >> Rajesh wrote
                  >> (in article
                  >> <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
                  >>[color=darkred]
                  >>> neglect that 'int main()' use 'void main()'[/color]
                  >>
                  >> I wish more candidates would try "void main()" right away, it
                  >> would shorten those interviews and leave more time for the
                  >> better candidates.[/color]
                  >
                  > You assume that the people conducting the interviews
                  > know better? I'd wager that many would view void main()
                  > as being perfectly acceptable.[/color]

                  Since I was speaking about myself, I know that the interviewer
                  knows better. :-)
                  [color=blue]
                  > The problem stems from the fact that many text books
                  > teach people to program using void main(); in their
                  > examples.[/color]

                  Most famously Schildt I suspect. Since for some reason authors
                  that commit this mistake to print are extremely reluctant to
                  correct it in subsequent printings or even errata (I know,
                  because I have tried on several occasions, and been blown off or
                  insulted for my troubles, despite all attempts at making the
                  case politely).
                  [color=blue]
                  > Unless one is lucky enough to stumble
                  > across one of the better books (or a forum such as
                  > comp.lang.c) which points out the (portability) issue
                  > they'll never know that void main(); is inappropriate.[/color]

                  Unless they bother to actually check a standard document.

                  I do wish gcc would spit out a warning on void main() except for
                  the embedded variants.


                  --
                  Randy Howard (2reply remove FOOBAR)

                  Comment

                  • Keith Thompson

                    #24
                    Re: print &quot;foo&qu ot; without using ;

                    Randy Howard <randyhoward@FO OverizonBAR.net > writes:
                    [...][color=blue]
                    > I do wish gcc would spit out a warning on void main() except for
                    > the embedded variants.[/color]

                    It does. I just tried versions 2.8.1, 2.95.2, 3.0.4, 3.4.4, and
                    4.0.0; they all print

                    void-main.c:2: warning: return type of `main' is not `int'

                    for the following:

                    void main(void)
                    {
                    }

                    even without any special options.

                    Unless you're saying you *don't* want the warning for the embedded
                    variants?

                    --
                    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.

                    Comment

                    • Fao, Sean

                      #25
                      Re: print &quot;foo&qu ot; without using ;

                      Randy Howard wrote:[color=blue]
                      > Rajesh wrote
                      > (in article
                      > <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
                      >[color=green]
                      >>neglect that 'int main()' use 'void main()'[/color]
                      >
                      > I wish more candidates would try "void main()" right away, it
                      > would shorten those interviews and leave more time for the
                      > better candidates.[/color]

                      Mostly off-topic, but perhaps surprising is the fact that Microsoft
                      actually accepts void as a valid return type.

                      http://msdn.microsoft.com./library/d...n_function.asp

                      --
                      Sean

                      Comment

                      • Fao, Sean

                        #26
                        Re: print &quot;foo&qu ot; without using ;

                        Rajesh wrote:[color=blue]
                        > I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no.
                        > of ways to achive it.
                        > mine sol. was....[/color]

                        Maybe I'm wrong, but I think everybody here is taking this too far. I
                        believe the intent of the interview question applied only to the line
                        printing "foo", which would make a return statement at the end a
                        perfectly legal solution.

                        #include <stdio.h>

                        int main(void)
                        {
                        if (printf("foo\n" ))
                        {
                        }

                        return 0;
                        }

                        Agreed, it's a stupid question. But knowing the answer does
                        --somewhat-- prove an open-minded solution to a problem that tests ones
                        knowledge of C.

                        I already posted this link in another reply; but, it should be noted
                        that Microsoft is one of the few implementors that *has* defined a void
                        return type for function main().

                        http://msdn.microsoft.com./library/d...n_function.asp

                        --
                        Sean

                        Comment

                        • Denis Kasak

                          #27
                          Re: print &quot;foo&qu ot; without using ;

                          Fao, Sean wrote:[color=blue]
                          >
                          > Mostly off-topic, but perhaps surprising is the fact that Microsoft
                          > actually accepts void as a valid return type.
                          >
                          > http://msdn.microsoft.com./library/d...n_function.asp[/color]

                          Sometimes I wonder if they're deliberately trying to break things.

                          -- Denis

                          Comment

                          • Flash Gordon

                            #28
                            Re: print &quot;foo&qu ot; without using ;

                            Fao, Sean wrote:[color=blue]
                            > Randy Howard wrote:
                            >[color=green]
                            >> Rajesh wrote
                            >> (in article <1124807254.654 883.178430@g43g 2000cwa.googleg roups.com>):
                            >>[color=darkred]
                            >>> neglect that 'int main()' use 'void main()'[/color]
                            >>
                            >> I wish more candidates would try "void main()" right away, it would
                            >> shorten those interviews and leave more time for the better candidates.[/color]
                            >
                            > Mostly off-topic, but perhaps surprising is the fact that Microsoft
                            > actually accepts void as a valid return type.
                            >
                            > http://msdn.microsoft.com./library/d...n_function.asp[/color]

                            Not at all surprising around here since we have discussed the fact that
                            MS often declares main as returning void in its examples.

                            The standard does not prohibit MS from doing this, it is just that it is
                            not portable since not all systems define it.
                            --
                            Flash Gordon
                            Living in interesting times.
                            Although my email address says spam, it is real and I read it.

                            Comment

                            • Keith Thompson

                              #29
                              Re: print &quot;foo&qu ot; without using ;

                              "Fao, Sean" <enceladus311@y ahoo.comI-WANT-NO-SPAM> writes:
                              [...][color=blue]
                              > Mostly off-topic, but perhaps surprising is the fact that Microsoft
                              > actually accepts void as a valid return type.
                              >
                              > http://msdn.microsoft.com./library/d...n_function.asp[/color]

                              That web page refers to C++, not C.

                              --
                              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.

                              Comment

                              • Old Wolf

                                #30
                                Re: print &quot;foo&qu ot; without using ;

                                Fao, Sean wrote:[color=blue]
                                > Rajesh wrote:[color=green]
                                >> I was informed tht $SUBJECT was asked in M$ interview. Real imp.
                                >> is no. of ways to achive it. mine sol. was....[/color]
                                >
                                > Maybe I'm wrong, but I think everybody here is taking this too
                                > far. I believe the intent of the interview question applied only
                                > to the line printing "foo", which would make a return statement at
                                > the end a perfectly legal solution.
                                >
                                > #include <stdio.h>
                                >
                                > int main(void)
                                > {
                                > if (printf("foo\n" ))
                                > {
                                > }
                                >
                                > return 0;
                                > }[/color]

                                Unlikely, eg:

                                #include <stdio.h>
                                int main()
                                {
                                /* here is the line that prints foo! */
                                puts("foo")

                                /* there it was */

                                ; return 0;
                                }

                                Of course, as has been pointed out on this thread, main does not
                                need a return statement.

                                Comment

                                Working...