About escape sequence in c [duplicate] - c

\f is said to be the form feed. \t is a tab, \a is a beep, \n is a newline. What exactly is a form feed - \f? The following program
#include <iostream>
int main()
{
std::cout << "hello\fgoodbye" << std::endl;
}
prints hello then a female sign (an upside down holy hand grenade:) and then goodbye all on one line.

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

From wiki page
12 (form feed, \f, ^L), to cause a
printer to eject paper to the top of
the next page, or a video terminal to
clear the screen.
or more details here.
It seems that this symbol is rather obsolete now and the way it is processed may be(?) implementation dependent. At least for me your code gives the following output (xcode gcc 4.2, gdb console):
hello
goodbye

If you were programming for a 1980s-style printer, it would eject the paper and start a new page. You are virtually certain to never need it.
http://en.wikipedia.org/wiki/Form_feed

It comes from the era of Line Printers and green-striped fan-fold paper.
Trust me, you ain't gonna need it...

Although recently its use is undefined, a common and useful use for the form feed is to separate sections of code vertically, like so:
(from http://ergoemacs.org/emacs/emacs_form_feed_section_paging.html)

It's go to newline then add spaces to start second line at end of first line
Output
Hello
Goodbye

Related

Can I change the text color through sprintf in C?

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?
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:
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.

Eclipse; Escape Sequences don't work?

I am doing a basic C tutorial. In an example this code was given to introduce escape sequences:
#include <stdio.h>
int main()
{
printf("This is a \"sample text\"\n");
printf("\tMore text\n");
printf("This is getting overwritten\r");
printf("By this, another sample text\n");
printf("The spa \bce is removed.\n");
return 0;
}
The console output is expected to look like this:
This is a "sample text"
More text
By this, another sample text
The space is removed.
Instead, I get this:
This is a "sample text"
More text
This is getting overwritten
By this, another sample text
The spa ce is removed.
I am using Eclipse Cpp Oxygen on Windows and the Cygwin toolchain to compile und run the code. I don't know what I'm doing wrong and I thought I'd ask here for help.
The console built in to Eclipse does not support the \r, \b (and \f) characters.
There is a long standing bug 76936 for this which has been open for 14 years. But doesn't look like being fixed.
In linux you example works exactly as you expect. Probably in windows the \r is considered like \n.
Instead on linux terminal the \r put (correctly) the cursor on the first char of the row.

How to display output on different screen in c?

I want to display output on different screen, but how?
For example:
I want to display " hello world " on the first black screen, and when I press enter button, the old screen will disappear and another new black screen will display "thank you" .
Just how to do that in c?
Depending on what you want but just clear the screen after getting an enter.
puts("\033[2J"); will work on most terminal using VT100 escape codes.
clrscr() is a nonstandard function defined in <conio.h> that some older compilers use.
system("cls"); works with Windows and system("clear"); with Linux and is in <stdlib.h>.
These options would cover most environments that you would encounter when using C.
This is platform-dependent.
In Unix you have to call the corresponding APIs, Qt or GTK, to create a new terminal console.
In Windows you have to call WinAPI such as CreateConsole and such.
It would be easier if you only want to clear your current console. Using ASCII CSI escape sequence works on most Unix & Linux systems, just
printf("\x1B[2J");
Or in Windows, call
system("cls");
There's actually a more native approach which calls FillConsoleOutputCharacter and FillConsoleOutputAttribute, which introduces extra code that I'm not talking about here.

Bolding text in console output

For extra credit, the professor wants us to use bolding and/or underlining to text output in the current project.
The example he gave was b\bb o\bo l\bl d\bd is displayed as b o l d
Following that example, I marked up SPACE as
printf("\033[7mS\bSP\bPA\bAC\bCE\E- move forward one page\033[0m");
I'm also implementing reverse video by enclosing strings within \033[7m and \033[0m fields. The reverse video inverts the colors of the line appropriately, but doesn't seem to be affecting the bolding, since both strings with and without the reverse video are not bolding.
Could it be the standard shell used in Ubuntu 10.10 that is at fault?
I agree about using curses, but given your starting point ....
I think you want to use the 'bright' feature of VT100 for the bold, ESC[1m
You can probably find better doc on VT100 codes, but using this page I found the codes. ANSI/VT100 Escape Codes
I hope this helps.
Unless you're just trying to be masochistic, try using curses (or ncurses) instead.
// warning: Going from distant memory here...
curs_attron(A_INVERSE); // maybe A_REVERSE? I don't remember for sure.
curs_addstr("SPACE - move forward one page");
curs_attroff(A_INVERSE);

How can I see an the output of my C programs using Dev-C++?

I'm looking to follow along with The C Programming Language (Second Addition) on a machine running Vista.
So far, I've found Dev-C++ the easiest IDE to do this in. However, I still have one problem. Whenever I run my compiled code, for example: a simple hello world program, it runs, but the console window just flickers on the screen, and I can't see the output.
How can I see an the output of my C programs using Dev-C++? I found a C++ specific solution, System("pause"), and a really ugly C solution, while looping fflush(stdout), but nothing nice and pretty.
I put a getchar() at the end of my programs as a simple "pause-method". Depending on your particular details, investigate getchar, getch, or getc
In Windows when a process terminates, the OS closes the associated window. This happens with all programs (and is generally desirable behaviour), but people never cease to be surprised when it happens to the ones they write themselves.
I am being slightly harsh perhaps; many IDE's execute the user's process in a shell as a child process, so that it does not own the window so it won't close when the process terminates. Although this would be trivial, Dev-C++ does not do that.
Be aware that when Dev-C++ was popular, this question appeard at least twice a day on Dev-C++'s own forum on Sourceforge. For that reason the forum has a "Read First" thread that provides a suggested solution amongst solutions to many other common problems. You should read it here.
Note that Dev-C++ is somewhat old and no longer actively maintained. It suffers most significantly from an almost unusable and very limited debugger integration. Traffic on the Dev-C++ forum has been dropping off since the release of VC++ 2005 Express, and is now down to a two or three posts a week rather than the 10 or so a day it had in 2005. All this suggest that you should consider an alternative tool IMO.
Use #include conio.h
Then add getch(); before return 0;
The easiest thing to do is to run your program directly instead of through the IDE. Open a command prompt (Start->Run->Cmd.exe->Enter), cd to the folder where your project is, and run the program from there. That way, when the program exits, the prompt window sticks around and you can read all of the output.
Alternatively, you can also re-direct standard output to a file, but that's probably not what you are going for here.
For Dev-C++, the bits you need to add are:-
At the Beginning
#include <stdlib.h>
And at the point you want it to stop - i.e. before at the end of the program, but before the final }
system("PAUSE");
It will then ask you to "Press any key to continue..."
Add this to your header file #include
and then in the end add this line : getch();
You can open a command prompt (Start -> Run -> cmd, use the cd command to change directories) and call your program from there, or add a getchar() call at the end of the program, which will wait until you press Enter. In Windows, you can also use system("pause"), which will display a "Press enter to continue..." (or something like that) message.
Add a line getchar(); or system("pause"); before your return 0; in main function.
It will work for you.
;
It works...
#include <iostream>
using namespace std;
int main ()
{
int x,y; // (Or whatever variable you want you can)
your required process syntax type here then;
cout << result
(or your required output result statement); use without space in getchar and other syntax.
getchar();
}
Now you can save your file with .cpp extension and use ctrl + f 9 to compile and then use ctrl + f 10 to execute the program.
It will show you the output window and it will not vanish with a second Until you click enter to close the output window.
i think you should link your project in console mode
just press Ctrl+h and in General tab select console.
When a program is not showing or displaying an output on the screen, using system("pause"); is the solution to it on a Windows profile.
The use of line system("PAUSE") will fix that problem and also include the pre processor directory #include<stdlib.h>.
Well when you are writing a c program and want the output log to stay instead of flickering away you only need to import the stdlib.h header file and type "system("PAUSE");" at the place you want the output screen to halt.Look at the example here.The following simple c program prints the product of 5 and 6 i.e 30 to the output window and halts the output window.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
a=5;b=6;
c=a*b;
printf("%d",c);
system("PAUSE");
return 0;
}
Hope this helped.

Resources