Rahul Subedi Rahul Subedi - 1 year ago 109
C Question

How to clear screen from simple C program?

#include <stdio.h>
#include <cstdlib>


int a, fact;
char q, n, y;
printf("\nEnter any number ");
scanf("%d", & a);
fact = rec(a);
printf("Factorial value = %d\n", fact);
printf("do you want to exit.....(y/n):");
scanf("%s" ,&q);
if (q == 'n')
return 0;

rec(int x)
int f;
if (x == 1)
return 1;
f = x * rec(x - 1);

return f;

I'm using code blocks but I don't know how to clear the screen. I searched then found
within header file
, but it shows the error
cstdlib: no such file of directory
. What should I do ?

Answer Source

Clearing the screen is outside the purview of a normal C program. It depends on the operating system.

For windows, you should look into .

For unix, look into or .

system() always launches a sub-shell which may or may not have any effect on the environment of the parent program. You do need a system-call, but not a system() call.

I didn't always know this. I once (long ago) suggested in comp.lang.c that someone should try system("exit"); to close the window around the DOS program. But that, of course, cannot work. And I was quickly advised to test my code before posting. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download