What does this code mean???

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

    What does this code mean???

    I am reading the code of a program from the examples, but really don't
    understand this one........

    int i,j;

    ............... ............... ............... ........

    while (scanf("%d",&i) ,j)
    {
    ............... ............... ..........
    }

    So I think the question should become,
    how does the compiler evaluate the expression, (int i,int j)??
    (am I right?? 'cause scanf function returns an integer as I know)
  • Ulrich Eckhardt

    #2
    Re: What does this code mean???

    Mars wrote:[color=blue]
    > I am reading the code of a program from the examples, but really don't
    > understand this one........
    >
    > int i,j;
    >
    > ............... ............... ............... .......
    >
    > while (scanf("%d",&i) ,j)
    > {
    > ............... ............... .........
    > }
    >
    > So I think the question should become,
    > how does the compiler evaluate the expression, (int i,int j)??[/color]

    That's a typical application of the comma-operator, look it up in your
    books.
    [color=blue]
    > (am I right?? 'cause scanf function returns an integer as I know)[/color]

    You really should not need to ask what scanf() returns and what that
    returnvalue means, you should know where to find that info. You are right
    with your assumption though.

    Uli

    Comment

    • Jonathan Burd

      #3
      Re: What does this code mean???

      Mars wrote:[color=blue]
      > I am reading the code of a program from the examples, but really don't
      > understand this one........
      >
      > int i,j;
      >
      > ............... ............... ............... .......
      >
      > while (scanf("%d",&i) ,j)
      > {
      > ............... ............... .........
      > }[/color]

      Break it down like this:

      scanf("%d", &i), j

      The value returned by scanf is discarded and the whole expression
      evaluates to the value of j. A sequence point occurs
      at the comma. (See Annex C of the C Standard).

      The loop is reading in values and will continue executing
      while j is not 0.

      As an example, see what this does:

      /* comma.c */
      #include <stdio.h>

      int
      main (void)
      {
      int a, b = 6;
      int ret;

      ret = (a = 5, b);

      printf ("a: %d; ret: %d\n", a, ret);

      return 0;
      }

      Regards,
      Jonathan.

      --
      "We must do something. This is something. Therefore, we must do this."
      - Keith Thompson

      Comment

      • Nick Austin

        #4
        Re: What does this code mean???

        On Sun, 30 Jan 2005 19:20:52 +0800, Mars <Mars@Mars> wrote:
        [color=blue]
        >I am reading the code of a program from the examples, but really don't
        >understand this one........
        >
        >int i,j;
        >
        >.............. ............... ............... ........
        >
        >while (scanf("%d",&i) ,j)
        >{
        >.............. ............... ..........
        >}[/color]

        It's the comma operator. The call to scanf() occurs as part of
        the loop but occurs before evaluating the end condition.

        It's the same as either:

        scanf("%d",&i);
        while (j)
        {
        /*.............. ............... .......... */
        scanf("%d",&i);
        }

        or:

        while (1)
        {
        scanf("%d",&i);
        if (!j)
        break;
        /*.............. ............... .......... */
        }

        Nick.

        Comment

        • Mars

          #5
          Re: What does this code mean???

          icic, thx~


          Jonathan Burd mentioned:[color=blue]
          > Mars wrote:
          >[color=green]
          >>I am reading the code of a program from the examples, but really don't
          >>understand this one........
          >>
          >>int i,j;
          >>
          >>............. ............... ............... .........
          >>
          >>while (scanf("%d",&i) ,j)
          >>{
          >>............. ............... ...........
          >>}[/color]
          >
          >
          > Break it down like this:
          >
          > scanf("%d", &i), j
          >
          > The value returned by scanf is discarded and the whole expression
          > evaluates to the value of j. A sequence point occurs
          > at the comma. (See Annex C of the C Standard).
          >
          > The loop is reading in values and will continue executing
          > while j is not 0.
          >
          > As an example, see what this does:
          >
          > /* comma.c */
          > #include <stdio.h>
          >
          > int
          > main (void)
          > {
          > int a, b = 6;
          > int ret;
          >
          > ret = (a = 5, b);
          >
          > printf ("a: %d; ret: %d\n", a, ret);
          >
          > return 0;
          > }
          >
          > Regards,
          > Jonathan.
          >[/color]

          Comment

          • Mars

            #6
            Re: What does this code mean???

            Thx~~


            Nick Austin mentioned:[color=blue]
            > On Sun, 30 Jan 2005 19:20:52 +0800, Mars <Mars@Mars> wrote:
            >
            >[color=green]
            >>I am reading the code of a program from the examples, but really don't
            >>understand this one........
            >>
            >>int i,j;
            >>
            >>............. ............... ............... .........
            >>
            >>while (scanf("%d",&i) ,j)
            >>{
            >>............. ............... ...........
            >>}[/color]
            >
            >
            > It's the comma operator. The call to scanf() occurs as part of
            > the loop but occurs before evaluating the end condition.
            >
            > It's the same as either:
            >
            > scanf("%d",&i);
            > while (j)
            > {
            > /*.............. ............... .......... */
            > scanf("%d",&i);
            > }
            >
            > or:
            >
            > while (1)
            > {
            > scanf("%d",&i);
            > if (!j)
            > break;
            > /*.............. ............... .......... */
            > }
            >
            > Nick.
            >[/color]

            Comment

            • Jonathan Burd

              #7
              Re: What does this code mean???

              Mars wrote:[color=blue]
              > icic, thx~[/color]

              <snip>

              Please do not top-post in c.l.c.
              Top-posting blurs the context and makes it
              difficult to see what you are replying to.

              Regards,
              Jonathan.

              --
              "We must do something. This is something. Therefore, we must do this."
              - Keith Thompson

              Comment

              • Richard Bos

                #8
                Re: What does this code mean???

                Jonathan Burd <jonathan.burd@ REMOVEMEgmail.c om> wrote:
                [color=blue]
                > Mars wrote:[color=green]
                > > while (scanf("%d",&i) ,j)[/color][/color]
                [color=blue]
                > Break it down like this:
                >
                > scanf("%d", &i), j
                >
                > The value returned by scanf is discarded and the whole expression
                > evaluates to the value of j. A sequence point occurs
                > at the comma. (See Annex C of the C Standard).
                >
                > The loop is reading in values and will continue executing
                > while j is not 0.[/color]

                True, but I must say that this is an inadvisable way of using the comma
                operator. The scanf() call has nothing to do with whether or not the
                loop continues at all. This use of the comma is mere obfuscation.

                Richard

                Comment

                • Servé La

                  #9
                  Re: What does this code mean???


                  "Richard Bos" <rlb@hoekstra-uitgeverij.nl> schreef in bericht
                  news:41fdeebe.2 54654545@news.i ndividual.net.. .
                  [color=blue]
                  > True, but I must say that this is an inadvisable way of using the comma
                  > operator. The scanf() call has nothing to do with whether or not the
                  > loop continues at all. This use of the comma is mere obfuscation.[/color]

                  That's your opinion of course. I like this style because the return value of
                  scanf is often ignored anyway and you dont need to put in 2 calls to scanf
                  as in:

                  scanf();
                  while(j)
                  {
                  scanf();
                  }


                  Comment

                  • Lawrence Kirby

                    #10
                    Re: What does this code mean???

                    On Mon, 31 Jan 2005 14:49:30 +0100, Servé La wrote:
                    [color=blue]
                    >
                    > "Richard Bos" <rlb@hoekstra-uitgeverij.nl> schreef in bericht
                    > news:41fdeebe.2 54654545@news.i ndividual.net.. .
                    >[color=green]
                    >> True, but I must say that this is an inadvisable way of using the comma
                    >> operator. The scanf() call has nothing to do with whether or not the
                    >> loop continues at all. This use of the comma is mere obfuscation.[/color]
                    >
                    > That's your opinion of course. I like this style because the return value of
                    > scanf is often ignored anyway[/color]

                    Unfortunately true. Consider however there are very few situations where
                    ignoring the return value of scanf() isn't a bug.

                    Lawrence

                    Comment

                    • Eric Sosman

                      #11
                      Re: What does this code mean???



                      Servé La wrote:[color=blue]
                      > "Richard Bos" <rlb@hoekstra-uitgeverij.nl> schreef in bericht
                      > news:41fdeebe.2 54654545@news.i ndividual.net.. .
                      >
                      > [color=green]
                      >>True, but I must say that this is an inadvisable way of using the comma
                      >>operator. The scanf() call has nothing to do with whether or not the
                      >>loop continues at all. This use of the comma is mere obfuscation.[/color]
                      >
                      >
                      > That's your opinion of course. I like this style because the return value of
                      > scanf is often ignored anyway [...][/color]

                      What else do you ignore? NULL values from malloc() and
                      fopen()? Warning messages from compilers? STOP signs? Fire
                      alarms?

                      No wonder software sucks.

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • CBFalconer

                        #12
                        Re: What does this code mean???

                        Lawrence Kirby wrote:[color=blue]
                        >[/color]
                        .... snip ...[color=blue]
                        >
                        > Unfortunately true. Consider however there are very few situations
                        > where ignoring the return value of scanf() isn't a bug.[/color]

                        I rarely use scanf so am not sure, but are there any situations in
                        which it isn't a bug?

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson


                        Comment

                        • parser

                          #13
                          Re: What does this code mean???

                          see the code below
                          x=(scanf("%d",& i),j,k,l,...... ,z); /*x will hav the value of z*/

                          u should that the comma separated expressions are evaluated from Left
                          to Right and thats why the whole expression i.e. x, woud have a value
                          of the rightmost expression i.e. z

                          now consider this example

                          printf("here is a value : %d", ( 1,2,3,4,5,6,7,8 ,9));

                          output:
                          here is a vlaue : 9
                          so now i think that the it is clear to u

                          thanx :-)

                          Comment

                          • Chris Croughton

                            #14
                            Re: What does this code mean???

                            On Tue, 01 Feb 2005 14:29:07 +0000, Lawrence Kirby
                            <lknews@netacti ve.co.uk> wrote:
                            [color=blue]
                            > On Mon, 31 Jan 2005 15:50:41 +0000, CBFalconer wrote:
                            >[color=green]
                            >> Lawrence Kirby wrote:[color=darkred]
                            >>>[/color]
                            >> ... snip ...[color=darkred]
                            >>>
                            >>> Unfortunately true. Consider however there are very few situations
                            >>> where ignoring the return value of scanf() isn't a bug.[/color]
                            >>
                            >> I rarely use scanf so am not sure, but are there any situations in
                            >> which it isn't a bug?[/color]
                            >
                            > Only situations where you didn't care if the read operation failed or
                            > encountered end-of-file, and the scanf() just readss characters without
                            > writing values. scanf(fp, " ") to skip white-space perhaps in some odd
                            > circumstances. Not very likely but it is difficult to say that there
                            > is NO situation where this isn't a bug. Well actually easy to say, maybe I
                            > should and just wait for others to come up with the counterexamples . :-)[/color]

                            I'm not aware of any for scanf, but sscanf can certainly be legitimately
                            used without checking the return value, because it leaves its arguments
                            alone if it has no data so they default to the latest value. Silly
                            example:

                            #include <stdio.h>
                            int main(void)
                            {
                            char buff[256];
                            int x = 0;
                            int y = 0;
                            while (fgets(buff, 256, stdin))
                            {
                            sscanf(buff, "%d%d", &x, &y);
                            printf("%d + %d = %d\n", x, y, x+y);
                            }
                            return 0;
                            }

                            I suppose one could use something similar with scanf() and check feof()
                            after each call.

                            Chris C

                            Comment

                            • Servé La

                              #15
                              Re: What does this code mean???


                              "Eric Sosman" <eric.sosman@su n.com> schreef in bericht
                              news:ctlio9$fmt $1@news1brm.Cen tral.Sun.COM...[color=blue][color=green]
                              >>True, but I must say that this is an inadvisable way of using the comma
                              >>operator. The scanf() call has nothing to do with whether or not the
                              >>loop continues at all. This use of the comma is mere obfuscation.[/color]
                              >
                              >
                              > That's your opinion of course. I like this style because the return value[/color]
                              of[color=blue]
                              > scanf is often ignored anyway [...][/color]

                              What else do you ignore? NULL values from malloc() and
                              fopen()? Warning messages from compilers? STOP signs? Fire
                              alarms?


                              As as advised in this group, scanf is best to be avoided except in quick
                              test programs. I haven't seen a lot of error checking in that kind of
                              programs.


                              Comment

                              Working...