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;
}
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.
J.Morty said:
6 years ago
According to me, It returns 7.
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!
#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!
Shubham Baiyas said:
7 years ago
Because in string length null character is not counted.
Devi achsah said:
8 years ago
What about the null character. Why 7 is not the ans?
Ashutosh said:
9 years ago
Will it not count the null in the string?
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.
NULL is related to pointer its 4 byte.
Sharanu said:
1 decade ago
What about NULL?
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.
Milind Walekar said:
1 decade ago
Will it not count the NULL in the string ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers