C Programming - Strings - Discussion


What will be the output of the program ?


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


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;
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.