Sequence Point before actual function call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • coolguyaroundyou@gmail.com

    Sequence Point before actual function call

    See the below code:

    void func(int x)
    {
    printf("%d",x);
    }

    int main()
    {
    int j=0;
    func(j++);
    return 0;
    }

    What should be the output?

    The C Standard says that there is a sequence point before the actual
    function call. So, according to me it should have been 1 but I got
    0(zero) on my screen.
  • Walter Banks

    #2
    Re: Sequence Point before actual function call



    coolguyaroundyo u@gmail.com wrote:
    See the below code:
    >
    void func(int x)
    {
    printf("%d",x);
    }
    >
    int main()
    {
    int j=0;
    func(j++);
    return 0;
    }
    >
    What should be the output?
    >
    The C Standard says that there is a sequence point before the actual
    function call. So, according to me it should have been 1 but I got
    0(zero) on my screen.
    The function is called before the increment.


    func(++j);

    The result should be 1. (Preincrement)


    Regards,


    --
    Walter Banks
    Byte Craft Limited






    Comment

    • Eric Sosman

      #3
      Re: Sequence Point before actual function call

      coolguyaroundyo u@gmail.com wrote:
      See the below code:
      >
      void func(int x)
      {
      printf("%d",x);
      }
      >
      int main()
      {
      int j=0;
      func(j++);
      return 0;
      }
      >
      What should be the output?
      "0" (if there is any, since the final line of output has no
      newline character).
      The C Standard says that there is a sequence point before the actual
      function call. So, according to me it should have been 1 but I got
      0(zero) on my screen.
      The expression `j++' increases the value of `j', and yields
      the value `j' had before it was incremented. As of the sequence
      point that precedes f(), `j' has the value 1 (the increment has
      taken place), and `x' has the value 0 (the value yielded by `j++').

      Keep in mind that f() has no connection with `j' at all, but
      only with the value of its argument expression as stored in `x'.

      --
      Eric.Sosman@sun .com

      Comment

      • viza

        #4
        Re: Sequence Point before actual function call

        On Mon, 27 Oct 2008 11:12:37 -0700, coolguyaroundyo u wrote:
        int j=0;
        func(j++);
        >
        The C Standard says that there is a sequence point before the actual
        function call. So, according to me it should have been 1 but I got
        0(zero) on my screen.
        That would be func(++j) (pre-increment). You used a post-increment.

        Comment

        • Nate Eldredge

          #5
          Re: Sequence Point before actual function call

          coolguyaroundyo u@gmail.com writes:
          See the below code:
          >
          void func(int x)
          {
          printf("%d",x);
          }
          >
          int main()
          {
          int j=0;
          func(j++);
          return 0;
          }
          >
          What should be the output?
          0.
          The C Standard says that there is a sequence point before the actual
          function call. So, according to me it should have been 1 but I got
          0(zero) on my screen.
          That would mean that the value of the variable j should be 1 before the
          function is called, but the value of the expression that you passed to
          func is still 0.

          The following two examples might be instructive:

          void foo(void) {
          int j = 0;
          int k;
          k = j++;
          func(k);
          }

          /* and: */

          int q;
          void func2(int x) {
          printf("x = %d, q = %d\n", x, q);

          void bar(void) {
          q = 0;
          func2(q++);
          }

          Comment

          • Richard Heathfield

            #6
            Re: Sequence Point before actual function call

            coolguyaroundyo u@gmail.com said:
            See the below code:
            >
            void func(int x)
            {
            printf("%d",x);
            }
            >
            int main()
            {
            int j=0;
            func(j++);
            return 0;
            }
            >
            What should be the output?
            >
            The C Standard says that there is a sequence point before the actual
            function call. So, according to me it should have been 1 but I got
            0(zero) on my screen.
            func()'s argument expression is j++. Since func() can't be called until the
            argument expression has been evaluated, j++ must be evaluated. Now, j++
            evaluates to 0, so 0 is the argument expression's value, so that's what
            func() will see. There is a side effect (the value of j changes), but that
            doesn't effect the result of the expression j++. It's still 0, because
            post-increment ++ yields as its value the value that the operand had
            before ++ got its hands on it.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Martin Ambuhl

              #7
              Re: Sequence Point before actual function call

              coolguyaroundyo u@gmail.com wrote:
              See the below code:
              >
              void func(int x)
              {
              printf("%d",x);
              }
              >
              int main()
              {
              int j=0;
              func(j++);
              return 0;
              }
              >
              What should be the output?
              >
              The C Standard says that there is a sequence point before the actual
              function call.
              So what?
              So, according to me it should have been 1 but I got
              0(zero) on my screen.
              Well, "according to [you]" seems to be a very non-authoritative source:

              #include <stdio.h /* mha: added. printf is a variadic
              function and needs a declaration */
              void func(int x)
              {
              printf("%d\n", x); /* mha: added '\n'. Without this you
              relation on implementation defined
              behavior */
              }

              int main()
              {
              int j = 0;
              printf("[Output]\n");
              func(j++);
              /* mha: added code so you can learn the difference between j++ and
              ++j */
              j = 0;
              printf("[Added output]\nThe value of j is %d\n", j);
              printf("j++ should have the value of j _before_ the increment,\n"
              " it is actually %d\n", j++);
              printf("The new value of j is %d\n\n", j);
              j = 0;
              printf("The value of j is %d\n", j);
              printf("++j should have the value of j _after_ the increment,\n"
              " it is actually %d\n", ++j);
              printf("The new value of j is %d\n\n", j);
              /* mha: end of added code */
              return 0;
              }

              [Output]
              0
              [Added output]
              The value of j is 0
              j++ should have the value of j _before_ the increment,
              it is actually 0
              The new value of j is 1

              The value of j is 0
              ++j should have the value of j _after_ the increment,
              it is actually 1
              The new value of j is 1


              Comment

              • Keith Thompson

                #8
                Re: Sequence Point before actual function call

                Walter Banks <walter@bytecra ft.comwrites:
                coolguyaroundyo u@gmail.com wrote:
                >
                >See the below code:
                >>
                >void func(int x)
                >{
                >printf("%d",x) ;
                >}
                >>
                >int main()
                >{
                >int j=0;
                >func(j++);
                >return 0;
                >}
                >>
                >What should be the output?
                >>
                >The C Standard says that there is a sequence point before the actual
                >function call. So, according to me it should have been 1 but I got
                >0(zero) on my screen.
                >
                The function is called before the increment.
                No, the increment occurs before the function is called, and there's a
                sequence point between those two actions. But the expression ``j++''
                yields the value that j has *before* the increment. If func examines
                the value of j (either because j has been moved to file scope or
                because it can see it via a pointer), it will see the value 1, but the
                value of the parameter x is 0.

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

                • Flash Gordon

                  #9
                  Re: Sequence Point before actual function call

                  Walter Banks wrote, On 27/10/08 18:26:
                  >
                  coolguyaroundyo u@gmail.com wrote:
                  >
                  >See the below code:
                  >>
                  >void func(int x)
                  >{
                  >printf("%d",x) ;
                  >}
                  >>
                  >int main()
                  >{
                  >int j=0;
                  >func(j++);
                  >return 0;
                  >}
                  >>
                  >What should be the output?
                  >>
                  >The C Standard says that there is a sequence point before the actual
                  >function call. So, according to me it should have been 1 but I got
                  >0(zero) on my screen.
                  >
                  The function is called before the increment.
                  No, it is called after the increment as all side-effects (and the
                  increment is a side effect) have to be completed before the sequence
                  point. It is just that the value passed to the function is the
                  pre-increment value.
                  func(++j);
                  >
                  The result should be 1. (Preincrement)
                  Yes.
                  --
                  Flash Gordon
                  If spamming me sent it to smap@spam.cause way.com
                  If emailing me use my reply-to address
                  See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                  Comment

                  Working...