¿Retorno de carro en C?

Output of Following program is : hai

I didn't get how the \r carriage return works in this program and in real can any one help me out ?

#include <stdio.h>
#include<conio.h>

void main()
{
    printf("\nab");
    printf("\bsi");
    printf("\rha");
    _getch();
}

preguntado el 09 de enero de 11 a las 09:01

That's not the output I get (At least once I fix the void main(), and the nonstandard conio.h stuff.) -

I'm using Visual C++ output is "hai" -

if you put \n at the end of ab like 'ab\n' will get what you desire? -

@Octopus: I don't think he deseos any particular output, he wants to understand the cause of the output he is getting. It is obviously just 'puzzle code' and of no practical use. -

3 Respuestas

From 5.2.2/2 (character display semantics) :

\b (Retroceso) Moves the active position to the previous position on the current line. If the active position is at the initial position of a line, the behavior of the display device is unspecified.

\n (nueva línea) Moves the active position to the initial position of the next line.

\r (retorno de carro) Moves the active position to the initial position of the current line.

Here, your code produces :

  • <new_line>ab
  • \b : back one character
  • escribir si : overrides the b es s (producing asi on the second line)
  • \r : back at the beginning of the current line
  • escribir ha : overrides the first two characters (producing hai on the second line)

In the end, the output is :

\nhai

Respondido el 09 de enero de 11 a las 12:01

@mr_eclair typo on the last line : fixed - hielo

Program prints ab, goes back one character and prints si overwriting the b resultante asi. Carriage return returns the caret to the first column of the current line. That means the ha will be printed over as y el resultado es hai

Respondido el 09 de enero de 11 a las 12:01

Paso a paso:

[newline]ab

ab

[backspace]si

asi

[carriage-return]ha

hai

Carriage return, does not cause a newline. Under some circumstances a single CR or LF may be translated to a CR-LF pair. This is console and/or stream dependent.

Respondido el 09 de enero de 11 a las 13:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.