Link to home
Start Free TrialLog in
Avatar of prasad2315
prasad2315

asked on

sizeof and strlen

why does the sizeof and srlen doesnot give the same answer
char str[]="prasad";

strlen(str) gives 6
sizeof(str) gives 7
which one is correct

i think both are correct as strlen gives the number of characters and
sizeof gives the number of bytes including the null character
Avatar of ozo
ozo
Flag of United States of America image

because str[6] == '\0'
both are correct
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ozo & Kdo are right.
but I have a question from Kdo  and ozo:
as youknow,
char *str = "test";
means that str points to a piece of memory that contains "test" string. well,
char *str;
str = "test";
this works too, but how? i defined a char pointer and then set its value to "test" string. in the second code, where is "test" string in the memory? in stack?
thanks
In translation phase 7, a byte or code of value zero is appended to each multibyte
character sequence that results from a string literal or literals.) The multibyte character
sequence is then used to initialize an array of static storage duration and length just
sufficient to contain the sequence. For character string literals, the array elements have
type char, and are initialized with the individual bytes of the multibyte character
sequence;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CSecurity,

>> where is "test" string in the memory?

"test" is a string literal, and would typically be in some read-only part of the process image. This is platform dependent though.
@I08

How was your holiday? :-)
It was good, thank you :) Two weeks in Scotland - just me and my backpack heh. It's an amazing country, great sceneries, and very nice people.
Comment {ID:22461703},  while true, for the example the author posed, the response is specific to the author's example without answering the question in general. Recommend accepting  also an additional answer such as Kdo's  22462431,  which does actually explain why  strlen and sizeof give different answers.

The caliber of a person who would need the answer to  a question like this is obviously a beginner,  and more likely will not  know what a NUL byte is, recognize significance of a NUL byte in a string,  or even necessarily recognize that \0 is a NUL byte.

That is:  understanding why sizeof()  returns a larger value in the example, is equivalent to understanding that strings have NUL termination  (not explained explained in  comment {ID:22461703}).

Understanding the cases  where sizeof() returns a smaller integer than strlen() requires  knowing the differences between compile-time reserved storage and run-time  interpretation.

Most C beginners also are not aware of the escape sequences such  as \0.
Which is NUL,  or  aware that  string literals  contain the implicit NUL.