doubt regarding sizeof operator

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

    doubt regarding sizeof operator















    Hi All,

    I am not able to understand the behavior of the bellow mentioned
    program.

    #include<stdio. h>
    int main(void)
    {
    printf("\n Size = %d \n",sizeof 1<0);
    return 0;
    }
    The output of the program is


    Size = 0

    But my understanding is sizeof returns the size of the type .In this
    case should it not returns sizeof (int) ?

    But if I rewrite the code as mentioned bellow the output is 4

    #include<stdio. h>
    int main(void)
    {
    printf("\n Size = %d \n",sizeof (1<0));
    return 0;
    }


    Size = 4

    Could you please help me to understand the problem?

    Regards
    Somenath

  • Ian Collins

    #2
    Re: doubt regarding sizeof operator

    somenath wrote:
    >
    Hi All,
    >
    I am not able to understand the behavior of the bellow mentioned
    program.
    >
    #include<stdio. h>
    int main(void)
    {
    printf("\n Size = %d \n",sizeof 1<0);
    return 0;
    }
    The output of the program is
    >
    Size = 0
    >
    But my understanding is sizeof returns the size of the type .In this
    case should it not returns sizeof (int) ?
    >
    The expression "sizeof 1<0" is false, hence the zero. sizeof binds
    tighter than '<', so you have written (sizeof 1) < 0.

    --
    Ian Collins.

    Comment

    • qiooeer

      #3
      Re: doubt regarding sizeof operator

      On 10 30 , 4 44 , somenath <somenath...@gm ail.comwrote:
      Hi All,
      >
      I am not able to understand the behavior of the bellow mentioned
      program.
      >
      #include<stdio. h>
      int main(void)
      {
      printf("\n Size = %d \n",sizeof 1<0);
      return 0;}
      >
      The output of the program is
      >
      Size = 0
      >
      But my understanding is sizeof returns the size of the type .In this
      case should it not returns sizeof (int) ?
      >
      But if I rewrite the code as mentioned bellow the output is 4
      >
      #include<stdio. h>
      int main(void)
      {
      printf("\n Size = %d \n",sizeof (1<0));
      return 0;
      >
      }
      >
      Size = 4
      >
      Could you please help me to understand the problem?
      >
      Regards
      Somenath
      printf("\n Size = %d \n",sizeof (1<0));//sizeof(bool)
      printf("\n Size = %d \n",sizeof 1<0);// equal to ((sizeof (int))<0)
      of course zero --false--


      Comment

      • Keith Thompson

        #4
        Re: doubt regarding sizeof operator

        somenath <somenathpal@gm ail.comwrites:
        I am not able to understand the behavior of the bellow mentioned
        program.
        >
        #include<stdio. h>
        int main(void)
        {
        printf("\n Size = %d \n",sizeof 1<0);
        return 0;
        }
        The output of the program is
        >
        Size = 0
        Which is exactly what it should be, as Ian Collins explained.

        [...]
        But if I rewrite the code as mentioned bellow the output is 4
        >
        #include<stdio. h>
        int main(void)
        {
        printf("\n Size = %d \n",sizeof (1<0));
        return 0;
        }
        >
        Size = 4
        (1<0) yields a value of type int (with the value 0), so sizeof (1<0)
        yields sizeof(int). Apparently that's 4 on your system. But you
        print the value using a "%d" format, which is correct for type int,
        but not for type size_t.

        For a small value like this, a reasonable way to print it is to
        convert it to int:

        printf("\n Size = %d \n",(int)size of (1<0));

        --
        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."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • vipvipvipvipvip.ru@gmail.com

          #5
          Re: doubt regarding sizeof operator

          On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgw rote:
          (1<0) yields a value of type int (with the value 0), so sizeof (1<0)
          yields sizeof(int). Apparently that's 4 on your system. But you
          print the value using a "%d" format, which is correct for type int,
          but not for type size_t.
          >
          For a small value like this, a reasonable way to print it is to
          convert it to int:
          >
          printf("\n Size = %d \n",(int)size of (1<0));
          The only way is this

          #if __STDC_VERSION_ _ >= 199901L
          printf("%zu\n", sizeof (int));
          #else
          printf("%u\n", (unsigned)sizeo f (int));
          #endif

          Comment

          • Charlie Gordon

            #6
            Re: doubt regarding sizeof operator

            "somenath" <somenathpal@gm ail.coma écrit dans le message de news:
            1193733892.4916 43.315010@y27g2 00...legr oups.com...
            >
            I am not able to understand the behavior of the bellow mentioned
            program.
            >
            #include<stdio. h>
            int main(void)
            {
            printf("\n Size = %d \n",sizeof 1<0);
            return 0;
            }
            This looks like a typo in a program that prints the size of floating point
            numbers:


            Comment

            • Charlie Gordon

              #7
              Re: doubt regarding sizeof operator

              "Charlie Gordon" <news@chqrlie.o rga écrit dans le message de news:
              47272d17$0$5451 $426a74cc@news. free.fr...
              "somenath" <somenathpal@gm ail.coma écrit dans le message de news:
              1193733892.4916 43.315010@y27g2 00...legr oups.com...
              >>
              >I am not able to understand the behavior of the bellow mentioned
              >program.
              >>
              >#include<stdio .h>
              >int main(void)
              >{
              > printf("\n Size = %d \n",sizeof 1<0);
              > return 0;
              >}
              >
              This looks like a typo in a program that prints the size of floating point
              numbers: (OOPS, my fingers slipped)
              #include<stdio. h>
              int main(void)
              {
              printf("\n Size = %d \n",sizeof 1.0);
              return 0;
              }

              Note that sizeof 1.0, which is the same as sizeof(double), should be cast to
              (int) for printf's %d format specifier. The format specifier for size_t is
              %zu, but it is c99 specific.

              --
              Chqrlie.


              Comment

              • Ben Pfaff

                #8
                Re: doubt regarding sizeof operator

                vipvipvipvipvip .ru@gmail.com writes:
                The only way is this
                >
                #if __STDC_VERSION_ _ >= 199901L
                printf("%zu\n", sizeof (int));
                #else
                printf("%u\n", (unsigned)sizeo f (int));
                #endif
                I would recommend "%lu" and unsigned long for the second case.
                --
                char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
                ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
                =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
                2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

                Comment

                • Keith Thompson

                  #9
                  Re: doubt regarding sizeof operator

                  vipvipvipvipvip .ru@gmail.com writes:
                  On Oct 30, 11:12 am, Keith Thompson <ks...@mib.orgw rote:
                  >(1<0) yields a value of type int (with the value 0), so sizeof (1<0)
                  >yields sizeof(int). Apparently that's 4 on your system. But you
                  >print the value using a "%d" format, which is correct for type int,
                  >but not for type size_t.
                  >>
                  >For a small value like this, a reasonable way to print it is to
                  >convert it to int:
                  >>
                  > printf("\n Size = %d \n",(int)size of (1<0));
                  >
                  The only way is this
                  >
                  #if __STDC_VERSION_ _ >= 199901L
                  printf("%zu\n", sizeof (int));
                  #else
                  printf("%u\n", (unsigned)sizeo f (int));
                  #endif
                  Why is that the only way?

                  It's safe to assume that sizeof(int) does not exceed INT_MAX (even
                  though it's not absolutely guaranteed by the standard), so for this
                  particular case, converting to int and using "%d" is not unreasonable.

                  If you want to be a bit safer, convert to unsigned long and use "%lu".

                  "%zu" is the correct format for C99 -- but in practice, there's a
                  significant risk that the compiler will claim to conform to C99, but
                  the runtime library won't support "%zu". "%lu" will work properly for
                  all sizes up to 2**32-1, and perhaps more.

                  --
                  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."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • CBFalconer

                    #10
                    Re: doubt regarding sizeof operator

                    somenath wrote:
                    >
                    #include<stdio. h>
                    int main(void) {
                    printf("\n Size = %d \n",sizeof 1<0);
                    return 0;
                    }
                    The output of the program is
                    Size = 0
                    Here "sizeof 1" returns size of an int, which is not < 0, so the
                    comparison function returns 0, which is then printed. Fully
                    parenthized the operation is "((sizeof 1) < 0)"
                    >
                    But my understanding is sizeof returns the size of the type .In
                    this case should it not returns sizeof (int) ?
                    >
                    But if I rewrite the code as mentioned bellow the output is 4
                    >
                    #include<stdio. h>
                    int main(void) {
                    printf("\n Size = %d \n",sizeof (1<0));
                    return 0;
                    }
                    >
                    Size = 4
                    Here "1 < 0" returns an int (which happens to be 0, and doesn't
                    matter). So sizeof (1 < 0) returns the sizeof an int, which
                    happens to be 4 on your system. Fully parenthized the operation is
                    "(sizeof (1 < 0))".

                    --
                    Chuck F (cbfalconer at maineline dot net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net>



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Army1987

                      #11
                      Re: doubt regarding sizeof operator

                      On Tue, 30 Oct 2007 09:03:33 +0000, qiooeer wrote:
                      printf("\n Size = %d \n",sizeof (1<0));//sizeof(bool)
                      (1<0) has type int.
                      %d is for signed int, not size_t.
                      --
                      Army1987 (Replace "NOSPAM" with "email")
                      A hamburger is better than nothing.
                      Nothing is better than eternal happiness.
                      Therefore, a hamburger is better than eternal happiness.

                      Comment

                      • vipvipvipvipvip.ru@gmail.com

                        #12
                        Re: doubt regarding sizeof operator

                        On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.o rgwrote:
                        That's true, but it will not handle huge variables under Win64 where %zu
                        might not be supported either. Should we use %llu ?
                        You cannot have an object whose size SIZE_MAX.
                        %zu will work everywhere, anywhere as long as it is supported.
                        Else you cast to unsigned.

                        Comment

                        • Charlie Gordon

                          #13
                          Re: doubt regarding sizeof operator

                          <vipvipvipvipvi p.ru@gmail.coma écrit dans le message de news:
                          1193781055.4504 57.290450@o80g2 00...legr oups.com...
                          On Oct 30, 11:15 pm, "Charlie Gordon" <n...@chqrlie.o rgwrote:
                          >"Keith Thompson" <kst-u@mib.orga écrit dans le message de news:
                          ln8x5k1j9e.fsf@ nuthaus.mib.org...
                          >That's true, but it will not handle huge variables under Win64 where %zu
                          >might not be supported either. Should we use %llu ?
                          You cannot have an object whose size SIZE_MAX.
                          %zu will work everywhere, anywhere as long as it is supported.
                          Else you cast to unsigned.
                          Why did you snip the relevant part by Keith Thomson below:
                          >>"%zu" is the correct format for C99 -- but in practice, there's a
                          >>significant risk that the compiler will claim to conform to C99, but
                          >>the runtime library won't support "%zu". "%lu" will work properly for
                          >>all sizes up to 2**32-1, and perhaps more.
                          On the win64 architecture, size_t is 64 bits, while both unsigned and
                          unsigned long are 32 bits.
                          One can have objects larger than 4 GB on thosw systems for which %lu and
                          (unsigned long)sizeof(obj ect) will fail to print the correct size. VC++ is
                          not c99 conforming, as %zu might not be supported. So the only way to print
                          a size on win64 is to cast to (unsigned long long) and use the %llu format.

                          --
                          Chqrlie.


                          Comment

                          • Ben Pfaff

                            #14
                            Re: doubt regarding sizeof operator

                            "Charlie Gordon" <news@chqrlie.o rgwrites:
                            One can have objects larger than 4 GB on thosw systems for which %lu and
                            (unsigned long)sizeof(obj ect) will fail to print the correct size. VC++ is
                            not c99 conforming, as %zu might not be supported. So the only way to print
                            a size on win64 is to cast to (unsigned long long) and use the %llu format.
                            If VC++ is not C99 conforming, then it may not have unsigned long
                            long type at all, and %llu may also not be supported.
                            --
                            "Some people *are* arrogant, and others read the FAQ."
                            --Chris Dollin

                            Comment

                            • Richard Bos

                              #15
                              Re: doubt regarding sizeof operator

                              Keith Thompson <kst-u@mib.orgwrote:
                              "%zu" is the correct format for C99 -- but in practice, there's a
                              significant risk that the compiler will claim to conform to C99, but
                              the runtime library won't support "%zu".
                              Possibly, but in that case, you have a broken implementation. %zu should
                              be among the least of your concerns. I'd advice upgrading to a correct,
                              well-built C99 implementation immediately. If that's not possible,
                              reverting to a correct, well-built C89 implementation would be
                              preferable over struggling with a chimaera.

                              Richard

                              Comment

                              Working...