system(“clear”)
like clrscr() function system(“clear”) also used to clear console screen in C. This function can be found in the stdlib.h header file. This method is useful in Linux operating system to clear the console screen. whenever the system(clear) function invokes the program it calls the system shell then a clear operation gets performed.
It is supported by almost all online compilers and GCC compilers whereas clrscr() won’t support it.
Properties :
- Linux specific
- Supported by all modern compilers.
- Make the software more readable.
Syntax :
System("clear")
Parameter :
This function doesn’t accept any parameters.
Return Type :
This function doesn’t return any value.
Program Code :
Let’s take an example that display welcome message.
Prerequisites :
- Run this code online compiler . you can find online compiler on google easily.
C
//Program to clear screen using system("clear"); #include<stdio.h> #include<stdlib.h> int main() { printf ( "Welcome To w3wiki\n" ); getchar (); system ( "clear" ); printf ( "A computer science portal " ); } |
Output :
Welcome To w3wiki A computer science portal
Explanation :
Here, we’ve displayed two welcome message . we also used getchar() method accept string which make user to press enter key . when user press enter key previous screen get cleared and displayed “A computer science portal” message.
How to clear console in C language?
It is one of the basic need a program may required i.e clear the console during execution time.
Need of a clear console screen :
A console screen must be cleared once its stated objectives have been completed. A user also needs a clear screen for new data. Like any other language, C offers a variety of ways to clear the console screen.
The clear screen method or function not only depends on types of operating systems but also compilers, which include modern as well as older compilers
There is function named clrscr() which is included in conio.h and is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.
So what should we use there?
There are two more ways to clear console:
- By Using system(“clear”)
- By using a regex “\e[1;1H\e[2J”
- by using clrscr() function
- System(“cls”)
The following are some of the factors that influence a console’s clear screen:
- Type of operating system.
- User compiler new or old.
- Need of the user.
Prerequisites :
- Some clear screen methods are platform specific. They won’t run on other systems or compilers.
- If you are using a visual studio then prefer the system.(“cls”)
Contact Us