Simple C quiz -- Strings | SoloLearn: Learn to code for FREE!

+46

Simple C quiz -- Strings

Hello Sololearners, Now that we have introduced C to you, I think it's time to introduce a little installment called simple quiz. The rules are simple: 1st correct answer and explanation will win best answer. This 1st one stretches a little further out than the current lessons stop at, but should still be straight forward enough to follow. Here we go: In the attached code, which mechanism(s) will correctly print the length of the string as written? https://code.sololearn.com/cg2WkOO8OtO7/?ref=app

1/19/2018 2:58:03 AM

Ace

33 Answers

New Answer

+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!

+38

In short, strlen() will always return what you want. It's built to count all non-null characters in a c-string. sizeof(char arr[]) returns the same as strlen + 1 for the null termination. It's the occupied length of the string. sizeof(char *ptr) is just the size of a pointer in bytes. On Sololearn, the compiler rests within a 32-bit system so any pointer will be 4 bytes.

+20

strlen() is used to know length of string

+13

Length of the string in this case is the number of printable characters in the string. Good luck.

+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)

+11

@Immortal ohhhk! my bad!πŸ˜πŸ‘

+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.

+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.

+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

+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)

+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

+9

first one

+9

char arr[] = "Sololearn!"; //This string is automatically null-terminated char *str = "Sololearn!"; //so is this one. literals always have a null termination character added, hence why strlen works as expected in my code. Look at this naïve implementation of strlen: for (size_t len = 0; ; ++str, ++len) //c99 and above { if (!*str) return len; } When strings have no null-terminator, the behavior is undefined as stated by @nonzyro (an example of why error handling is important in C).

+8

ohhh 😘

+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.

+4

I actually learned quite a bit reading through these posts regarding your question!

+4

+3

first one

+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)

+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.