Clear Console in C



In C, we have various methods to clear the console or output screen and one of them is clrscr() function. It clears the screen as function invokes. It is declared in "conio.h" header file. There are some other methods too like system (cls) and system ("clear") and these are declared in "stdlib.h" header file.

Syntax

Following are the list of syntaxes to clear the console in C programming language as follows:

clrscr();

Or,

system("cls");

Or,

system("clear");

Let's say we have "new.txt" file with the following content:

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

Example to Clear Console

Following example shows the usage of clear console in C.

#include <stdio.h>
#include <conio.h>

void main() {
   FILE *f;
   char s;
   clrscr();
   
   f=fopen("new.txt","r");
   while((s=fgetc(f))!=EOF) {
      printf("%c",s);
   }
   
   fclose(f);
   getch();
}

The above program produces the follwing result:

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

Explanation

Here, we have a text file named "new.txt". A file pointer is used to open and read the file. It is displaying the content of file. To clear the console, clrscr() is used.

clrscr();
f=fopen("new.txt","r");
while((s=fgetc(f))!=EOF) {
   printf("%c",s);
}
Updated on: 2025-04-14T19:13:13+05:30

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements