C Programming - Strings - Discussion

10. 

What will be the output of the program ?

#include<stdio.h>
#include<string.h>

int main()
{
    char str[] = "India\0\BIX\0";
    printf("%d\n", strlen(str));
    return 0;
}

[A]. 10
[B]. 6
[C]. 5
[D]. 11

Answer: Option C

Explanation:

The function strlen returns the number of characters int the given string.

Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string is a collection of characters terminated by '\0'.

The output of the program is "5".


Amul Antony Charles said: (Jun 25, 2010)  
In strlen() it also counts '\0' as a character, so answer would be 6.

Ravi said: (Sep 29, 2010)  
@Amul: No, its implementation like

...
int i=0;
while(*(s++))
i++; "
....

Where "i" is the length of string "s".

Praveen said: (Feb 5, 2015)  
Answer should be 13, as its a string, not collection of character constants, so '\0' counts as 2 characters.

Bharani said: (Jun 9, 2016)  
In String() its don't count '\0'.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.