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

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

问题描述

我试图打造为K&放一个.exe文件; R你好,世界。在这本书中给出的code是:

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 ::块(在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:

你所看到的,当您双击该文件的正常的。你的IDE做什么(保持窗口打开)是的功能的帮你调试应用程序。

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.

为什么会这样?

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

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).

如果你从一个控制台中启动程序(比如,从的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.

在另一方面,您的应用程序可以是互动的:等待用户输入,做一些事情,当用户告诉它退出只。你不能脚本这些应用中,很明显,因为你需要有一个人的present在键盘上告诉应用程序做什么。

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中运行应用程序,他们算账,通常等待一个按键preSS。这让你有机会与输出来检查窗口,以确认该应用程序是否工作正常。

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]秘籍:除非你去通过不继承的控制台应用程序。该应用程序推出的任何控制台应用程序不会继承控制台,因为继承是由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天全站免登陆