C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    printf("%d\n", strlen("123456"));
    return 0;
}
6
12
7
2
Answer: Option
Explanation:

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

Discussion:
9 comments Page 1 of 1.

Milind Walekar said:   1 decade ago
Will it not count the NULL in the string ?

Sneha said:   1 decade ago
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:   1 decade ago
What about NULL?

Sikandar said:   10 years ago
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:   9 years ago
Will it not count the null in the string?

Devi achsah said:   8 years ago
What about the null character. Why 7 is not the ans?

Shubham Baiyas said:   7 years ago
Because in string length null character is not counted.

Arulmani said:   7 years ago
#include<stdio.h>
#include<string.h>

int main()
{
char s[20]={'1','2','3','4','5','\0'};
int len=strlen(s);
printf("%d",len);
}

Here, even the output will be 5. In the string, null is not counted!

J.Morty said:   6 years ago
According to me, It returns 7.

Post your comments here:

Your comments will be displayed after verification.