what is difference between sizeof and strlen

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

    what is difference between sizeof and strlen

    hello,
    what is difference between sizeof("abcd") and strlen("abcd")? why
    both functions gives different output when applied to same string
    "abcd".
    I tried following example for that.
    #include <stdio.h>
    #include <string.h>
    void main()
    {
    char *str1="abcd";
    char str2[]="abcd";
    printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
    printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
    }

  • Suman

    #2
    Re: what is difference between sizeof and strlen


    rahul8143@gmail .com wrote:[color=blue]
    > hello,
    > what is difference between sizeof("abcd") and strlen("abcd")? why[/color]

    1. Sizeof is an unary operator. Strlen is a function defined in
    string.h.

    2. Strlen looks for the first null('\0') and when found returns the
    number
    of characters that precede the null character.
    [color=blue]
    > both functions gives different output when applied to same string[/color]

    See above.[color=blue]
    > "abcd".
    > I tried following example for that.
    > #include <stdio.h>
    > #include <string.h>
    > void main()[/color]
    should be
    int main()[color=blue]
    > {
    > char *str1="abcd";
    > char str2[]="abcd";[/color]
    Among other things, strlen() returns a size_t object, and you should
    use
    the `z' length modifier, followed by a conversion specifier d,i,o,u, x
    or X.

    Sizeof again yields the size of an object and using the specifier for
    size_t
    (and a cast, I'm not sure if required, before printing) should again be
    preferred.[color=blue]
    > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
    > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
    > }[/color]

    Do something like
    char str[12];
    strcpy(str, "hello");
    printf("%zu vs %zu\n", DIM(str), strlen(str));
    and you'll know.

    Comment

    • Martin Ambuhl

      #3
      Re: what is difference between sizeof and strlen

      rahul8143@gmail .com wrote:[color=blue]
      > hello,
      > what is difference between sizeof("abcd") and strlen("abcd")? why
      > both functions gives different output when applied to same string
      > "abcd".
      > I tried following example for that.
      > #include <stdio.h>
      > #include <string.h>
      > void main()[/color]
      ^^^^
      Why do so many people post without
      a) following the newsgroup
      b) checking the FAQ
      or even
      c) learning the first thing about C from even the most basic textbook?

      It is impossible to take seriously a question from someone who could
      screw up something so basic.

      Comment

      • Antonio Contreras

        #4
        Re: what is difference between sizeof and strlen

        Martin Ambuhl wrote:[color=blue]
        > rahul8143@gmail .com wrote:[color=green]
        > > hello,
        > > what is difference between sizeof("abcd") and strlen("abcd")? why
        > > both functions gives different output when applied to same string
        > > "abcd".
        > > I tried following example for that.
        > > #include <stdio.h>
        > > #include <string.h>
        > > void main()[/color]
        > ^^^^
        > Why do so many people post without
        > a) following the newsgroup
        > b) checking the FAQ
        > or even
        > c) learning the first thing about C from even the most basic textbook?
        >
        > It is impossible to take seriously a question from someone who could
        > screw up something so basic.[/color]

        Indeed it is possible. Most compilers won't issue a warning if you set
        the return type to void. They will silently insert code to return a 0
        and just go on.

        Comment

        • junky_fellow@yahoo.co.in

          #5
          Re: what is difference between sizeof and strlen


          rahul8143@gmail .com wrote:[color=blue]
          > hello,
          > what is difference between sizeof("abcd") and strlen("abcd")? why
          > both functions gives different output when applied to same string
          > "abcd".
          > I tried following example for that.
          > #include <stdio.h>
          > #include <string.h>
          > void main()
          > {
          > char *str1="abcd";
          > char str2[]="abcd";
          > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
          > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
          > }[/color]

          sizeof is an operator and strlen is a C library function.
          sizeof gives the size in bytes of its operand. size is determined
          by the type of operand.
          "abcd" is a string literal and its size is 5.
          So, sizeof("abcd") = 5.
          strlen("abcd") = 4. It does not include the terminating null character.

          sizeof(str1) --> The operand str1 is a pointer to char. So, its type
          is char pointer and size will be the size of char
          pointer.
          sizeof(str2) --> str2 is an array of characters. Standard C says
          that "sizeof operator When applied to an operand
          that has array type, the result is the total number
          of bytes in the array. So, sizeof(str2) = 5.

          sizeof("abcd") = 5.

          strlen(str1) = 4.
          strlen(str2) = 4.
          strlen("abcd") = 4.

          Comment

          • pete

            #6
            Re: what is difference between sizeof and strlen

            rahul8143@gmail .com wrote:[color=blue]
            >
            > hello,
            > what is difference between sizeof("abcd") and strlen("abcd")?[/color]

            (sizeof("abcd") - strlen("abcd") == 1)

            --
            pete

            Comment

            • RAJU

              #7
              Re: what is difference between sizeof and strlen

              Hi Rahul,

              First of all, sizeof() and strlen() are differnt. The sizeof() is an
              operator which gives the number of bytes required to store an object of
              the type of it's operand and the strlen() is a function which takes a
              constant string as an input and returns length of it's argument string.

              If a constant string "abcd" is passed then strlen() will always return
              4.
              [color=blue][color=green][color=darkred]
              >>>printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;[/color][/color][/color]
              This will print the storage required for str1, which is a pointer to
              char and pointer size is 4. This is irrespective of the length of the
              string it's assigned to.
              str2 and "abcd" required '\0' at the end of their string. So, they
              require 4 + 1 = 5 bytes of storage and print 5. In this case,
              sizeof(constant string) = strlen(constant string) + 1;

              Regards,
              Raju

              rahul8143@gmail .com wrote:[color=blue]
              > hello,
              > what is difference between sizeof("abcd") and strlen("abcd")? why
              > both functions gives different output when applied to same string
              > "abcd".
              > I tried following example for that.
              > #include <stdio.h>
              > #include <string.h>
              > void main()
              > {
              > char *str1="abcd";
              > char str2[]="abcd";
              > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
              > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
              > }[/color]

              Comment

              • junky_fellow@yahoo.co.in

                #8
                Re: what is difference between sizeof and strlen


                RAJU wrote:[color=blue]
                > Hi Rahul,
                >
                > First of all, sizeof() and strlen() are differnt. The sizeof() is an
                > operator which gives the number of bytes required to store an object of
                > the type of it's operand and the strlen() is a function which takes a
                > constant string as an input and returns length of it's argument string.
                >
                > If a constant string "abcd" is passed then strlen() will always return
                > 4.
                >[color=green][color=darkred]
                > >>>printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;[/color][/color]
                > This will print the storage required for str1, which is a pointer to
                > char and pointer size is 4. This is irrespective of the length of the
                > string it's assigned to.[/color]

                Wrong. Pointer variable size is not always 4. Its implementation
                specific.

                Comment

                • Suman

                  #9
                  Re: what is difference between sizeof and strlen


                  Suman wrote:[color=blue]
                  > rahul8143@gmail .com wrote:[color=green]
                  > > hello,
                  > > what is difference between sizeof("abcd") and strlen("abcd")? why[/color]
                  >
                  > 1. Sizeof is an unary operator. Strlen is a function defined in
                  > string.h.
                  >
                  > 2. Strlen looks for the first null('\0') and when found returns the
                  > number
                  > of characters that precede the null character.
                  >[color=green]
                  > > both functions gives different output when applied to same string[/color]
                  >
                  > See above.[color=green]
                  > > "abcd".
                  > > I tried following example for that.
                  > > #include <stdio.h>
                  > > #include <string.h>
                  > > void main()[/color]
                  > should be
                  > int main()[color=green]
                  > > {
                  > > char *str1="abcd";
                  > > char str2[]="abcd";[/color]
                  > Among other things, strlen() returns a size_t object, and you should
                  > use
                  > the `z' length modifier, followed by a conversion specifier d,i,o,u, x
                  > or X.
                  >
                  > Sizeof again yields the size of an object and using the specifier for
                  > size_t
                  > (and a cast, I'm not sure if required, before printing) should again be
                  > preferred.[color=green]
                  > > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
                  > > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
                  > > }[/color]
                  >
                  > Do something like[/color]
                  #define DIM(x) sizeof x / sizeof x[0][color=blue]
                  > char str[12];
                  > strcpy(str, "hello");
                  > printf("%zu vs %zu\n", DIM(str), strlen(str));
                  > and you'll know.[/color]

                  Comment

                  • RAJU

                    #10
                    Re: what is difference between sizeof and strlen

                    You are right. Pointer variable is not always 4. But, on most of the
                    current 32-bit systems it's 4 bytes.It can be 8 bytes also. Every thing
                    is depends on Memory Management.

                    I just wanted to make a point that it's of fixed size for an
                    environment and it doesn't depend on the input string,

                    Thanks,
                    Raju

                    Comment

                    • Lawrence Kirby

                      #11
                      Re: what is difference between sizeof and strlen

                      On Fri, 05 Aug 2005 02:25:57 -0700, Antonio Contreras wrote:

                      ....
                      [color=blue][color=green][color=darkred]
                      >> > I tried following example for that.
                      >> > #include <stdio.h>
                      >> > #include <string.h>
                      >> > void main()[/color]
                      >> ^^^^[/color][/color]

                      ....
                      [color=blue]
                      > Indeed it is possible. Most compilers won't issue a warning if you set
                      > the return type to void. They will silently insert code to return a 0
                      > and just go on.[/color]

                      Or more likely they won't, the'll just generate code for a function that
                      returns void. Depending on how the implementation works that may or may
                      not work where a function that returns int is required. Sometimes it works
                      and you'll get a garbage value back, but it may not work at all, there's
                      no guarantee that a main returning void will even be called successfully.

                      Lawrence

                      Comment

                      • Kenny McCormack

                        #12
                        Re: what is difference between sizeof and strlen

                        In article <1123228624.756 300.56730@f14g2 000cwb.googlegr oups.com>,
                        <rahul8143@gmai l.com> wrote:[color=blue]
                        >hello,
                        > what is difference between sizeof("abcd") and strlen("abcd")? why
                        >both functions gives different output when applied to same string
                        >"abcd".
                        >I tried following example for that.
                        >#include <stdio.h>
                        >#include <string.h>
                        >void main()
                        > {
                        > char *str1="abcd";
                        > char str2[]="abcd";
                        > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
                        > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
                        > }
                        >[/color]

                        You'd think the trolls would at least be a little creative.

                        Personally, I prefer to declare it as "double *main()".
                        Works just as well.

                        Comment

                        • rahul8143@gmail.com

                          #13
                          Re: what is difference between sizeof and strlen


                          Kenny McCormack wrote:[color=blue]
                          > In article <1123228624.756 300.56730@f14g2 000cwb.googlegr oups.com>,
                          > <rahul8143@gmai l.com> wrote:[color=green]
                          > >hello,
                          > > what is difference between sizeof("abcd") and strlen("abcd")? why
                          > >both functions gives different output when applied to same string
                          > >"abcd".
                          > >I tried following example for that.
                          > >#include <stdio.h>
                          > >#include <string.h>
                          > >void main()
                          > > {
                          > > char *str1="abcd";
                          > > char str2[]="abcd";
                          > > printf("%d %d %d",sizeof(str1 ),sizeof(str2), sizeof("abcd")) ;
                          > > printf("%d %d %d",strlen(str1 ),strlen(str2), strlen("abcd")) ;
                          > > }
                          > >[/color]
                          >
                          > You'd think the trolls would at least be a little creative.
                          >
                          > Personally, I prefer to declare it as "double *main()".
                          > Works just as well.[/color]

                          THANKS everybody i understood difference between sizeof operator and
                          strlen function.

                          Comment

                          • Keith Thompson

                            #14
                            Re: what is difference between sizeof and strlen

                            "RAJU" <kanaka@cadence .com> writes:[color=blue]
                            > You are right. Pointer variable is not always 4. But, on most of the
                            > current 32-bit systems it's 4 bytes.It can be 8 bytes also. Every thing
                            > is depends on Memory Management.
                            >
                            > I just wanted to make a point that it's of fixed size for an
                            > environment and it doesn't depend on the input string,[/color]

                            Correct, but "pointer size is 4" was perhaps not the best way to make
                            that point.

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

                            • Emmanuel Delahaye

                              #15
                              Re: what is difference between sizeof and strlen

                              Suman wrote on 05/08/05 :[color=blue]
                              > 1. Sizeof is an unary operator. Strlen is a function /defined/ in
                              > string.h.[/color]

                              erm... declared...

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

                              "It's specified. But anyone who writes code like that should be
                              transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC


                              Comment

                              Working...