Thread: Difference between strlen() and sizeof()

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Question Difference between strlen() and sizeof()

    Hi Friends,

    Can anyone tell me what is the difference between strlen() and sizeof()

    Thanks in Advance!!!!

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    The difference is:
    strlen() is used to get the length of an array of chars / string. sizeof() is used to get the actual size of any type of data in bytes.

    Consider there's this array of char:
    Code:
    const char* A = "Hello World!\0";
    strlen will output 12 while sizeof 13. The NULL character at the end of the string won't be calculated in strlen.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's not really accurate and can lead to a misunderstanding, especially since sizeof(A) will not give you 13.

    sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.

    strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a NUL character. It counts the number of characters before it finds the NUL character. In other words, it gives you the length of a C-style NUL-terminated string.

    The two are entirely different in purpose and have nearly nothing to do with each other. In C++, you shouldn't need either very much; strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by g4j31a5 View Post
    The difference is:
    strlen() is used to get the length of an array of chars / string. sizeof() is used to get the actual size of any type of data in bytes.

    Consider there's this array of char:
    Code:
    const char* A = "Hello World!\0";
    strlen will output 12 while sizeof 13. The NULL character at the end of the string won't be calculated in strlen.
    No, sizeof this will give you 13:
    Code:
    const char A[] = "Hello World!";
    And sizeof this would give you 14 since it's double-null-terminated:
    Code:
    const char A[] = "Hello World!\0";
    But sizeof this will most likely give you 4 or 8 depending on the size of a pointer:
    Code:
    const char *A = "Hello World!\0";
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.
    Your comment here is inapplicable to string literals - the type (and therefore size) of a string literal is related to its value. However, the relationships between string literals, arrays of char, and pointers to char are technically an anomaly in C (and C++) - no other types are affected by this anomaly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by grumpy View Post
    Your comment here is inapplicable to string literals - the type (and therefore size) of a string literal is related to its value.
    The type derives from the value, which means that the size changes with the value. The sizeof() operator still operates strictly on the type, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by iMalc View Post
    No, sizeof this will give you 13:
    Code:
    const char A[] = "Hello World!";
    And sizeof this would give you 14 since it's double-null-terminated:
    Code:
    const char A[] = "Hello World!\0";
    But sizeof this will most likely give you 4 or 8 depending on the size of a pointer:
    Code:
    const char *A = "Hello World!\0";
    Umm... Yeah, you're right. LoL. What was in my head and what I had written is different.

    Thanks for making it clear.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Code:
    No, sizeof this will give you 13:
    Code:
    const char A[] = "Hello World!";
    What will be the output of strlen(A) ??

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What will be the output of strlen(A) ??
    Try it for yourself!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    The type derives from the value, which means that the size changes with the value. The sizeof() operator still operates strictly on the type, though.
    Agreed; we're on the same page now.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Hey,

    The ''sizeof" is a keyword of operator, while the strlen() is a function. The sizeof returns the length of a varible or data type, in other words, how many bytes this data block of type occupied. It can used on all dada types. The strlen() can only operate on a string and returns the number of characters before '\0' of a string.

    For example:

    char a[100] = "abc\0ddddd";

    The result of sizeof(a) will be 100 while the strlen(a) will be 4.

    Carle
    _____________________
    Free chat software for you
    Microsoft Certified Partner

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Smile

    Thanks A lot Carle...

    Now I am much more clear...

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by carle View Post
    Hey,
    For example:

    char a[100] = "abc\0ddddd";

    The result of sizeof(a) will be 100 while the strlen(a) will be 4.
    Three, actually.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Yaaaa Sebastiani...

    you are correct....

    Thanks...
    Last edited by subhashish1213; 09-21-2009 at 11:51 PM.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Smile

    haha,

    yes, you r right. I made a mistake~~~~~~~~~




    carle
    ________________
    Free chat software for you
    Microsoft Certified Partner

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof / strlen which one?
    By eponymous in forum C Programming
    Replies: 3
    Last Post: 03-15-2008, 01:17 PM
  2. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM
  5. A newby question on the sizeof operator
    By Evo in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 03:17 AM