When printing a char array directly I get garbage data printed out with it.
#include <iostream>
using namespace std;
int main(){
char x [5] {'H','E','L','L','O'};
cout << "Output : ";
cout << x << endl;
return 0; }
Output : HELLO@ ~
But, this doesn't happen when I flush the output buffer before printing the char array.
#include <iostream>
using namespace std;
int main(){
char x [5] {'H','E','L','L','O'};
cout << "Output : " << flush;
cout << x << endl;
return 0; }
Output : HELLO
However, I don't see this behaviour if I don't print anything before flushing output buffer.
#include <iostream>
using namespace std;
int main(){
char x [5] {'H','E','L','L','O'};
cout << flush;
cout << x << endl;
return 0; }
Output : HELLO}~
The char array in all of the above examples doesn't have the '\0'
escape sequence character in the end so when printing the array, stuff inside the memory after the array also gets printed. Why flushing the output buffer showing a different behaviour?
Also, since the 3rd example is printing garbage data at the end inspite of flusing the output buffer. How is the 3rd example different from 2nd?