Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 29
1st one will return the length of string, i.e., strlen, which is an inbuilt function of string.h which returns the length of string passed as a parameter. In 2nd, there's "sizeof()" functions which returns the total size occupied by any variable or in this case, strings! In 3rd, there's also "sizeof()" function but there's a pointer being passed on as parameter which basically doesn't make any difference because the arrays or strings, are always being passed as reference!
19th Jan 2018, 3:29 AM
Harjeet Singh
Harjeet Singh - avatar
+ 20
strlen() is used to know length of string
19th Jan 2018, 7:04 AM
tejeswar
tejeswar - avatar
+ 12
I think the first one (using strlen()). sizeof1, returns the occupied size in bytes. sizeof2, the size of the memory address (4 bytes on 32 bit machine)
19th Jan 2018, 3:48 AM
Boris Batinkov
Boris Batinkov - avatar
+ 11
@Immortal ohhhk! my bad!😐👍
19th Jan 2018, 3:55 AM
Harjeet Singh
Harjeet Singh - avatar
+ 11
First one returns the number of characters in a string excluding null terminator. Second one returns the total length of a string including null terminator. The third one returns the length of content present in pointer. We knew that pointer holds address of another variable then, the number of digits in address number is returned. Example :if address is 4532, 4 is returned.
19th Jan 2018, 10:39 AM
Nashat
Nashat - avatar
+ 10
strlen() function takes a string as an argument and returns the number of characters in it. i.e. The length of characters. The sizeof() is an operator that returns the size of the datatype in bytes in a specific computer.
21st Jan 2018, 3:44 PM
Erick Mwenda Njagi
Erick Mwenda Njagi - avatar
+ 9
the first one will give you the string length the second one will give you the actual string length will null ending . the third one will give you the size of pointer i.e4 bytes
19th Jan 2018, 3:53 PM
Shubham Tewari
Shubham  Tewari - avatar
+ 9
strlen(arr) gives the number of characters present which in this case are 10(Sololearn!) sizeof(arr) gives the occupied size ...which in the case of the current array is 11 (Sololearn!\0)...implicitly placed terminator And sizeof(ptr) gives the size of the pointer which holds the address of the array (I have no idea how that is 4 btw)
19th Jan 2018, 4:44 PM
Kavya Sree Kante
Kavya Sree Kante - avatar
+ 9
Note that sizeof isn't an actual function call, it's a compiler builtin that translates the evaluation of the argument into an integer constant. This means you cannot get the sizeof anything that is heap (dynamically) allocated; you'll get the pointer type size instead of the allocation size. http://en.cppreference.com/w/cpp/language/sizeof
20th Jan 2018, 12:39 PM
Simon
Simon - avatar
+ 9
first one
22nd Jan 2018, 12:33 PM
Brix P. Abreu
Brix P. Abreu - avatar
+ 8
ohhh 😘
20th Jan 2018, 8:35 AM
Bharat😃
Bharat😃 - avatar
+ 5
The first printf () statement prints out the length of the character array which is 10 because of the strlen function. Next printf() prints the sizeof(arr) , the number of bytes in the character array called arr , the total bytes in memory including the end of string marker '\0' , which is 11 bytes, because sizeof computes the size of the variable or datatype. Lastly sizeof(ptr) returns the number of bytes of the size of the pointer ptr occupies in memory , which is 4 bytes.
23rd Jan 2018, 1:34 AM
Rick Zalman
+ 4
I actually learned quite a bit reading through these posts regarding your question!
22nd Jan 2018, 6:02 AM
Haseo
Haseo - avatar
+ 4
1st one
22nd Jan 2018, 9:54 AM
Dixit Moradiya
Dixit Moradiya - avatar
+ 3
first one
22nd Jan 2018, 9:22 AM
Jerome
Jerome - avatar
+ 3
strlen: return the length of a buffer until the first NUL byte is read (undefined result if the buffer does not contain a NUL byte; see man 3 strlen) sizeof1: absolute size of a stack/static allocated array(!) including NUL bytes (used for binary buffers e.g. shellcode, encrypted data; absolute size equals sizeof(datatype)*array_length) sizeof2: virtual address length (equals sizeof(long) on x86/x64 systems)
29th Jan 2018, 3:24 AM
Linus Liberty
Linus Liberty - avatar
+ 3
The function strlen (char *) returns the length of the string(character array) up to but not including the terminating null ('\0'), that is it does not count the '\0' but uses terminating null to know where the string ends. For example char hi []="hello world" ; total bytes of character array is string length + '\0' which is 12 bytes. But strlen (hi) is 11 characters only.
30th Jan 2018, 12:17 AM
Rick Zalman
+ 2
in short strlen()wll always return what you want it`s builts to count all non_null characters in a c-string
29th Jan 2018, 2:32 AM
Puchi Wolff
Puchi Wolff - avatar
+ 1
first one will print that
22nd Jan 2018, 7:46 PM
kanika gupta
kanika gupta - avatar
+ 1
first one because it is built=in function of string which will get accurate length of string without adding any null character at end. sizeof passed with direct array will add null character in last. sizeof function again passed with ptr will print memory occupied by ptr.
23rd Jan 2018, 5:22 AM
Sami Solangi
Sami Solangi - avatar