为什么我的程序的输出在Windows中闪烁和关闭? [英] Why does my program's output flash and close in Windows?

查看:164
本文介绍了为什么我的程序的输出在Windows中闪烁和关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为K& RHello,world构建一个.exe文件。书中给出的代码是:

I'm trying to build an .exe file for the K&R "Hello, world". The code given in the book is:

#include <stdio.h>  
main()  
{  
    printf("Hello, world!\n");  
}  

从Code :: Blocks运行(在Windows XP下),我得到带有hello world消息的提示窗口。它保持打开,直到我手动关闭它。但是,当我双击.exe文件,提示只是闪烁和消失,为什么?

When I build & run from Code::Blocks (under Windows XP), I get the prompt window with the "hello world" message. It stays open until I close it manually. However, when I double click the .exe file, the prompt just flashes and disappears, why is that?

推荐答案

明确告诉你这一点,所以我会:

No one is explicitly telling you this, so I will:

双击文件时看到的是正常

What you see when you double-click the file is normal. What your IDE does (keeping the window open) is a feature to help you debug the application.

为什么这么做是为了帮助您调试应用程序。 ?

Why is this so?

由于您正在开发一个控制台应用程序,因此您的应用程序必须有一个控制台才能显示其输出。如果没有,则会创建一个新的控制台(这是黑色窗口)。

Since you're developing a console application, there has to be a console for your application to display its output on. If there is none yet, a new console is created (which is the black window).

如果从控制台启动程序$ c> cmd.exe ),它将继承父级的控制台,而不创建新的[1]。

If you launch your program from inside a console (say, from cmd.exe), it will just inherit the console of the parent without creating a new one[1].

最后一个应用程序使用控制台退出(在第一种情况只是你的程序),控制台关闭。你会注意到这一点,控制台应用程序只打印一个帮助文本时,没有参数运行。

After the last application using the console exits (which, in the first case is just your program), the console closes. You will notice this all the time for console applications that print nothing but a help text when run without parameters. If you double-click them from explorer, a black window with some text will flash and then immediately close.


  • 有时候,一个文本框的黑色窗口会闪烁,然后立即关闭。程序,做某事,他们立即关闭是你想要的。例如,您可以从脚本调用这些应用程序。

  • Sometimes, a program that does something and them immediately closes is what you want. For example, you can call these applications from scripts.

另一方面,您的应用程序可以是交互式的:等待用户输入,做某事,只有当用户告诉它时退出。你不能脚本化这些应用程序,显然,你将需要一个人的礼物在键盘上告诉应用程序做什么。

On the other hand, your application could be interactive: waiting for user input, doing some thing, and only exiting when the user tells it to. You cannot script these applications, obviously, as you will need to have a human present at the keyboard to tell the application what to do.

现在我们来到IDE部分:假设你正在开发第一类应用程序,一个做某事,然后立即关闭。它不是非常方便的屏幕闪烁和消失,每次你运行它,因为你怎么知道程序是否工作?假设你可以从它生成的输出中告诉这个。

Now we get to the IDE part: let's say you're developing an application of the first kind, one that does something and then immediately closes. It's not very convenient to have the screen flash and disappear every time you run it, because how can you tell if the program worked? Assuming you can tell this from the output it generates.

你当然可以从命令行窗口开始运行应用程序,但是程序将单独执行IDE,您将失去实时调试功能。

You could of course start a command-line window and run the application from there, but the program would execute separately from the IDE, and you would lose live debugging capabilities.

因此,IDE制造商想出了一个控制台应用程序的功能:当您直接从IDE运行应用程序时,之后,通常等待按键。这将允许您使用输出检查窗口,以确认应用程序是否正常工作。

So, IDE makers came up with a feature for console applications: when you run the application directly from your IDE, they afterwards, usually waiting for a keypress. This gives you the opportunity to inspect the window with the output, to confirm that the application is working properly.

[1 ] Esoterica:除非你经历一个不继承控制台的应用程序。由该应用程序启动的任何控制台应用程序将不继承控制台,因为GUI应用程序打破了继承。例如, start.exe 这样做。比较:

[1] Esoterica: unless you go through an application that does not inherit the console. Any console app launched by that application will not inherit the console, since the inheritance was broken by the GUI app. For example, start.exe does this. Compare:

foo.exe (inherits the console)
start foo.exe (start.exe is a GUI app, so foo.exe is launched in a new console)

这篇关于为什么我的程序的输出在Windows中闪烁和关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆