178 views

What is the output of this program?

#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "Hello";
printf("%lu %lu", sizeof(string), strlen(string));
return 0;
}
1. $5 \ 6$
2. $5 \ 5$
3. $6 \ 5$
4. $6 \ 6$

C.  6 5

• sizeof() counts the number of characters in the string, including the null character. (It actually tells you how much memory an object takes. Since each charachter in C++ takes one byte, a string takes as much memory as the number of characters in it, including the null character.)

https://en.cppreference.com/w/cpp/language/sizeof

• strlen() computes the number of characters in the string, ignoring the null character.

https://en.cppreference.com/w/cpp/string/byte/strlen

sizeof("Hello") = 6.

strlen("Hello") = 5.

https://ideone.com/oi9ANE

i am confused with strlen() here mentioned strlen compute the number of character in the string ignoring the null character .but i saw a question on go( programming in c:gate 2004-23) where  null character is also included to count the lenth of string.
strlen() will return length as 6 and p[0]=s[6-0] will print nothing.

1
201 views