How do you clear the console screen in C?
Well, C doesn't understand the concept of screen. So any code would fail to be portable. Maybe take a look at conio.h or
curses, according to your needs?
Portability is an issue, no matter what library is used.
Clearing output of a terminal program in Linux C
There're several ways to delete the DDDDDDDDDDDDDD
- Print backspace several times
printf("\b");
- Print carriage-return and then print something to override the original line
printf("\r");
- If you are in a newline. You may use terminal commands to move your cursor
such as printf("\033[8;5Hhello"); // Move to (8, 5) and output hello
Other commands:
printf("\033[XA"); // Move up X lines;
printf("\033[XB"); // Move down X lines;
printf("\033[XC"); // Move right X column;
printf("\033[XD"); // Move left X column;
printf("\033[2J"); // Clear screen
...
- Don't forget ncurses
It is the best ways to control the exact layout and format in a terminal
Clearing terminal in Linux with C++ code
These are ANSI escape codes. The first one (\033[2J
) clears the entire screen (J
) from top to bottom (2
). The second code (\033[1;1H
) positions the cursor at row 1
, column 1
.
All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033
is the C-style octal sequence for the escape character.
See here for the full roadshow.
How do I clear the screen in C?
The best way to clear the screen is to call the shell via system(const char *command)
in stdlib.h:
system("clear"); //*nix
or
system("cls"); //windows
Then again, it's always a good idea to minimize your reliance on functions that call the system/environment, as they can cause all kinds of undefined behavior.
how to clear my previous output in Linux terminal using C?
Anyway here is the correct code. Thanks to Mobius and Dmitri.
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < TIMELIMIT; ++i) {
j = i;
printf("Loading: %i%%", ++j);
printf("\r");
fflush(stdout);
sleep(1);
}
return(0);
}
How to clear screen in both windows and linux in C
Try using this instead:
system("clear | cls")
You could also try using two pipes (||
) instead of one (|
), like this:
system("clear || cls")
Clearing output of a terminal program Linux C/C++
You can have the desired result both for terminal and pipes if you remember to remove the control characters as well. This is hardcoded for two lines.
#include <stdio.h>
int
main ()
{
fputs("output1\n",stdout);
fputs("output2\n",stdout);
fputs("\033[A\033[2K\033[A\033[2K",stdout);
rewind(stdout);
ftruncate(1,0); /* you probably want this as well */
fputs("output3\n",stdout);
fputs("output4\n",stdout);
return 0;
}
How do I clear the console in BOTH Windows and Linux using C++
Short answer: you can't.
Longer answer: Use a curses library (ncurses on Unix, pdcurses on Windows). NCurses should be available through your package manager, and both ncurses and pdcurses have the exact same interface (pdcurses can also create windows independently from the console that behave like console windows).
Most difficult answer: Use #ifdef _WIN32
and stuff like that to make your code act differently on different operating systems.
Related Topics
Lvalue to Rvalue Implicit Conversion
C++11 Equivalent to Boost Shared_Mutex
Compute Median of Values Stored in Vector - C++
Do All Pointers Have the Same Size in C++
Using Quaternions for Opengl Rotations
How to Handle Arbitrarily Large Integers
Sfinae Works Differently in Cases of Type and Non-Type Template Parameters
Case Insensitive String Comparison C++
How to Connect MySQL Database Using C++
Cmake: Include Library Dependencies in a Static Library
Uninitialized Pointers in Code
Most Efficient/Elegant Way to Clip a Number