20

Possible Duplicate:
Different answers from strlen and sizeof for Pointer & Array based init of String

Can anybody help me in understanding difference between sizeof method and strlen method in c programming?

Community
  • 1
  • 1
Prabhu
  • 723
  • 2
  • 10
  • 29

2 Answers2

36

strlen() is used to get the length of a string stored in an array.

sizeof() is used to get the actual size of any type of data in bytes.

Besides, 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 null character. It counts the number of characters before it finds the null character. In other words, it gives you the length of a C-style null-terminated string.

The two are quite different. In C++, you do not 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).

Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • 1
    +1, but even using `std::copy` and `std::fill` you have to provide the number of elements of the array if you are using C-style arrays. The point is not much `memcpy`/`memset` vs STL algorithms, but C-arrays/strings vs `std::vector`/`std::array`/`std::string`/other containers. – Matteo Italia Dec 21 '11 at 13:16
  • 4
    "sizeof is a compile-time expression" - except when used on a VLA in C99. – Steve Jessop Dec 21 '11 at 13:21
  • 2
    Just a nit, but... there's a `NULL` pointer and a "null pointer", but it's a nul character (or what I prefer: a `'\0'` character). – James Kanze Dec 21 '11 at 13:32
  • @James: saying "NULL character" or "null character" for `0` is about as valid as saying "BELL character" or "bell character" for `7`, or "line feed", "(horizontal) tab" etc. I also prefer "nul" just to help distinguish type and context, and using the ASCII-endorsed names of ASCII characters provides a kind of consistency. But let's not pretend that's anything other than a three-letter contraction of "null" :-) – Steve Jessop Dec 21 '11 at 15:19
  • @SteveJessop It is something more: it's the official name of the character in the original ASCII standard (which seems to have a decided preference for three letter names). In a C++ context, I prefer `'\0'`, because that's the way you write it in code. You could also write just `0`, but then it's not clear that you're dealing with a character. The C++ standard does call it a "null character type value charT()". But in C++, `NULL` is a macro, defined by the standard, so I'd avoid that (unless you mean the macro). – James Kanze Dec 21 '11 at 15:29
  • sizeof is an operator, so you don't need parenthesis around the argument, unless it's a type name. `sizeof x` and `sizeof(x)` both work, but you must write `sizeof (int)`. – Klas Lindbäck Dec 21 '11 at 15:56
  • we must remember that the sizeof() is to get the actual size of any type of data in bytes(Any type of data) – BertKing Mar 25 '18 at 13:40
13

sizeof is not a method. It is a compile-time construct that determines the amount of memory a particular type or a variable occupies. strlen, on the other hand, is a function that counts the number of consecutive non-zero char values starting at the specified location in memory (which happens to be the same as determining the length of a zero-terminated C string).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523