Sizeof and increment operators

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

    Sizeof and increment operators

    #include <stdio.h>

    int main()
    {
    int number = 10;
    printf("Number is: %d\n", number);

    printf("Sizeof of number is: %d\n", sizeof(number++ ));

    printf("Number now is: %d\n", number);

    return 0;
    }


    The post-increment operator doesnt seem to work here. The value of the
    number remains the same even after the call to the sizeof operator.
    Can someone clarify this please?

    Thanks,
    Anjali.
  • Richard Bos

    #2
    Re: Sizeof and increment operators

    g_managoli@redi ffmail.com (Anjali M) wrote:
    [color=blue]
    > printf("Sizeof of number is: %d\n", sizeof(number++ ));[/color]
    [color=blue]
    > The post-increment operator doesnt seem to work here. The value of the
    > number remains the same even after the call to the sizeof operator.
    > Can someone clarify this please?[/color]

    From the last public draft of the C99 Standard, 6.5.3.4#2:

    # The sizeof operator... If the type of the operand is a variable length
    # array tye, the operand is evaluated; otherwise, the operand is not
    # evaluated and the result is an integer constant.

    Richard

    Comment

    • smitha

      #3
      Re: Sizeof and increment operators

      Hi,

      The sizeof operator just returns the size of the type or expressions,

      so i guess,it is not defined to allow airthmetic operations within it.

      Bye,

      Smitha

      "Anjali M" <g_managoli@red iffmail.com> wrote in message
      news:997f953e.0 407052026.38f5e 1f2@posting.goo gle.com...[color=blue]
      > #include <stdio.h>
      >
      > int main()
      > {
      > int number = 10;
      > printf("Number is: %d\n", number);
      >
      > printf("Sizeof of number is: %d\n", sizeof(number++ ));
      >
      > printf("Number now is: %d\n", number);
      >
      > return 0;
      > }
      >
      >
      > The post-increment operator doesnt seem to work here. The value of the
      > number remains the same even after the call to the sizeof operator.
      > Can someone clarify this please?
      >
      > Thanks,
      > Anjali.[/color]


      Comment

      • Sumukh

        #4
        Re: Sizeof and increment operators

        HI anjali,

        Sizeof is an *operator* in C.
        The operand expected in sizeof is either an identifier that is a
        unary-expression, or a type-cast expression (that is, a type specifier
        enclosed in parentheses). The unary-expression cannot represent a
        bit-field object, an incomplete type, or a function designator.

        A unary expression is an expression whose valuation is computed by
        applying its (unary) operator to the valuation of its operand. There are
        three kinds of unary operators:
        (1) the logical complement unary operator "!",
        (2) the plus unary operator "+", and
        (3) the minus unary operator "-".

        Hope this helps

        sumukh


        Anjali M wrote:
        [color=blue]
        > #include <stdio.h>
        >
        > int main()
        > {
        > int number = 10;
        > printf("Number is: %d\n", number);
        >
        > printf("Sizeof of number is: %d\n", sizeof(number++ ));
        >
        > printf("Number now is: %d\n", number);
        >
        > return 0;
        > }
        >
        >
        > The post-increment operator doesnt seem to work here. The value of the
        > number remains the same even after the call to the sizeof operator.
        > Can someone clarify this please?
        >
        > Thanks,
        > Anjali.[/color]

        Comment

        • Nitin Bhardwaj

          #5
          Re: Sizeof and increment operators

          g_managoli@redi ffmail.com (Anjali M) wrote in message news:<997f953e. 0407052026.38f5 e1f2@posting.go ogle.com>...[color=blue]
          > #include <stdio.h>
          >
          > int main()
          > {
          > int number = 10;
          > printf("Number is: %d\n", number);
          >
          > printf("Sizeof of number is: %d\n", sizeof(number++ ));
          >
          > printf("Number now is: %d\n", number);
          >
          > return 0;
          > }
          >
          >
          > The post-increment operator doesnt seem to work here. The value of the
          > number remains the same even after the call to the sizeof operator.
          > Can someone clarify this please?
          >
          > Thanks,
          > Anjali.[/color]

          Hi Anjali,

          The problem is not that of the ++ operator...but the 'sizeof'
          operator.
          The C Standard states that the operand of sizeof can be either a type
          name or an expression.

          e.g. sizeof (int) OR sizeof (a+b)

          If it is an expression,then it will not be evaluated; See section
          A.7.4.8 of K&R2 - [The C Programming Language 2/ed - Kernighan &
          Ritchie ], Appendix A.

          For the sake of convenience I'm reproducing the contents verbatim
          here :

          A.7.4.8 Sizeof Operator
          The sizeof operator yields the number of bytes required to store an
          object of the type of its operand. The operand is either an
          expression,***_ which is not evaluated_**, or a parenthesized type
          name. When sizeof is applied to a char, the result is 1; when applied
          to an array, the result is the total number of bytes in the array.
          When applied to a structure or union, the result is the number of
          bytes in the object, including any padding required to make the object
          tile an array: the size of an array of n elements is n times the size
          of one element. The operator may not be applied to an operand of
          function type, or of incomplete type, or to a bit-field. The result is
          an unsigned integral constant; the particular type is
          implementation-defined.

          Hence your expresion number++ never gets evaluated.So the value of
          number doesn't increase.

          HTH
          Nitin

          Comment

          • Nitin Bhardwaj

            #6
            Re: Sizeof and increment operators

            "smitha" <smithas@cisco. com> wrote in message news:<108909700 7.531247@sj-nntpcache-3>...[color=blue]
            > Hi,
            >
            > The sizeof operator just returns the size of the type or expressions,
            >
            > so i guess,it is not defined to allow airthmetic operations within it.[/color]

            Wrong, the operand of sizeof operator is either type-names OR
            expressions - which also includes arithmetic expressions.If it were
            not defined for arithmetic expressions then the compiler must have
            complained !!

            You can even include a function call in the argument to sizeof !!![color=blue]
            >
            > Bye,
            >
            > Smitha
            >
            > "Anjali M" <g_managoli@red iffmail.com> wrote in message
            > news:997f953e.0 407052026.38f5e 1f2@posting.goo gle.com...[color=green]
            > > #include <stdio.h>
            > >
            > > int main()
            > > {
            > > int number = 10;
            > > printf("Number is: %d\n", number);
            > >
            > > printf("Sizeof of number is: %d\n", sizeof(number++ ));
            > >
            > > printf("Number now is: %d\n", number);
            > >
            > > return 0;
            > > }
            > >
            > >
            > > The post-increment operator doesnt seem to work here. The value of the
            > > number remains the same even after the call to the sizeof operator.
            > > Can someone clarify this please?
            > >
            > > Thanks,
            > > Anjali.[/color][/color]

            Comment

            • smitha

              #7
              Re: Sizeof and increment operators

              yes... sizeof is an operator and not a function. In a function call each
              argument will be evaluated but this is not the case in sizeof.

              "Nitin Bhardwaj" <nitinbhardwaj8 0@hotmail.com> wrote in message
              news:17fa8780.0 407060427.4a1f3 a99@posting.goo gle.com...[color=blue]
              > "smitha" <smithas@cisco. com> wrote in message[/color]
              news:<108909700 7.531247@sj-nntpcache-3>...[color=blue][color=green]
              > > Hi,
              > >
              > > The sizeof operator just returns the size of the type or expressions,
              > >
              > > so i guess,it is not defined to allow airthmetic operations within it.[/color]
              >
              > Wrong, the operand of sizeof operator is either type-names OR
              > expressions - which also includes arithmetic expressions.If it were
              > not defined for arithmetic expressions then the compiler must have
              > complained !!
              >
              > You can even include a function call in the argument to sizeof !!![color=green]
              > >
              > > Bye,
              > >
              > > Smitha
              > >
              > > "Anjali M" <g_managoli@red iffmail.com> wrote in message
              > > news:997f953e.0 407052026.38f5e 1f2@posting.goo gle.com...[color=darkred]
              > > > #include <stdio.h>
              > > >
              > > > int main()
              > > > {
              > > > int number = 10;
              > > > printf("Number is: %d\n", number);
              > > >
              > > > printf("Sizeof of number is: %d\n", sizeof(number++ ));
              > > >
              > > > printf("Number now is: %d\n", number);
              > > >
              > > > return 0;
              > > > }
              > > >
              > > >
              > > > The post-increment operator doesnt seem to work here. The value of the
              > > > number remains the same even after the call to the sizeof operator.
              > > > Can someone clarify this please?
              > > >
              > > > Thanks,
              > > > Anjali.[/color][/color][/color]


              Comment

              • Dan Pop

                #8
                Re: Sizeof and increment operators

                In <1089097007.531 247@sj-nntpcache-3> "smitha" <smithas@cisco. com> writes:
                [color=blue]
                >The sizeof operator just returns the size of the type or expressions,[/color]

                Without evaluating them (except for VLAs in C99).
                [color=blue]
                >so i guess,it is not defined to allow airthmetic operations within it.[/color]

                It is perfectly defined and allowed, they just won't be performed: the
                compiler will merely figure out the type of the result.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Charles Richmond

                  #9
                  Re: Sizeof and increment operators

                  Anjali M wrote:[color=blue]
                  >
                  > #include <stdio.h>
                  >
                  > int main()
                  > {
                  > int number = 10;
                  > printf("Number is: %d\n", number);
                  >
                  > printf("Sizeof of number is: %d\n", sizeof(number++ ));
                  >
                  > printf("Number now is: %d\n", number);
                  >
                  > return 0;
                  > }
                  >
                  > The post-increment operator doesnt seem to work here. The value of the
                  > number remains the same even after the call to the sizeof operator.
                  > Can someone clarify this please?
                  >[/color]
                  What's the matter??? Can't you *do* the job after you
                  suck it up from the U. S.???

                  --
                  +-------------------------------
                  | Charles and Francis Richmond
                  | richmond at plano dot net
                  | Re-Defeat Bush!!!
                  +-------------------------------

                  Comment

                  • Richard Bos

                    #10
                    Re: Sizeof and increment operators

                    Charles Richmond <richmond@plano .net> wrote:
                    [color=blue]
                    > What's the matter??? Can't you *do* the job after you
                    > suck it up from the U. S.???[/color]

                    Got fired, didya?

                    Richard

                    Comment

                    • Joona I Palaste

                      #11
                      Re: Sizeof and increment operators

                      Charles Richmond <richmond@plano .net> scribbled the following:[color=blue]
                      > Anjali M wrote:[color=green]
                      >> #include <stdio.h>
                      >>
                      >> int main()
                      >> {
                      >> int number = 10;
                      >> printf("Number is: %d\n", number);
                      >>
                      >> printf("Sizeof of number is: %d\n", sizeof(number++ ));
                      >>
                      >> printf("Number now is: %d\n", number);
                      >>
                      >> return 0;
                      >> }
                      >>
                      >> The post-increment operator doesnt seem to work here. The value of the
                      >> number remains the same even after the call to the sizeof operator.
                      >> Can someone clarify this please?
                      >>[/color]
                      > What's the matter??? Can't you *do* the job after you
                      > suck it up from the U. S.???[/color]

                      You are incredibly rude. What makes you so sure the OP sucked a job from
                      the US? What makes you so sure the OP has a job as a programmer at all?
                      This might just as well be a question from a genuine C newbie. And
                      besides, even if he did suck up the job from the US, what business is
                      that of yours? This is a technical newsgroup. If you need to vent your
                      patriotism, go to a politics newsgroup.

                      --
                      /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                      \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                      "You could take his life and..."
                      - Mirja Tolsa

                      Comment

                      • Nitin Bhardwaj

                        #12
                        Re: Sizeof and increment operators

                        >Charles Richmond <richmond@plano .net> wrote in message >news:<40ECF393 .79AED7B5@plano .net>...
                        [color=blue][color=green]
                        > >[/color]
                        > What's the matter??? Can't you *do* the job after you
                        > suck it up from the U. S.???[/color]


                        It seems frm yr language that u hate Indians !!

                        Did u lost ur job to an Indian, buddy?

                        Cough up yr technical skills to match-up to ours & take on the
                        competition instead of making political transgressions on a technical
                        group like c.l.c!!

                        Comment

                        • Richard Bos

                          #13
                          Re: Sizeof and increment operators

                          nitinbhardwaj80 @hotmail.com (Nitin Bhardwaj) wrote:
                          [color=blue]
                          >Charles Richmond <richmond@plano .net> wrote in message >news:<40ECF393 .79AED7B5@plano .net>...
                          >[color=green]
                          > > suck it up from the U. S.???[/color]
                          >
                          > It seems frm yr language that u hate Indians !!
                          >
                          > Did u lost ur job to an Indian, buddy?
                          >
                          > Cough up yr technical skills to match-up to ours & take on the
                          > competition instead of making political transgressions on a technical
                          > group like c.l.c!![/color]

                          Ow! I know the English did some pretty awful things in India, but do you
                          _have_ to take revenge by murdering their language? Leave that to the
                          USAnians, like Mr. Richmond ;->

                          Richard

                          Comment

                          • Dan Pop

                            #14
                            Re: Sizeof and increment operators

                            In <40ECF393.79AED 7B5@plano.net> Charles Richmond <richmond@plano .net> writes:
                            [color=blue]
                            >Anjali M wrote:[color=green]
                            >>
                            >> #include <stdio.h>
                            >>
                            >> int main()
                            >> {
                            >> int number = 10;
                            >> printf("Number is: %d\n", number);
                            >>
                            >> printf("Sizeof of number is: %d\n", sizeof(number++ ));
                            >>
                            >> printf("Number now is: %d\n", number);
                            >>
                            >> return 0;
                            >> }
                            >>
                            >> The post-increment operator doesnt seem to work here. The value of the
                            >> number remains the same even after the call to the sizeof operator.
                            >> Can someone clarify this please?
                            >>[/color]
                            >What's the matter??? Can't you *do* the job after you
                            >suck it up from the U. S.???[/color]

                            How many job assignments like this have you seen? This is not only
                            a newbie question, it is a perfectly legitimate newbie question.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • Anjali M

                              #15
                              Re: Sizeof and increment operators

                              Charles Richmond <richmond@plano .net> wrote in message news:<40ECF393. 79AED7B5@plano. net>...[color=blue]
                              > Anjali M wrote:[color=green]
                              > >
                              > > #include <stdio.h>
                              > >
                              > > int main()
                              > > {
                              > > int number = 10;
                              > > printf("Number is: %d\n", number);
                              > >
                              > > printf("Sizeof of number is: %d\n", sizeof(number++ ));
                              > >
                              > > printf("Number now is: %d\n", number);
                              > >
                              > > return 0;
                              > > }
                              > >
                              > > The post-increment operator doesnt seem to work here. The value of the
                              > > number remains the same even after the call to the sizeof operator.
                              > > Can someone clarify this please?
                              > >[/color]
                              > What's the matter??? Can't you *do* the job after you
                              > suck it up from the U. S.???[/color]

                              I don't know what you have got against me. I asked a very valid
                              question on this newsgroup and I _dont_ have to take this crap from
                              you. If you can be rude, so can _I_. So, better be careful before you
                              say something like this again.

                              Comment

                              Working...