Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

clrscr in C

Last Updated on March 30, 2023 by Prepbytes

The C programming language provides various in-built functions for assisting the programmer. One such function is clrscr in C. In this article, we will learn more about the clrscr in C, along with its syntax, working, and examples.

clrscr in C

The clrscr in C is a built-in function that is used for clearing the screen of the console output during the execution of the C program. This function is defined in the conio.h header file. We need to include the “conio.h” file for using the clrscr in C. This function is useful in the case of the console-based program as it allows the programmer to clear the screen and starts displaying the output from the top of the screen.

Syntax of clrscr in C

The syntax of the clrscr in C is given below:

clrscr();

To use this function we need to include the stdio.h file by using the statement given below.

#include < stdio.h >

The clrscr() function is declared in the stdio.h header file, which is why it is necessary to include this header file in any program that uses this function.

Working of clrscr in C

The clrscr() function in C works by clearing the console screen (with any previous output that may have been present on the screen). It should be noted that this function does not delete any previously written data or any code from the program, it just simply clears the screen and allows the programmer in showing the new output from the top of the screen.

Examples of clrscr in C

Let us see some examples so that we can understand the concept of clrscr in C.

Example 1 of clrscr in C
The following is a simple example that shows how we can use the clrscr in C program.

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello World!");
    getch();
    clrscr();
    return 0;
}

Output:

Hello World!

Explanation:
The above C code is just printing the “Hello World!” on the console screen. We have used the printf() Function to display the output on the console. The getch() function is used to wait for the user to press a key before the screen is cleared. Then we used the “clrscr()” function which clears the screen and allows the programmer to display the next output from the top of the screen.

Example 2 of clrscr in C
Here is another example showing the usage of the clrscr in C.

Code (with clrscr Function in C):

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, k;
    clrscr();

    printf("Enter the value of i: ");
    scanf("%d", &i);
    printf("Enter the value of j: ");
    scanf("%d", &j);
    k = i + j;
    clrscr();

    printf("The value of i is %d\n", i);
    printf("The value of j is %d\n", j);
    printf("The sum of i and j is %d\n", k);
    
    getch();
    clrscr();
    return 0;
}

Input:

5
8

Output:

The value of i is 5
The value of j is 8
The sum of i and j is 13

Explanation:
In the above code, we have used clrscr() in the starring to clear the output screen( it may contain the output of the previously run code). Next we have asked the user to enter the values of two variables i and j using the scanf() Function. Then we added the values of i and j and stored them in the variable k. Then again we used the clrscr Function to clear the screen and then we protected the output. This results in the output shown above.

Let us also see what happens if we don’t use the second clrscr() function.

Code (Without clrscr in C)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, k;
    clrscr();

    printf("Enter the value of i: ");
    scanf("%d", &i);
    printf("Enter the value of j: ");
    scanf("%d", &j);
    k = i + j;
    //clrscr();

    printf("The value of i is %d\n", i);
    printf("The value of j is %d\n", j);
    printf("The sum of i and j is %d\n", k);
    
    getch();
    clrscr();
    return 0;
}

Output:

Enter the value of i: 5
Enter the value of j: 8
The value of i is 5
The value of j is 8
The sum of i and j is 13

This is the output if we don’t use the clrscr() Function in the above code.

Note: The above codes run only on the older compiler (such as Turbo C) and give errors on the modern compilers as they do not include the “stdio.h” file”.

Conclusion
In the C programming language, the clrscr() function is quite helpful. It enables the programmer to clean the console screen and start showing output from the screen’s top. This function is highly useful in console-based applications where the output has to be clean and structured.

Frequently Asked Questions (FAQs)

Here are some Frequently Asked Questions on “clrscr in C”.

Ques 1. What is the purpose of clearing the console screen?
Ans. Clearing the console screen allows for a clean slate to display the output of the program in a more organized and clear way.

Ques 2. Does the clrscr() function delete previously written data or code from the console?
Ans. No, the clrscr() function only clears the console screen and does not delete previously written data or code.

Ques 3. When should the clrscr() function be used in a C program?
Ans. The clrscr() function should be used at the beginning of the program or before displaying new output to ensure a clear screen and organized display.

Ques 4. Can clrscr() function be used in graphical user interface (GUI) applications?
Ans. No, the clrscr() function is only used in console-based applications.

Ques 5. What is the return value of clrscr() function?
Ans. The clrscr() function returns void and does not have a return value.

Ques 6. Is it necessary to include the stdio.h header file for clrscr() function to work?
Ans. Yes, the stdio.h header file must be included in the program for the clrscr() function to work.

Leave a Reply

Your email address will not be published. Required fields are marked *