Escape Sequence \F - Form Feed - What Exactly Is It

Escape sequence \f - form feed - what exactly is it?

It skips to the start of the next page. (Applies mostly to terminals where the output device is a printer rather than a VDU.)

What is the use of '\f' character.where is to use it? [duplicate]

There are a bunch of so-called escaped characters - characters that have some special meaning and are formed from a backslash and another character. The most common one is newline \n, but there are others.

Many of them became less and less useful over time, such as \f. This is called form feed, is used to indicate to a printer that it should start a new page. You may notice multiple escape characters used to control printers, since back in the day printers were ascii controlled.

In C, is \f same as \v in terminals now?

How control characters render on a terminal depends on the terminal emulator, not the programming language you use. So you should search for documentation about your particular system.

For example, a Linux console responds to \v and \f in the same way:

LF (0x0A, ^J), VT (0x0B, ^K) and FF (0x0C, ^L) all give a linefeed, and if LF/NL (new-line mode) is set also a carriage return;

(quoted from man 4 console_codes)

Note that despite the above statement, the character 0x0A (\n) will echo as a CR-LF sequence because of the underlying terminal default onlcr setting, which causes newlines to be automatically translated to a CR-NL sequence. See man stty.

What is the use of formfeed and backspace escape strings in Java?

I usually use \r together with System.out.print when printing some progress percentage.

Try running this in your terminal:

class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; i++) {
System.out.print("Progress: " + i + " %\r");
Thread.sleep(100);
}
}
}

Why C++ standard reserves the formfeed character?

The formfeed character is not only for typewriters, it's universally recognized by all printers (or printer drivers) to stop the current page and advance to the next.

What is the meaning \r carriage return and \f form feed in Java?

You rarely use either of them (in isolation) in modern applications.

\r (carriage return) is what it sounds like if you're familiar with old-fashioned manual typewriters: It moves the "carriage" (the roll the paper is fed through) back to the beginning of the line. On a terminal (or similar), it moves the output point back to the beginning of the line, without moving down a line (usually).

\f is (as you said) a formfeed, it tells old-fashioned printers to start a new page. In computer documents, it's sometimes used to indicate a page break.

Rounding things out, there's also line feed (aka "newline"): \n. This means "move down a line." In some terminals, it just moves down a line without doing a carriage return; on others, it does both.

\n (LF, newline) is the standard text file line break on *nix and derived operating systems. \r\n (CRLF) is the standard text file line break on DOS and Windows. Back in the day, \r (on its own) was the standard line break in text files on the Mac, prior to Mac OS X (which is *nix-derived).

What do \b, \f, \n, \r do? What are the differences?

You can test these escape sequences using printf, for example in a C program or using a Unix/Linux shell such as Bash as shown below.

  • Backspace causes the cursor to move backwards across the previous character
  • Form feed is usually only used with printers to cause the current page to be fed out so the next page is the current one
  • New line (line feed) causes the cursor to move to the next line. On Unix-related systems this also causes the cursor to move to the beginning of that line.
  • Carriage return causes the cursor to move to the beginning of the current line. On systems using Windows (or MS-DOS) or its relatives (and especially its consoles), a combination of carriage return and line feed is used for new lines.
  • Tab causes the cursor to move to the next tab stop position which is often set at intervals of four or eight character positions apart. It is useful for lining up data in columns. Note, however, that string formatting such as that used by printf is much more reliable because use of tabs can still result in staggered columns. See an example at the bottom of this answer.
  • Space moves the cursor one position forward (the opposite of backspace). Note that on terminals (on the screen) space is most often destructive - that is, if a character was in the position that cursor moves across, that character is removed. The same thing happens when a new character other than space is printed in a position where another character existed (as seen in the backspace example below).

The $ represents the shell prompt and should not be typed. It is included in order to differentiate between what you type and what is output. I've included a newline \n at the end of each string so the output occupies its own line.

$ printf 'help\blo - backspace\n'
hello - backspace
$ printf 'two\nlines - newline\n'
two
lines - newline
$ printf 'clocks - carriage return\rsla\n'
slacks - carriage return
$ printf 'Fruit\tColor\tQuantity\ngrapes\tgreen\t100\nbananas\tyellow\t50\n- tabs\n'
Fruit Color Quantity
grapes green 100
bananas yellow 50
- tabs

Please see ASCII Control Characters for more information.

Try this to see how printf formatting can be an improvement over tabs:

$ array=(A B C 'apple pie' 'banana split' 'cherry turnover' 100 200 300)
$ for ((i=0; i<12; i+=3)); do printf '%s\t%s\t%s\n' "${array[@]:i:3}"; done
A B C
apple pie banana split cherry turnover
100 200 300
$ for ((i=0; i<12; i+=3)); do printf '%-16s%-16s%s\n' "${array[@]:i:3}"; done
A B C
apple pie banana split cherry turnover
100 200 300

As you can see, the columns aren't staggered in the second version and overall the output is the same width.

What does ^L mean in C?

Literally, it's a page break ("form feed") character. The compiler treats it as ordinary whitespace. But it's very useful for printing source code - it starts a new page (for example, use ^L between functions to force each call to get its own page).

In Vim/vi based editors, you can insert such a character within edit mode by typing Ctrl + V followed by Ctrl + L. It will look like ^L in the editor, but it's actually just one character (ASCII value: 12 or 0x0C).

In other words, ^L does this:


Code which actually uses the \a, \f, and \v escape sequences?

You are conflating the escape sequence, which is used by the compiler, with the character, which is used the program (and perhaps passed on to other programs).

Your question, though relevant, is a bit late. Java has already dropped '\v'. Funny, I never noticed. But, not C# or JavaScript. Never noticed that, either.



Related Topics



Leave a reply



Submit