I have written a small C++ program to understand the use of \b
. The program is given below -
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << "\b";
return 0;
}
So, this program gives the desired output Hello World
.This should not happen because backspace only moves cursor one space back and not delete it from the buffer.So,why !
is not printed?
Now,Consider another program-
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << "\b";
cout << "\nAnother Line\n";
return 0;
}
So, here the output is -
Hello World!
Another Line
Why does the backspace does not work here? Newline should not flush the buffer,so !
should be deleted.What is the issue here?
Also,when i add either endl
or \n
after \b
,in both the cases,the output is Hello World!
.But,newline character does not flush the buffer whereas endl
flushes the buffer.So, how the output is same in both the cases?