Home  >  Article  >  Backend Development  >  How to clear console in C language?

How to clear console in C language?

王林
王林forward
2023-09-23 21:57:041509browse

How to clear console in C language?

There are several ways to clear the console or output screen, one of which is the clrscr() function. It clears the screen when the function is called. It is declared in the "conio.h" header file. There are some other methods such as system("cls") and system("clear"), which are declared in the "stdlib.h" header file.

The following is the syntax to clear the console in C language:

clrscr();
OR
system(“cls”);
OR
system(“clear”);

The following is an example of clearing the console in C language:

Suppose we have a file called " new.txt" file with the following content:

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

Now, let us look at an example.

Example

#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();
}

Output

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo
In the above program, we have a text file "new.txt". Open and read the file using the file pointer. It displays the contents of the file. To clear the console, use clrscr()
clrscr();
f=fopen("new.txt","r");
while((s=fgetc(f))!=EOF) {
   printf("%c",s);
}

The above is the detailed content of How to clear console in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete