如何在C语言中清除控制台

1,230 阅读5分钟

在程序的执行过程中,开发者需要清除屏幕或删除以前的输出以获得新的输出。要在c语言中清除控制台屏幕,有许多方法可用。

下面是其中的一些方法。

  1. Clrscr()函数
  2. System("clear")函数
  3. System("cls")函数

重要提示 :

这个函数取决于操作系统的类型、编译器和其他因素。
例如。如果你试图在现代编译器中使用clrscr()函数来编译程序,那么它将产生一个错误。这些错误是 "Function is not declared "或 "conio. h file not found "等。
所以建议在他们指定的编译器中使用这个函数。

1.Clrscr()函数。

Clrscr()是c语言中的一个库函数。它用于清除控制台的屏幕。它将光标移到控制台的左上方。Clrscr()函数与conio.h头文件一起使用。

每当一个函数被调用时,它就会清除控制台屏幕。要使用这个函数,用户可以在主函数或任何定义了它的函数中调用clrscr()。

注意:

Clrscr()不是一个标准库函数。它是conio.h [console input output header file]头文件中的预定义函数。所以它只用于清除旧编译器如Turbo C或C++中的控制台。

优势。

  1. 使执行速度加快。
  2. 在老式编译器的MS-DOS控制台屏幕中很有用。

缺点。

  1. 对现代编译器没有用。
  2. 只在窗口系统中可用。
  3. 这个功能是可选的。
  4. 只对turbo c编译器起作用。
  5. 每次用户都必须包含一个conio.h文件。

语法。

Void clrscr (void);
Or
Clrscr();
参数:
Void:它是一个没有返回数据类型的函数。
clrscr():清除屏幕的函数
返回类型:
没有任何返回类型,因为它使用void函数。

例子。

//Program to clear screen in Modern complier like Gcc
//Run in vscode
#include<stdio.h>
//include conio.h header file
#include<conio.h>
int main()
{
    int a,b,sum;
    clrscr(); //clear screen
    printf("Enter No 1\n"); //This statement will input no 1
      scanf("%d",&a);
    printf("Enter No 2\n"); //This statement will input no 2
      scanf("%d",&b);
    sum=a+b;
    printf("Sum Of Two Number=%d",sum); //This statement will sum of number
}

输出

Enter no1 : 20
Enter no2 : 20 
Sum = 40

解释 :

在这个程序中,我们计算并打印两个数字的总和。在声明两个数字后,我们必须调用clrscr()函数。我将清除之前的输出屏幕。当我们第一次执行这个程序时,它将打印数字的总和,当我们第二次运行这个程序时,它将清除之前的输出,只显示当前的输出。
如果我们不使用clrscr(),它将打印新的输出和旧的输出,像这样

Enter no1 : 20
Enter no2 : 20 
Sum = 40Enter no1 : 10
Enter no2 : 10 
Sum = 20 

注意 :

这在Dec C++ Complier上是行不通的。使用cleardevice()函数。

2.system("clear")

第二个清除控制台屏幕的方法是linux中的clear(),
顾名思义它是用来清除控制台屏幕的。其中system()是一个库函数,在stdlib.h头文件中可用。

语法。

System.("clear")
参数
System:用于运行命令提示符命令的系统
Cls:清除输出屏幕或控制台屏幕。

优势:

  1. 对Linux和macOS操作系统有用。
  2. 对现代编译器有用,如Linux中的GCC/gcc++。

劣势

  1. 只限于特定的操作系统或编译器。
  2. 只对Linux有用。

例子 :

//Program to clear screen in Linux
#include<stdio.h>
//include stdlib header file
#include<stdlib.h>
int main()
{
    printf("Hello"); //This statement will print Hello
    getchar();
    system("clear");   // This statement will clear previous output.
    printf("World "); //This statement will print World
}

输出

Hello

清除输出后

World

解释:

在给定的程序中,我们使用了system("clear")函数来清除屏幕。 在第一步中,它打印了 "Hello",Getch将等待接受一个字符,并且不在屏幕上回声。
然后system("clear")将清除之前的输出,并在控制台中打印下一条语句World。

3.system.cls()

Cls()函数是用来清除控制台屏幕的,就像clrscr()一样。其中system()是在stdlib.h [标准库]头文件中可用的一个库函数。

语法。

System.cls()

参数。

System:用于运行命令提示符命令,也可以等待用户输入或按下键来终止程序。
Cls:清除输出屏幕。

优势:

  1. 对现代编译器如GCC有用。
  2. 对窗口有用。
  3. 对Turbo C编译器有用

劣势 :

  1. 这只适用于窗口系统。

例子:

//Program to clear screen in Modern complier like Gcc
//Run in vscode
#include<stdio.h>
//include stdlib header file
#include<stdlib.h>
int main()
{
   printf("Hello"); //This statement will print Hello
   getchar();
   system("cls");   // This statement will clear previous output.
   printf("World "); //This statement will print World
}

输出。

Hello_

清除输出后

world

解释:

在给定的程序中,我们使用了system("cls")函数来清除屏幕。在第一步,它打印了 "hello",getch将等待接受一个字符,并不在屏幕上回显。
然后system("cls")将清除之前的输出,并在控制台打印下一条语句World。

应用:

清理控制台屏幕。
显示输出的方式。

检查你的理解

问题

以下哪项不是用来清除控制台屏幕的?

getch()

clrscr()

system("cls")

system("clear")

Getch不是用来清除控制台屏幕的。它是用来接受一个字符并不在屏幕上回显。

问题

在旧的编译器(如Turbo C)中,要清除控制台屏幕,应该使用哪个函数?

getch()

Clrscr()

clrscr()

system("clear")

clrscr()是用来清除像Turbo C这样的老编译器的控制台屏幕的。另外,C语言是区分大小写的,所以它认为Clrscr()和clrscr()是不同的术语。

通过OpenGenus的这篇文章,你一定对如何在C语言中清除控制台有了完整的认识。