C语言 如何从简单的 C 程序中清除屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15102976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:28:15  来源:igfitidea点击:

How to clear screen from simple C program?

ccoding-styleheader

提问by Rahul Subedi

#include <stdio.h>
#include <cstdlib>

rec();

main() 
{
    int a, fact;
    char q, n, y;
    printf("\nEnter any number ");
    scanf("%d", & a);
    fact = rec(a);
    printf("Factorial value = %d\n", fact);
    printf("do you want to exit.....(y/n):");
    scanf("%s" ,&q);
    if (q == 'n')
    {
        system("cls");
        main();
    }
    else
        return 0;
}

rec(int x) 
{
    int f;
    if (x == 1) 
        return 1;
    else 
        f = x * rec(x - 1);

    return f;
}

I'm using code blocks but I don't know how to clear the screen. I searched then found system("cls");within header file #include<cstdlib>, but it shows the error cstdlib: no such file of directory. What should I do ?

我正在使用代码块,但我不知道如何清除屏幕。我搜索然后system("cls");在头文件中找到#include<cstdlib>,但它显示错误cstdlib: no such file of directory。我该怎么办 ?

回答by Karthik T

Change

改变

#include <cstdlib>

to

#include <stdlib.h>

cstdlibis a C++ header file, and thus will be unusable in C.

cstdlibC++ 头文件,因此在 C中将无法使用

回答by luser droog

Clearing the screen is outside the purview of a normal C program. It depends on the operating system.

清除屏幕超出了普通 C 程序的权限。这取决于操作系统。

For windows, you should look into conio.

对于 Windows,您应该查看conio

For unix, look into cursesor termios.

对于 unix,请查看Cursestermios

system()always launches a sub-shell which may or may not have any effect on the environment of the parent program. You doneed a system-call, but nota system()call.

system()总是启动一个子shell,它可能会或可能不会对父程序的环境产生任何影响。您确实需要一个system-call,但不需要一个system()电话。



I didn't always know this. I once (long ago) suggested in comp.lang.c that someone should try system("exit");to close the window around the DOS program. But that, of course, cannot work. And I was quickly advised to test my code before posting. :)

我并不总是知道这一点。我曾经(很久以前)在 comp.lang.c 中建议有人应该尝试system("exit");关闭 DOS 程序周围的窗口。但这当然行不通。我很快被建议在发布之前测试我的代码。:)

回答by Keith Nicholas

you have lots of problems in your code....

你的代码有很多问题......

but for the specific problem, try #include <stdlib.h>

但对于特定问题,请尝试 #include <stdlib.h>

回答by Hymanotonye

use the #include<stdlib.h>that's where the clear screen function is defined.

使用#include<stdlib.h>那是定义清除屏幕功能的地方。

回答by Mike

To use system("cls")you need the header <iostream>. This will allow all system()types to execute. Unsure if it is a C++ header file, but it works for the compiler that I use.

要使用,system("cls")您需要 header <iostream>。这将允许所有system()类型执行。不确定它是否是 C++ 头文件,但它适用于我使用的编译器。