Home>

Good evening. This time I will ask you how to use escape sequences (/ and ESC ... etc.) (how to output to the console).

Problems i am experiencing

When I want to execute a line break when using an escape sequence in the printf function etc.
I learned to use \ n to break a line like printf ("test \ n");In addition, I learned that warnings and reversions can be performed by using \ a and \ r.
For this reason, I thought that the characters that corresponded to the escape sequence would appear after "\" or ESC [.
However, there is the following code in the text I am using (Shinmeikan series), and the escape sequence notation is spread over the three printf functions, so I do not know why such a notation can be done. did.

Applicable source code

This code is an escape sequence that specifies the color and background color of the console screen
This code changes the color using ESC [-;-;... m. (~ Is omitted)

void color (int fg, int bg) {
    int col [] = {~};
    printf ("ESC [0;");
    if (~) {
        printf ("1;");
    }
    printf ("% d;% dm", ~);
}

As you can see, ESC [-;-;... m escape sequences were split into multiple printf functions.
As in the code above, is an escape sequence notation written as a single escape sequence without the need to follow it? Or is there something that can or cannot be described in different parts of each escape sequence?

I would appreciate your professor. Thanking you in advance.

Tried

I only have visual studio and wanted to see this in action, but I couldn't run it because ESC and other escape sequences are not supported.

Escape characters (\ a and \ n) could be executed, but if they were not written in the arguments in the printf, they would not function as an escape sequence, and an escape sequence not present Undefined behavior as using? And the execution has gone wrong. (If only \ is specified at the end of the argument, an"constant is continued on the second line"will result in an error.)

I've also tried to find out if there are other codes like the above, and whether they can be written separately, but I couldn't find them.

  • Answer # 1

    For example, if"abc"is output to the console, followed by"def"without a line break,"abcdef"is output to the console.
    This means that the actual output is continuous even though it is separated in the source code.

    "ESC [0;"is an escape sequence for the console, so it only needs to be continuous when viewed from the console.

  • Answer # 2

    I'm curious,

      

    printf ("ESC [0;");

    Is this part of an escape sequence?
    If so, just the"ESC [0;"character is output to the console, is that what you expect?
    There seems to be few consoles that support escape sequences in recent times, but it is the same even if output to a terminal that supports it.
    Since ESC needs to be output as a control code, it should be"\ x1B [0;" ;."\ x1B"(which can be"\ 033") is an escape code.

  • Answer # 3

    The escape notation for character string notation and the escape sequence are different.
    It does n’t make sense to be together w