small c programming challenge

basically wanted to know how to clear the screen of a simple c program
figured out that by creating a loop that adds 25 blank lines it has the same effect. anybody know a better way?



No, clrscr() is deprecated. It is not part of the ISO-C standard an should be absolutely avoided.

Some operating systems provide this function within conio.h though. But basically spoken it is the same to write 25 newlines to the console.



#include <graph.h>

printing 25 newlines has 2 disadvantages: The cursor ends up at the bottom of the screen (instead of the top) and if the screen is set to 50 lines then it won’t work :wink:


printf(" \033[2J");

I got this from here:

The trouble I’ve heard with that is that it’s an escape sequence - \033 is the code for esc - and it’s not the same for all terminals. I think a lot of programmers use the curses C package, which has a lot of screen functions including a clear function:

It’s mainly used under unix (usually built-in - I’ve found an implementation on my Mac OS X just searching for curses.h) but there’s a Windows version here called cursesp:

thanks for the help guys, I think for now I’ll just have to stick to the loop that makes 25 blank spaces, cuz none of the things mentioned have worked (probably cuz of stupid windows i have)

thanks for the help :smiley:

what about the windows api call to clear a console?

though… I don’t remember it at the moment, I will look for the docs later

ok was on #c and this guy told me this


it works like a charm! :smiley:

system(“cls”) only works for systems that have a shell command callled ‘cls’ that clears the screen (if someone defines/redefines cls to something else, then something else is what will happen).

The only generic call to clear a console screen for C is if you use the curses package and the user has defined the terminal type (commonly done with UNIX/Linux, seldom done with Windows).

If you’re certain your end-users are on Windows, the the cls shell command is probably the way I’d go.

Include the <windows.h> header files and make a function like this:

void clrscr() 
  COORD coordScreen = { 0, 0 };
  DWORD cCharsWritten;
  DWORD dwConSize;
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen,      &cCharsWritten);
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen,    &cCharsWritten);
  SetConsoleCursorPosition(hConsole, coordScreen);

Messy and only works with Windows, but it works.

you dirty people…

that’s hideous, I’m sure it can be done more easily than that

you still have to get a handle to the console window

I don’t remember the call exactly, poke around for it:

that’s hideous, I’m sure it can be done more easily than that[/quote]

Show me one the works with Visual C++.

z3r0 d’s post in summary:

<vK posts code, most likely copied (possibly indirectly) from msdn>
z3r0 d: OMG it’s hideous
<z3r0 d posts msdn code, that is identical apart from very basic error checking and doesn’t say how to get the console handle>
z3r0 d: wow, that’s much better

I noticed that later…

but it is still hideous the way msdn does it, printing spaces to all of the buffer as opposed to providing a call that actually does it