C Programming - Strings - Discussion


What will be the output of the program ?


int main()
    printf("%d\n", strlen("123456"));
    return 0;

[A]. 6
[B]. 12
[C]. 7
[D]. 2

Answer: Option A


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

Therefore, strlen("123456") returns 6.

Hence the output of the program is "6".

Milind Walekar said: (Oct 8, 2013)  
Will it not count the NULL in the string ?

Sneha said: (Jul 10, 2014)  
I perceive that strlen() function is defined in c library in such a way that null is not counted unless it is mentioned specifically as '\0' at the end during the string declaration.

Sharanu said: (Feb 13, 2015)  
What about NULL?

Sikandar said: (Feb 12, 2016)  
It's not null. It's nul character '\0'-Ascii value is 0 (1 byte) it excluding in string length as well nul character including in string size.

NULL is related to pointer its 4 byte.

Ashutosh said: (Sep 26, 2016)  
Will it not count the null in the string?

Devi Achsah said: (Jul 25, 2017)  
What about the null character. Why 7 is not the ans?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.