0

I'm new to C and I came across this code and it was confusing me:

sprintf(banner1, "\e[37╔═╗\e[37┌─┐\e[37┌┐┌\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37m\r\n");
sprintf(banner2, "\e[37╠═╝\e[37├─┤\e[37│││\e[37│ ┬\e[37├─┤\e[37├┤\e[37 ├─┤\e[37m\r\n");
sprintf(banner3, "\e[37╩  \e[37┴ ┴┘\e[37└┘\e[37└─┘\e[37┴ ┴\e[37└─┘\e[37┴ ┴\e[37m\r\n");

I was just confused as I don't know what do \e[37 and \r\n mean. And can I change the colors?

EvgenKo423
  • 2,256
  • 2
  • 16
  • 23
A_J_D_H
  • 27
  • 3
  • 1
    Perhaps your terminal doesn't support ANSI escape sequences. One of the languages you've chosen seems relevant, you might consider removing the other two, putting together a more complete [mcve], and describing the environment where you're running this. – Retired Ninja Jul 03 '21 at 23:33
  • 4
    The `\e[37m`, if printing on a terminal that supports ANSI, will cause the text to be printed in white (which is the default anyway). Note that `\e` is a special sequence in C that gives the escape character (ASCII 27) that is necessary to start an ANSI Escape Sequence. It does seem you might have a transcription error, however, because each of the `\e[37`'s should be followed by a`m`. To see how ANSI sequences work, do a google search for "ANSI Escape Sequences". – SGeorgiades Jul 03 '21 at 23:50
  • The `\r\n` is carriage-return and line feed, to move to the start of the next line. Generally, in C, just printing `\n` is sufficient, because a `\r` is automatically added, but there's no harm in including it. – SGeorgiades Jul 03 '21 at 23:51

1 Answers1

1

This looks like an attempt to use ANSI terminal color escapes and Unicode box drawing characters to write the word "PANGAEA" in a large, stylized, colorful manner. I'm guessing it's part of a retro-style BBS or MUD system, intended to be interacted with over telnet or ssh. It doesn't work, because whoever wrote it made a bunch of mistakes. Here's a corrected, self-contained program:

#include <stdio.h>

int main(void)
{
    printf("\e[31m╔═╗\e[32m┌─┐ \e[33m┌┐┌\e[34m┌─┐\e[35m┌─┐\e[36m┌─┐\e[37m┌─┐\e[0m\n");
    printf("\e[31m╠═╝\e[32m├─┤ \e[33m│││\e[34m│ ┬\e[35m├─┤\e[36m├┤ \e[37m├─┤\e[0m\n");
    printf("\e[31m╩  \e[32m┴ ┴┘\e[33m┘└┘\e[34m└─┘\e[35m┴ ┴\e[36m└─┘\e[37m┴ ┴\e[0m\n");
    return 0;
}

The mistakes were: using \r\n instead of plain \n, leaving out the m at the end of each and every escape sequence, and a number of typos in the actual letters (missing spaces and the like).

I deliberately changed sprintf(bannerN, ... to printf to make it a self-contained program instead of a fragment of a larger system, and changed the actual color codes used for each letter to make it a more interesting demo. When I run this program on my computer I get this output:

"PANGAEA" drawn with box-drawing characters, each letter in a different color

The program will only work on your computer if your terminal emulator supports both ANSI color escapes and printing UTF-8 with no special ceremony. Most Unix-style operating systems nowadays support both by default; I don't know about Windows.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • @A_J_D_H Almost certainly yes. I can think of situations where it wouldn't, but they are all rare nowadays. If it doesn't work for you, make a new post on Ask Ubuntu and tell them which terminal emulator you're using and the *complete and unedited* output of the `locale` command (run with no arguments). – zwol Jul 11 '21 at 13:15