main function

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

    #1

    main function

    I was reading the various posts in this newsgroup, and found
    that main function should be declared as
    int main(void) // if no arguments are passed to main

    i wanted to know whats the purpose of return value int ?
    where does main return ? As far as i know it is the first function
    to be given control. So where does this first function return ?

    If it returns, some function then how does this return value interpreted
    by that function ?

    thanx in advance for any help ...
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: main function

    junky_fellow <junky_fellow@y ahoo.co.in> wrote:[color=blue]
    > I was reading the various posts in this newsgroup, and found
    > that main function should be declared as
    > int main(void) // if no arguments are passed to main[/color]
    [color=blue]
    > i wanted to know whats the purpose of return value int ?
    > where does main return ? As far as i know it is the first function
    > to be given control. So where does this first function return ?[/color]

    Typically, a program has a (very system-dependent) startup function,
    which initializes everything for the program and then calls main().
    To this function main() returns and that function passes the return
    value back to the operating system. That in turn gives back that
    value (often after adding some additional bits of information) to
    whatever invoked the program, which then can use it to check if the
    program exited successfully or failed.
    [color=blue]
    > If it returns, some function then how does this return value interpreted
    > by that function ?[/color]

    E.g. under Unix it's a very common practice that within a shell script
    a program is started and afterwards its return value is checked to
    determine how to proceed. An (admittedly stupid) way to check for the
    existence of a file would be to call 'ls' (a program that lists files
    in a directory) with the name of the file as its argument. When it
    returns you can check the return value to find out if the file exists
    - if it does the 'ls' program returned a value indicating success,
    otherwise a value indicating failure. Using the return value is much
    simpler than having the script read and interpret the output of the
    'ls' program.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Emmanuel Delahaye

      #3
      Re: main function

      junky_fellow wrote on 13/08/04 :[color=blue]
      > I was reading the various posts in this newsgroup, and found
      > that main function should be declared as
      > int main(void) // if no arguments are passed to main[/color]

      Correct.
      [color=blue]
      > i wanted to know whats the purpose of return value int ?
      > where does main return ? As far as i know it is the first function
      > to be given control. So where does this first function return ?[/color]

      main() and exit() return a value to the system.
      [color=blue]
      > If it returns, some function then how does this return value interpreted
      > by that function ?[/color]

      Yes. This value can be interpreted by the current command interpreter
      (shell, bash, command.com or whatever), specifically in scripts (or
      batch etc.) to act on decisions (if ...).

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

      "C is a sharp tool"

      Comment

      • suri

        #4
        Re: main function

        Jens.Toerring@p hysik.fu-berlin.de wrote in message news:<2o3jmeF6b tt3U1@uni-berlin.de>...[color=blue]
        > junky_fellow <junky_fellow@y ahoo.co.in> wrote:[color=green]
        > > I was reading the various posts in this newsgroup, and found
        > > that main function should be declared as
        > > int main(void) // if no arguments are passed to main[/color]
        >[color=green]
        > > i wanted to know whats the purpose of return value int ?
        > > where does main return ? As far as i know it is the first function
        > > to be given control. So where does this first function return ?[/color]
        >
        > Typically, a program has a (very system-dependent) startup function,
        > which initializes everything for the program and then calls main().
        > To this function main() returns and that function passes the return
        > value back to the operating system. That in turn gives back that
        > value (often after adding some additional bits of information) to
        > whatever invoked the program, which then can use it to check if the
        > program exited successfully or failed.
        >[color=green]
        > > If it returns, some function then how does this return value interpreted
        > > by that function ?[/color]
        >
        > E.g. under Unix it's a very common practice that within a shell script
        > a program is started and afterwards its return value is checked to
        > determine how to proceed. An (admittedly stupid) way to check for the
        > existence of a file would be to call 'ls' (a program that lists files
        > in a directory) with the name of the file as its argument. When it
        > returns you can check the return value to find out if the file exists
        > - if it does the 'ls' program returned a value indicating success,
        > otherwise a value indicating failure. Using the return value is much
        > simpler than having the script read and interpret the output of the
        > 'ls' program.
        > Regards, Jens[/color]


        I used linux and there are some commands, to check the exit status of
        the previous programs, In that case the return value from main will be
        Used to check wheater the program is successful or not

        Comment

        • Kenny McCormack

          #5
          Re: main function

          In article <mn.6b307d48a53 c573a.15512@YOU RBRAnoos.fr>,
          Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=blue]
          >junky_fellow wrote on 13/08/04 :[color=green]
          >> I was reading the various posts in this newsgroup, and found
          >> that main function should be declared as
          >> int main(void) // if no arguments are passed to main[/color]
          >
          >Correct.[/color]

          Not really.

          I'm surprised that no one has pointed out that this fragment (pick one or
          more of the following, as you see fit):
          1) Isn't C (*)
          2) Doesnt't compile in c.l.c (**)
          3) Is OT here.

          (*) comp.lang.c++ is down the hall.
          (**) Might compile in C99 - but we're mostly C89 around here.

          Comment

          • Kieran Simkin

            #6
            Re: main function

            "Kenny McCormack" <gazelle@yin.in teraccess.com> wrote in message
            news:cfin8t$m83 $1@yin.interacc ess.com...[color=blue]
            > In article <mn.6b307d48a53 c573a.15512@YOU RBRAnoos.fr>,
            > Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=green]
            >>junky_fello w wrote on 13/08/04 :[color=darkred]
            >>> I was reading the various posts in this newsgroup, and found
            >>> that main function should be declared as
            >>> int main(void) // if no arguments are passed to main[/color]
            >>
            >>Correct.[/color]
            >
            > Not really.
            >
            > I'm surprised that no one has pointed out that this fragment (pick one or
            > more of the following, as you see fit):
            > 1) Isn't C (*)[/color]

            Please explain how this isn't C, I don't understand what makes this
            definition C++ or C99 rather than C(89).
            [color=blue]
            > 2) Doesnt't compile in c.l.c (**)
            > 3) Is OT here.
            >
            > (*) comp.lang.c++ is down the hall.
            > (**) Might compile in C99 - but we're mostly C89 around here.
            >[/color]


            Comment

            • Rich Grise

              #7
              Re: main function

              Kieran Simkin wrote:
              [color=blue]
              > Please explain how this isn't C, I don't understand what makes this
              > definition C++ or C99 rather than C(89).[/color]

              You've snipped the very part you're asking about. That's really stupid.
              But, since I'm so fucking wonderful, I'll do your homework for you:

              ---<quote>---
              int main(void)  // if no arguments are passed to main
              ---<endquote>---

              It's not C because '//' is a syntax error.

              Now, please be kind enough to go read something.

              HTH!
              Rich

              Comment

              • Keith Thompson

                #8
                Re: main function

                gazelle@yin.int eraccess.com (Kenny McCormack) writes:[color=blue]
                > In article <mn.6b307d48a53 c573a.15512@YOU RBRAnoos.fr>,
                > Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:[color=green]
                > >junky_fellow wrote on 13/08/04 :[color=darkred]
                > >> I was reading the various posts in this newsgroup, and found
                > >> that main function should be declared as
                > >> int main(void) // if no arguments are passed to main[/color]
                > >
                > >Correct.[/color]
                >
                > Not really.
                >
                > I'm surprised that no one has pointed out that this fragment (pick one or
                > more of the following, as you see fit):
                > 1) Isn't C (*)
                > 2) Doesnt't compile in c.l.c (**)
                > 3) Is OT here.
                >
                > (*) comp.lang.c++ is down the hall.
                > (**) Might compile in C99 - but we're mostly C89 around here.[/color]

                Kenny's complaint is that "//" comments are not supported in ANSI C89
                (or, equivalently, in ISO C90). They are supported in ISO C99 (and in
                C++, but that's beside the point).

                It's certainly worth pointing out that "//" comments are not supported
                in what is still the most commonly implemented C standard. It is
                absurd, however, to claim that they aren't C.

                We frequently discuss C99 here. We also discuss C90, and K&R C, and
                sometimes even the versions that preceded the publication of K&R1.

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

                • Keith Thompson

                  #9
                  Re: main function

                  Rich Grise <null@example.n et> writes:[color=blue]
                  > Kieran Simkin wrote:[color=green]
                  > > Please explain how this isn't C, I don't understand what makes this
                  > > definition C++ or C99 rather than C(89).[/color]
                  >
                  > You've snipped the very part you're asking about. That's really stupid.
                  > But, since I'm so ****ing wonderful, I'll do your homework for you:
                  >
                  > ---<quote>---
                  > int main(void)  // if no arguments are passed to main
                  > ---<endquote>---
                  >
                  > It's not C because '//' is a syntax error.
                  >
                  > Now, please be kind enough to go read something.
                  >
                  > HTH!
                  > Rich[/color]

                  [Quotation edited for content.]

                  Rich, Kieran's question was perfectly reasonable, and your response
                  was rude and incomplete (and arguably incorrect).

                  "//" comments are illegal in C90, but legal in C99. That's certainly
                  worth pointing out, and there are other valid reasons not to use "//"
                  comments when posting to comp.lang.c (line wrapping can cause
                  problems), but claiming that it's "not C" is misleading at best.

                  I'll also point out that Kieran did not snip anything; he quoted the
                  entire article to which he was following up.

                  Rich, you may indeed be ****ing wonderful most of the time, but this
                  was not your finest moment. You might want to re-read your own
                  excellent article <3df9fd6c.03080 71505.5083605c@ posting.google. com>,
                  posted to comp.lang.c about a year ago (2003-08-07).

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

                  • E. Robert Tisdale

                    #10
                    Re: main function

                    Emmanuel Delahaye wrote:
                    [color=blue]
                    > junky_fellow wrote:
                    >[color=green]
                    >> I was reading the various posts in this newsgroup
                    >> and found that main function should be declared as
                    >> int main(void) // if no arguments are passed to main[/color]
                    >
                    > Correct.[/color]

                    No.

                    The C programming language does *not* specify
                    how function main is to be called.
                    Generally, programmers have no control
                    over what arguments are *passed* to main.
                    The programmer decides only which arguments main will *accept*.

                    My C++ programs call

                    int main(int argc, char* argv[], char* env[]);

                    Other C++ compilers must, at least, emit code to call

                    int main(int argc, char* argv[]);

                    When you define

                    int main(void);

                    all you are telling the compiler is that
                    main will ignore all of the arguments passed to it.

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: main function

                      Kenny McCormack wrote:
                      [color=blue]
                      > In article <mn.6b307d48a53 c573a.15512@YOU RBRAnoos.fr>,
                      > Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote:
                      >[color=green]
                      >>junky_fello w wrote on 13/08/04 :
                      >>[color=darkred]
                      >>>I was reading the various posts in this newsgroup, and found
                      >>>that main function should be declared as
                      >>>int main(void) // if no arguments are passed to main[/color]
                      >>
                      >>Correct.[/color]
                      >
                      >
                      > Not really.
                      >
                      > I'm surprised that no one has pointed out that this fragment (pick one or
                      > more of the following, as you see fit):
                      > 1) Isn't C (*)
                      > 2) Doesnt't compile in c.l.c (**)
                      > 3) Is OT here.[/color]

                      I'm more surprised that someone would be so ignorant as to claim that C
                      code isn't C, doesn't compile, and is off-topic.
                      [color=blue]
                      > (*) comp.lang.c++ is down the hall.
                      > (**) Might compile in C99 - but we're mostly C89 around here.[/color]

                      The current standard defines the current standard. CLC has historically
                      allowed legacy "standards. " After C89 was adopted, K&R C continued to
                      be considered topical. Now that C99 has been adopted, C89 (C90)
                      continues to be considered topical. Only a damn fool would consider
                      questions about code written according to the current C standard as
                      off-topic.


                      Comment

                      • Martin Ambuhl

                        #12
                        Re: main function

                        Rich Grise wrote:
                        [color=blue]
                        > Kieran Simkin wrote:
                        >
                        >[color=green]
                        >>Please explain how this isn't C, I don't understand what makes this
                        >>definition C++ or C99 rather than C(89).[/color]
                        >
                        >
                        > You've snipped the very part you're asking about. That's really stupid.
                        > But, since I'm so fucking wonderful, I'll do your homework for you:
                        >
                        > ---<quote>---
                        > int main(void) // if no arguments are passed to main
                        > ---<endquote>---
                        >
                        > It's not C because '//' is a syntax error.[/color]

                        No, it isn't. Where did the trolls Kenny McCormack and Rich Grise come
                        from?
                        [color=blue]
                        > Now, please be kind enough to go read something.[/color]

                        Good advise. Follow it.

                        Comment

                        • junky_fellow

                          #13
                          Re: main function

                          Jens.Toerring@p hysik.fu-berlin.de wrote in message news:<2o3jmeF6b tt3U1@uni-berlin.de>...[color=blue]
                          > junky_fellow <junky_fellow@y ahoo.co.in> wrote:[color=green]
                          > > I was reading the various posts in this newsgroup, and found
                          > > that main function should be declared as
                          > > int main(void) // if no arguments are passed to main[/color]
                          >[color=green]
                          > > i wanted to know whats the purpose of return value int ?
                          > > where does main return ? As far as i know it is the first function
                          > > to be given control. So where does this first function return ?[/color]
                          >
                          > Typically, a program has a (very system-dependent) startup function,
                          > which initializes everything for the program and then calls main().
                          > To this function main() returns and that function passes the return
                          > value back to the operating system. That in turn gives back that
                          > value (often after adding some additional bits of information) to
                          > whatever invoked the program, which then can use it to check if the
                          > program exited successfully or failed.
                          >[color=green]
                          > > If it returns, some function then how does this return value interpreted
                          > > by that function ?[/color]
                          >
                          > E.g. under Unix it's a very common practice that within a shell script
                          > a program is started and afterwards its return value is checked to
                          > determine how to proceed. An (admittedly stupid) way to check for the
                          > existence of a file would be to call 'ls' (a program that lists files
                          > in a directory) with the name of the file as its argument. When it
                          > returns you can check the return value to find out if the file exists
                          > - if it does the 'ls' program returned a value indicating success,
                          > otherwise a value indicating failure. Using the return value is much
                          > simpler than having the script read and interpret the output of the
                          > 'ls' program.
                          > Regards, Jens[/color]

                          thank you all who devoted their precious time to answer the stupid questions
                          of a "c learner".

                          Comment

                          • Keith Thompson

                            #14
                            Re: main function

                            junky_fellow@ya hoo.co.in (junky_fellow) writes:
                            [...][color=blue]
                            > thank you all who devoted their precious time to answer the stupid
                            > questions of a "c learner".[/color]

                            Contrary to popular belief, there *are* stupid questions (I've seen
                            some in this newsgroup), but yours wasn't one of them.

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

                            • Rich Grise

                              #15
                              Re: main function

                              Martin Ambuhl wrote:
                              [color=blue]
                              > Rich Grise wrote:
                              >[color=green]
                              >> Kieran Simkin wrote:
                              >>
                              >>[color=darkred]
                              >>>Please explain how this isn't C, I don't understand what makes this
                              >>>definition C++ or C99 rather than C(89).[/color]
                              >>
                              >>
                              >> You've snipped the very part you're asking about. That's really stupid.
                              >> But, since I'm so fucking wonderful, I'll do your homework for you:
                              >>
                              >> ---<quote>---
                              >> int main(void) // if no arguments are passed to main
                              >> ---<endquote>---
                              >>
                              >> It's not C because '//' is a syntax error.[/color]
                              >
                              > No, it isn't. Where did the trolls Kenny McCormack and Rich Grise come
                              > from?[/color]

                              Sorry, just passing through.[color=blue]
                              >[color=green]
                              >> Now, please be kind enough to go read something.[/color]
                              >
                              > Good advise. Follow it.[/color]

                              OK, good idea. :-)

                              Thanks,
                              Rich

                              Comment

                              Working...