in Programming
1,331 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

0 votes
0 votes
Sizeof(String) = 6

Strlen(String) = 5

Since if condition fails answer = 0
0 votes
0 votes
The condition fails and print 0
0 votes
0 votes

"Hello" is saved as "Hello\0", "\0" being the NULL terminator.

strlen() is used to get the length of an array of chars / string. 

strlen() takes a pointer to a character, and walks the memory from this character on, looking for a NULL character. It counts the number of characters before it finds the NULL character.

=5

sizeof() is used to get the actual size of any type of data in bytes.

Hence it would include the NULL terminator as well.

=6.


if(sizeof(string) <= strlen(string))

if-condition fails, so print 0.

Answer:

Related questions