0

Why output of this code is:5 9 #include<stdio.h> int main() { char s[] = "Hello\0Hi"; printf("%d %d", strlen(s), sizeof(s)); }

c

4/1/2020 7:01:36 AM

Farzam

1 Answer

New Answer

+3

In char s[] = "Hello\0Hi"; s has an embeded '\0' char, the strlen(s) will stop counting as soon as it come across first null terminator hence it reports length as 5 sizeof function/operator does not evaluates it arguments it just counts number of bytes allocated/used, so it counts the full size of the array. Note \0 is counted as 1 char and there is a hidden '\0' at the end, so the size is 9 Also note that the correct format specifier for both functions should be "%ld" as both returns unsigned long int value.