in Programming
1,269 views
7 votes
7 votes

What will be the output of the following code?

#include <stdio.h> 
#include <string.h>
int main() 
{ 
	char string[] = "Hello"; 
	if(sizeof(string) <= strlen(string))
		printf("1");
	printf("0");
}

______

in Programming
by
1.3k views

7 Answers

21 votes
21 votes
Best answer
char string[] = "Hello"; //'\0' is added by compiler at end of string literals
sizeof(string) = (5+1) = 6  //+1 to store EndOfString char '\0'.
strlen(string) = 5

if(sizeof(string) <= strlen(string)) returns False so it wont print 1 and finally it will print 0.
selected by
1 vote
1 vote

Answer will be Zero as strlen() function always gives the actual length of string excluding '\0', but '\0' is always present at the end of string taking space in memory so it is counted in sizeof operator.

0 votes
0 votes

0 because 

sizeof(string) = strlen(string)

1 comment

No. sizeof includes null char at the end but strlen ignores it.
2
2
0 votes
0 votes
it will print 0 as a o/p
Answer:

Related questions