1.11M Members

How do I clear the screen?

 
0
 

First off, C does not know anything about the I/O device you are using. It can be a keyboard and monitor, or a file, or a network connection, or anything which can do I/O. What that means is that there is no standard way to do this in C.

That said, the following should work on any Unix/Linux platform and on any version of Windows. That's because it cheats by using another program to clear the screen.

All Unix/Linux systems have a command line program called "clear" which clears the screen.
Likewise, all Windows systems have a command line command called "cls" to do the same.

This works by first trying one, and if that doesn't work, then using the other. The order in which they are tried does not matter.

#include <stdlib.h>  /* system() */
#include <stdio.h>   /* getchar(), printf() */

void clearscreen() {
  if (system( "clear" )) system( "cls" );
  }

int main() {
  // Clear the screen and print a friendly message
  clearscreen();
  printf( "Hello world!\n" );

  // Wait for the user to press the enter key
  printf( "Press ENTER to continue..." );
  while (getchar() != '\n');

  return 0;
  }
 
0
 

i got it

 
0
 

Keep in mind that this is useful only for homework problems or other simple software.

If you are writing commercial software, or secure systems, you should know better than to use this, as it is resource heavy and it is a huge, gaping security hole.

Perhaps I should post an update for how to do this the Right Way.

 
0
 

That is a good solution to the problem but a linker error keeps coming up when i try to run the code. It says "unidentified symbol_system in mocule" any one knows what the problem could be?

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post:
Start New Discussion
Tags Related to this Article