0

I have the following example

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

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

First off , I don't understand how the printf function works. I have been trying to find tutorials that explain this, but they are too basic.

The way understand this is that there is a cursor which reads every character along the input. For example if we just have the line:

printf("\bsi")

by intuition I would think that it just outputs the string si since there is no previous characters before si, but the output is i. I also want to add that \b takes into account the input of another printf which is weird.

I also think that all those printfs are equivalent to just one printf. Something like:

printf("\nab\bsi\rha");

which makes me think that there is something going on behind the bars that I can't really explain or tell what it is. I think it's something related to some pointer on the input or printf stores the input in a buffer and reads it in a certain way.

Can someone explain how printf works in detail?

....

The question is a bit different because I know it's the same exercise, but I have a different opinion about it and approach it in a different way.

daniel
  • 557
  • 1
  • 4
  • 15
  • 1
    What do you see? The `ab` are output before the `\bsi` — they're on the same line, since you didn't output a newline after the first string. I think you should see `hai`. And yes, the three are equivalent to one. And intuition is a very bad indicator of what will happen in a C program until you've been programming in it a long time. – Jonathan Leffler Apr 04 '17 at 06:22
  • Maybe [this](https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences) might help ? – Himal Apr 04 '17 at 06:23
  • @Avi: What's `\a` then? – Jonathan Leffler Apr 04 '17 at 06:25
  • @JonathanLeffler... Yes I am learning how to program in C in detail. A lot of newcomers bypass a lot of what I am trying to understand. No one has answered any of my questions Ex: why does printf("\bsi") returns i instead of si if there is no prior characters to delete. – daniel Apr 04 '17 at 06:29
  • Ultimately, what happens depends on how the output is displayed — and the standard doesn't say, for good reason. However, on most terminals, the `\n` outputs a newline, moving the cursor to the start of a new line. The letters `a` and `b` are written; then the backspace `\b` moves the cursor back one so that the `s` overwrites the `b` and the `i` follows. The carriage return `\r` moves the cursor back to the start of the line; the letters `h` overwrites `a` and `a` overwrites `s`, leaving `hai` on display. And your "what happens if" question is unclear — it depends on the terminal, etc. – Jonathan Leffler Apr 04 '17 at 06:30
  • If it is any consolation, on a Unix box, `\bsi` produces `si` as you expected. You're on Windows; you'll have to look at the documented behaviour of the terminal driver on Windows to find out why it behaves as you say on Windows. Are you sure there isn't an `s` at the end of the prior line of output? – Jonathan Leffler Apr 04 '17 at 06:33
  • I am assuming that the only line written inside the main is printf("\bsi"), so ignore the other two. Why does it print i? – daniel Apr 04 '17 at 06:36
  • Can you also elaborate on the claim "\r moves the cursor back to the start of the line"?. Right after the second printf is executed we have asi\rha, so what does it mean to move the cursor to the start of the line? – daniel Apr 04 '17 at 06:42
  • Like I said, on a sane system (macOS Sierra 10.12.4 — approximately sane), the output of the solo `printf("\bsi");` is `si` as you expected. There is no way anyone not intimately familiar with the Windows terminal driver (which rules me out) can explain what happens on Windows. As to CR moves to the start of line — that's what CR is defined to do, and NL or LF (aka `\n`) moves to the start of the next line, usually. Terminal settings and so on can affect this; it rapidly gets very intricate, and doubly so since you're on Windows and I'm not. – Jonathan Leffler Apr 04 '17 at 06:43
  • On a Mac, there are three settings for the management of CR and NL during output: `onlcr` — map NL to CRNL on output; `-onocr` — do output CR at start of line; and `-onlret` — On the terminal, NL does not perform the CR function. There are three input controls, too; I'm not about to list them. This is system-specific — and Windows and Unix are very different. – Jonathan Leffler Apr 04 '17 at 06:50
  • The right output is hai as you pointed out; however I don't understand how asi\rha results in hai. – daniel Apr 04 '17 at 06:54
  • @JonathanLeffler ...I think that I know understand the carriage return; however, it's pretty different from what you explained. The way I see asi\rha is that ha will override the first two chracters of what was previously written, so a is replaced by h, s is replaced by a and then just append the last i. If we had astttttti\rha, the output is hatttttti as I pointed out. I don't know how that translates to "move the cursor to the start of the line"? – daniel Apr 04 '17 at 07:06
  • In that case, I can't help further. What you expect moves the cursor to the start of line, and then your explanation is OK. If you're determined not to accept that, so be it, but I have no other way of explaining what happens other than by telling you what happens. – Jonathan Leffler Apr 04 '17 at 07:11
  • Now I see it. Move the cursor to the start of the line and then just start overriding characters with all the characters after \r. – daniel Apr 04 '17 at 07:14
  • Yes. That's what I meant. – Jonathan Leffler Apr 04 '17 at 20:26

1 Answers1

0

Escape sequences

enter image description here

Escape sequences are used to represent certain special characters within string literals and character literals. The above escape sequences are available (extra escape sequences may be provided with implementation-defined semantics):

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Abi
  • 724
  • 6
  • 22