void main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
} please tell how the answer is coming;the compiler showed it as "hai"
but why the answer is hai please tell how to arrive at the answer
What is the use of \r in the c language? please tell the output for the code?
Look :
1) first u know '\n' is newline, so only "\nab" would print 'ab' in a newline. But in 2nd. statement "\bsi" , it prints "si" after previous "b". Check , just cancel the last line [i.e. printf("\rha")], then u'll get 'asi' as output.
2) but '\r' acts as carriage return (feeds at beginning), here 'ha' string after '\r' replaces first 2 letters from 'asi' %26amp; so it becomes 'hai' now.
So the output is "hai". Is it OK ?
If u find any problem, contact me through mail. U r always welcome, frnd.
Reply:slevy13 is right ,
\r is carrige return ,
carrige return moves the current cursor to the start of line, where as a line feed causes the cursor to jump exactly one line. new line is often composed of carriage return and line feed,
in ur example , first line prints
ab
then second printf erases b of ab and write asi, (due to \b that means backspace)
then the third line moves the cursor to the start of line at 'a' of 'asi' and starts writing 'ha' thus over writing 'as' as 'ha' without touching the 'i' which results in 'hai'
Reply:\r is a carriage return (hexadecimal value D, decimal 13 in ASCII). It generally returns the cursor to the beginning of the line without moving to the next line. So the 'i' was left there from the previous printf.
Reply:it simple
\b is used to shift one space backward
\r is used to first cordinate of the line
therefore
output
ab
asi
hai
simple!!!!!!!
Reply:Okay, lets look at each line and what it prints where.
The first printf is "\nab". The '\n' is the escape newline sequence. That character combination *ALWAYS* puts the cursor to the first position on the next line, it is an ANSI standard that requires the compiler to take into account the different OS's interpretations. Then beginning there it prints 'ab' and leaves the cursor positioned right after the 'b'.
The next printf starts with the '\b' is the equivalent of hitting the backspace key, it removes the letter and shifts the cursor position back one space. So, starting with 'ab', it does the erasure to 'a' and then writes the 'si', leaving 'asi' on screen.
The final printf statement starts by printing the '\r' escape sequence, which acts as a carriage return without going to the next line. It simply moves the cursor to the same position the 'a' is in at the beginning of the line. Then it prints the 'ha' over the positions of the 'as' from the last statement.
I hope that this has helped you to realize the steps the program is printing and what each escape character is doing. If you need to see this happen to understand, I'd recommend putting in a couple of for loop structures with a very large count ( you may even have to nest them! ) to slow execution down to a point where you have time to see the letters get replaced in each printf. This always makes it simpler to see what happens when you can see the cursor move and sit under the letter and such. Try writing a program with all the escape sequences in it and separating the steps so you can see each one execute.
Reply:Let's go step by step.
First line,
printf("\nab");
prints "New Line" character + "ab", so you get "ab" at the second line of the console.
Second printf() prints backspace + "si". So, it wipes 'b' and continues printing after 'a', thus, you get "asi".
Third printf() prints "Carriage Return" (places cursor at the start of the same line) and prints "ha" overwriting 'a' with 'h' and 's' with 'a'.
That's all: you got "hai".
Reply:carriage return. depends on the system, but generally causes the cursor to move to the beginning of the line (but no go to the next line), thus overwriting what was already on the line.
calling cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment