C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 10)
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;
}
10
6
5
11
Answer: Option
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".

Discussion:
7 comments Page 1 of 1.

Dinesh Kumar said:   8 years ago
The '\0' inside character literals and string literals stands for the character with the code zero.so 0 is considerd in the Turbo c.It may answer vary in the GCC complier.

Ravi said:   1 decade ago
@Amul: No, its implementation like

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

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

Praveen said:   1 decade ago
Answer should be 13, as its a string, not collection of character constants, so '\0' counts as 2 characters.

Amul antony charles said:   2 decades ago
In strlen() it also counts '\0' as a character, so answer would be 6.

Saba said:   7 years ago
If we use 'gets' in the place of print? Please explain me.

Priyanka said:   8 years ago
The answer should be 6 as it will also count '\0'.

Bharani said:   9 years ago
In String() its don't count '\0'.

Post your comments here:

Your comments will be displayed after verification.