##### what is the output and please provide the explaination,the a

what is the output and please provide the explaination,the answer given is hai

main(){

printf("\nab");

printf("\nbsi");

printf(\rha");

}

Ranita Biswas
10 Jan 2015 04:07 pm
#include<stdio.h>
int main()
{
printf("\nab");
printf("\nbsi");
printf("\rha");
return 0;
}

Output of this code will be


ab
ha

The reason is the escape sequence '\r' which moves the cursor to the beginning of the current line, so "bsi" got deleted by "ha".

Arul
10 Jan 2015 08:20 pm

I think, \r (carriage return) will move the cursor to the beginning, without deleting the current line.

so, the output will be,

ab
hai

Ranita Biswas
11 Jan 2015 10:52 am

Arul, have you ran the code and got that output?
As you are passing the string "\rha" to the printf function, and any literal string contains a '\0' null character at the end of it; so '\r' moves the cursor to the beginning of the line, and 'h', 'a', and '\0' are written. So, whatever be there in the buffer previously, only "ha" will be written in that line.

Arul
11 Jan 2015 07:21 pm

ranitha, though I agree with the reason that you are giving, I have checked it with Microsoft VC++ 9.0, gcc version 4.5.3 (GCC). I am getting the output that I have written above:)

ab
hai