How to clear output screen in C programming Language?

Clear Output Screen using C program

Clear Output Screen - When we run a program, previous output or other command prompt/ Linux Terminal command's output appear there. We can clear the output screen using C program.

Functions which are used to clear output screen depend on the compiler, commonly used functions/methods are:

  1. Using clrscr() - For TurboC Compiler
  2. Using system("cls") - For TurboC Compiler
  3. Using system("clear") - For gcc/g++ compiler in Linux

1. Using clrscr() - For TurboC Compiler

clrscr() is a library function declared in conio.h header file. This function clears the output screen.

Consider the following program

#include <stdio.h>
#include <conio.h> /*for clrscr()*/
 
int main()
{
    int num=100;
     
    /*Use after declaration section*/
    clrscr(); /*clear output screen*/
     
    printf("value of num: %d\n",num);
     
    return 0;
}

2. Using system("cls") - For TurboC Compiler

system() is a library function of stdlib.h header file. This function is used to run system/ command prompt commands and here cls is a command to clear the output screen.

Consider the following program

#include <stdio.h>
#include <stdlib.h> /*for system()*/
 
int main()
{
    int num=100;
     
    /*Use after declaration section*/
    system("cls") /*clear output screen*/
     
    printf("value of num: %d\n",num);
     
    return 0;
}

3. Using system("clear") - For gcc/g++ compiler in Linux

system() is a library function of stdlib.h header file. This function is used to run system/ Linux Terminal commands and here clear is a command to clear the output screen.

Consider the following program

#include <stdio.h>
#include <stdlib.h> /*for system()*/
 
int main()
{
    int num=100;
     
    /*Use after declaration section*/
    system("clear") /*clear output screen*/
     
    printf("value of num: %d\n",num);
     
    return 0;
}



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.