C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 8)
8.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[7] = "IndiaBIX";
printf("%s\n", str);
return 0;
}
Answer: Option
Explanation:
Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for '\0'.
Discussion:
12 comments Page 1 of 2.
Anusha.v said:
1 decade ago
Answer can be Indiabi since when we compile this progamme it is giving Indiabi as answer someone is saying it depends on the compiler. Based on that condition we can say that answer is not predictable.
Taj said:
1 decade ago
In the gcc compiler output observed "IndiaBi" with following warning:
Warning : Initializer-string for array of chars is too long [enabled by default].
char str[7] = "IndiaBIX";
Warning : Initializer-string for array of chars is too long [enabled by default].
char str[7] = "IndiaBIX";
Ajamathusen Jahagirdar said:
10 years ago
Answer for these question will be none of above because output of the program is IndiaBI and it is constant and no other option matches the output so answer will be none of above.
Naman_Shah said:
9 years ago
According to me,
Output is IndiaBi, because the array initialize up to 7 characters. So the Answer should be option 'D' because IndiaBi is not mentioned.
Thank you!!!
Output is IndiaBi, because the array initialize up to 7 characters. So the Answer should be option 'D' because IndiaBi is not mentioned.
Thank you!!!
Vivek Todi said:
1 decade ago
Gcc compilation shows that only the first seven characters are going to be printed. The correct answer should be option D. None of these.
Amrit said:
1 decade ago
Looks like that the answers depends on the compiler used, so option C would be the ans, since one can't predict the answer.
Smith said:
1 decade ago
If we assign more elements than array size then Turbo C gives complie time erro "Too many initialisers".
Kanak vashistha said:
1 decade ago
Why shouldn't be the answer of this question is an error can anyone explain me?
Amit kumar said:
3 years ago
This programs gives the output as IndiaBI.
So, the correct option is D.
So, the correct option is D.
Deepak Chauhan said:
10 years ago
Since the answer depends on compiler hence cannot predict option C.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers