C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 20)
20.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[] = "India\0BIX\0";
printf("%d\n", sizeof(str));
return 0;
}
Answer: Option
Explanation:
The following examples may help you understand this problem:
1. sizeof("") returns 1 (1*).
2. sizeof("India") returns 6 (5 + 1*).
3. sizeof("BIX") returns 4 (3 + 1*).
4. sizeof("India\0BIX") returns 10 (5 + 1 + 3 + 1*).
Here '\0' is considered as 1 char by sizeof() function.
5. sizeof("India\0BIX\0") returns 11 (5 + 1 + 3 + 1 + 1*).
Here '\0' is considered as 1 char by sizeof() function.
Discussion:
12 comments Page 2 of 2.
Usha.R.T said:
7 years ago
If that is the case for sizeoff(), then how it will detect the end of the string?
And why did it not considered last \0 character in a string?
If it considers \0 character as an end of string then it would be same with the one appeared after India.
And why did it not considered last \0 character in a string?
If it considers \0 character as an end of string then it would be same with the one appeared after India.
Keerthi said:
7 years ago
I am not understanding this, please can you explain me in detail?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers