C语言 如何使用 Dev-C++ 查看 C 程序的输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2366138/
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 04:42:43  来源:igfitidea点击:

How can I see an the output of my C programs using Dev-C++?

cdev-c++

提问by deeb

I'm looking to follow along with The C Programming Language (Second Addition)on a machine running Vista.

我希望在运行 Vista 的机器上跟随C 编程语言(第二次添加)

So far, I've found Dev-C++ the easiest IDE to do this in. However, I still have one problem. Whenever I run my compiled code, for example: a simple hello world program, it runs, but the console window just flickers on the screen, and I can't see the output.

到目前为止,我发现 Dev-C++ 是最容易做到这一点的 IDE。但是,我仍然有一个问题。每当我运行我编译的代码时,例如:一个简单的 hello world 程序,它都会运行,但控制台窗口只是在屏幕上闪烁,我看不到输出。

How can I see an the output of my C programs using Dev-C++? I found a C++ specific solution, System("pause"), and a really ugly C solution, whilelooping fflush(stdout), but nothing nice and pretty.

如何使用 Dev-C++ 查看 C 程序的输出?我找到了一个 C++ 特定的解决方案,System("pause")和一个非常丑陋的 C 解决方案,whilelooping fflush(stdout),但没有什么好和漂亮的。

回答by abelenky

I put a getchar()at the end of my programs as a simple "pause-method". Depending on your particular details, investigate getchar, getch, or getc

getchar()在我的程序末尾放了一个作为简单的“暂停方法”。根据您的具体细节,调查getchargetchgetc

回答by Clifford

In Windows when a process terminates, the OS closes the associated window. This happens with all programs (and is generally desirable behaviour), but people never cease to be surprised when it happens to the ones they write themselves.

在 Windows 中,当进程终止时,操作系统会关闭关联的窗口。所有程序都会发生这种情况(并且通常是可取的行为),但是当他们自己编写的程序发生这种情况时,人们永远不会停止感到惊讶。

I am being slightly harsh perhaps; many IDE's execute the user's process in a shell as a child process, so that it does not own the window so it won't close when the process terminates. Although this would be trivial, Dev-C++ does not do that.

也许我有点苛刻;许多 IDE 在 shell 中将用户进程作为子进程执行,因此它不拥有窗口,因此在进程终止时它不会关闭。尽管这很简单,但 Dev-C++ 不会这样做。

Be aware that when Dev-C++ was popular, this question appeard at least twice a day on Dev-C++'s own forum on Sourceforge. For that reason the forum has a "Read First" thread that provides a suggested solution amongst solutions to many other common problems. You should read it here.

请注意,当 Dev-C++ 流行时,这个问题每天至少在 Dev-C++ 自己的 Sourceforge 论坛上出现两次。出于这个原因,论坛有一个“阅读第一”主题,它在许多其他常见问题的解决方案中提供了建议的解决方案。你应该在这里阅读。

Note that Dev-C++ is somewhat old and no longer actively maintained. It suffers most significantly from an almost unusable and very limited debugger integration. Traffic on the Dev-C++ forum has been dropping off since the release of VC++ 2005 Express, and is now down to a two or three posts a week rather than the 10 or so a day it had in 2005. All this suggest that you should consider an alternative tool IMO.

请注意,Dev-C++ 有点旧,不再积极维护。它受到几乎无法使用且非常有限的调试器集成的最大影响。自 VC++ 2005 Express 发布以来,Dev-C++ 论坛上的访问量一直在下降,现在每周发布两到三个帖子,而不是 2005 年每天 10 个左右。所有这些都表明您应该考虑替代工具 IMO。

回答by maribeth

Use #include conio.h

#include conio.h

Then add getch();before return 0;

然后在getch();前面加return 0;

回答by Glen Everett

For Dev-C++, the bits you need to add are:-

对于 Dev-C++,您需要添加的位是:-

At the Beginning

一开始

#include <stdlib.h>

And at the point you want it to stop - i.e. before at the end of the program, but before the final }

并且在您希望它停止的时候 - 即在程序结束之前,但在最后}

system("PAUSE");

It will then ask you to "Press any key to continue..."

然后它会要求您“按任意键继续...”

回答by Johny Boy

Add this to your header file #include and then in the end add this line : getch();

将此添加到您的头文件#include 中,然后在最后添加这一行:getch();

回答by bta

The easiest thing to do is to run your program directly instead of through the IDE. Open a command prompt (Start->Run->Cmd.exe->Enter), cdto the folder where your project is, and run the program from there. That way, when the program exits, the prompt window sticks around and you can read all of the output.

最简单的方法是直接运行您的程序,而不是通过 IDE。打开命令提示符(开始->运行Cmd.exe- > ->回车)cd到项目所在的文件夹,然后从那里运行程序。这样,当程序退出时,提示窗口仍然存在,您可以阅读所有输出。

Alternatively, you can also re-direct standard output to a file, but that's probably not what you are going for here.

或者,您也可以将标准输出重定向到文件,但这可能不是您在这里要做的。

回答by Javier

You can open a command prompt (Start -> Run -> cmd, use the cdcommand to change directories) and call your program from there, or add a getchar()call at the end of the program, which will wait until you press Enter. In Windows, you can also use system("pause"), which will display a "Press enter to continue..." (or something like that) message.

您可以打开命令提示符(开始 -> 运行 -> cmd,使用该cd命令更改目录)并从那里调用您的程序,或者getchar()在程序末尾添加一个调用,直到您按下 Enter 键。在 Windows 中,您还可以使用system("pause"),它将显示“按回车键继续...”(或类似的)消息。

回答by Pengfei Liu

Add a line getchar();or system("pause");before your return 0;in main function. It will work for you.

在 main 函数之前getchar();system("pause");之前添加一行return 0;。它会为你工作。

回答by som

; It works...

; 有用...

#include <iostream>
using namespace std;
int main ()
{
   int x,y; // (Or whatever variable you want you can)

your required process syntax type here then;

您所需的流程语法类型则在此处;

   cout << result 

(or your required output result statement); use without space in getchar and other syntax.

(或您所需的输出结果语句);在 getchar 和其他语法中不带空格使用。

   getchar();
}

Now you can save your file with .cpp extension and use ctrl + f 9 to compile and then use ctrl + f 10 to execute the program. It will show you the output window and it will not vanish with a second Until you click enter to close the output window.

现在您可以使用 .cpp 扩展名保存文件并使用 ctrl + f 9 进行编译,然后使用 ctrl + f 10 执行程序。它将向您显示输出窗口,并且它不会在一秒钟内消失,直到您单击 Enter 关闭输出窗口。

回答by Amir

i think you should link your project in console mode

我认为你应该在控制台模式下链接你的项目

just press Ctrl+h and in General tab select console.

只需按 Ctrl+h 并在常规选项卡中选择控制台。