×

clrscr in C

clrscr function in C

clrscr stands for:

clr = clear

scr= screen

When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program, user inputs, error messages etc) screen is cleared or deleted.

Basically the clrscr(); function clears the terminal (console) window in which you are currently working and places the cursor (pointer) to the top left position of the window.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen.

*The use of the clrscr() function in C is always optional but it should be placed after variable or function declaration only.

Why use the clrscr function in C?

If you go on running the program several times, the output of the program gets longer for each execution, as the output of the previous program remains on the console window, which makes it cumbersome.

This can be avoided using the clrscr() function.The clrscr() function must be used in the program main() body. It is always used after the variable declaration instructions.

What it does is, everytime we execute our program, only the output of the current execution will be visible on the terminal window. All the previous outputs which were displayed earlier will be cleared.

clrscr() function simply clears all existing content on the console. You can understand the difference in a much better way by just running your code, once using clrscr() and once without clrscr().

Program: clrscr in C

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 5, y = 15;
int sum = 0;
clrscr();           // using clrscr() after variable declaration
sum = x+y;
printf("Sum is equals to: %d",sum);
getch();


}

Output :

Sum is equals to: 20

Note:

clrscr() is a function used by Borland Turbo C, which is an old IDE(Integrated Development Environment) in MS DOS and Windows platform. Due to old habits and persistent use of common textbooks in our curriculum, the use of this obsolete compiler and IDE  still exists, especially among many Indian and South Asian developers communities.

Hence, many new compilers may throw errors while implementing this function. It is better to use any IDE that supports clang or GCC.

* People may face errors like undefined reference to `clrscr'

which is bound to happen to some compilers because new compilers may not consist of the implementation of this function.

Summing up:

There are several methods to clear the console or output screen and one of them is the clrscr() function.

It clears the screen(terminal) as function invokes. It is declared in the “conio.h” header file and must be implemented in the main() body. The use of the clrscr() function is optional but should be placed after variable or function declaration only.

There are some other methods too like system(“cls”) and system(“clear”) and these are declared in the “stdlib.h” header file. They are also used to clear the console.

Here is the syntax to clear the console in C language,

clrscr();    //clrscr function


OR


system(“cls”);  


OR


system(“clear”);
x

Related Topics

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.