2

The first printf statement is giving output 3 and second giving 20. Can anybody please explain what's the difference between the two here?

char frame[20],str[20];
printf("\nstrlen(frame)= %d",strlen(frame));
printf("\nsizeof(frame) = %d",sizeof(frame));

Thanks :)

user3542109
  • 121
  • 1
  • 3
  • 11
  • 3
    One is length of the string the other size of the pointer. This is basic C knowledge :) – Stolas Apr 25 '14 at 12:36
  • 6
    @Stolas none is the size of the pointer, though one is the size of the array. – nos Apr 25 '14 at 12:39
  • `strlen()` evals *content* at **run-time** for its computation. `sizeof` is deduced at **compile-time** and is content-agnostic. *Both* are covered in any remotely-decent text on the C language. – WhozCraig Apr 25 '14 at 12:42
  • @Stolas , in addition to nos comment, sizeof pointer is 4 on my system. – user3542109 Apr 25 '14 at 12:45
  • @WhozCraig could you please tell me why sizeof is returning 3 even if it's compiled time. – user3542109 Apr 25 '14 at 12:46
  • I stand corrected. I forgot the array over the malloc. – Stolas Apr 25 '14 at 12:48
  • @user3542109 You are getting `strlen(frame) --> 3` because of the _garbage_ in `frame[20]` when code runs as `frame` is not initialized. Another day or on another system you may get something different - it is undefined behavior. – chux - Reinstate Monica Apr 25 '14 at 12:50
  • 2
    @user3542109 Sure, your program is executing undefined behavior. You're using an incorrect format-specifier. `sizeof()` evaluates to type `size_t`, yet your invoking `printf()` telling it the value is a system `int`. use `%zu` for your second `printf()`. *Both* lines invoke UB (the former invokes `strlen()` on a buffer with indeterminate content, the latter for the reasons I just said). – WhozCraig Apr 25 '14 at 12:51
  • @WhozCraig thanks for pointing me over it. I usually don't do it. but will do from now :) – user3542109 Apr 25 '14 at 12:53

3 Answers3

6

sizeof is a compile-time operator and determines the size in bytes that a type consumes. In the case of frame (char[20]) that is 20 bytes.

strlen is a run-time function and scans a given pointer until the first occurrence of a nul terminator '\0' returning the amount of characters until then.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • 3
    @user3542109 Because frame[] is an uninitialized array, and it probably contains bogus characters up to frame[3], which is '\0'. If you would declare it as "char frame[20] = {0}", strlen() would return 0 – Claudiu Apr 25 '14 at 12:48
5
  1. Because the contents of frame is not initialized, which means it is not a valid C string, so strlen(frame) could return any value, or crash. Actually, its behavior is undefined in this case.

  2. Because frame is an array of 20 characters, therefore sizeof(frame) will return 20 * sizeof(char), which will always be 20 (sizeof(char) always equals 1).

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
3

strlen actually gives you the length of the string, whereas sizeof gives you the size of the allocated memory in bytes. It is infact quite nicely explained here http://www.cplusplus.com/reference/cstring/strlen/ Extract given below.

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

This should not be confused with the size of the array that holds the string. For example:

char mystr[100]="test string";

defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.

And yes as per the other comments, you are trying to get length for uninitialized strings and that leads to undefined behaviour, it can be 3 or anything else, depending on whatever garbage is present in the memory that got allocated for your string.

Community
  • 1
  • 1
Saba
  • 412
  • 5
  • 9