编译后的 c 脚本仅保持打开状态不到一秒

发布于 2025-01-05 23:19:44 字数 617 浏览 1 评论 0原文

初学者爱好程序员在这里。我习惯在 OSX 中使用 GCC 编译运行 C,但最近不得不切换到 Windows。我正在使用 Microsoft Visual Studio Express 2010 编译我的代码。编译进展顺利,但之后当我尝试运行它时,它只会闪烁打开一毫秒,然后关闭。我该如何解决这个问题?

我尝试过的所有脚本都会发生这种情况,但这里有一个特别重要的脚本,即 K&R 的经典华氏温度和摄氏温度转换器,它不起作用。 如果这是一个愚蠢的问题,抱歉。两周前刚开始学习C。

 #include <stdio.h>

    main()
   {
     int fahr, celsius;
     int lower, upper, step;
     lower = 0;     
     upper = 300;    
     step = 20;    
     fahr = lower;
     while (fahr <= upper) {
         celsius = 5 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr = fahr + step;
      }
   }

Beginner hobby programmer here. I'm used to running C in OSX compiling with GCC but I recently had to switch to windows. I'm compiling my code using Microsoft visual studio express 2010. The compiling goes fine but after that when I try to run it it only flashes open for a millisecond and then closes. How do I fix this?

This happens to all of the scripts I have tried but here is one in particular, the classic Fahrenheit and Celsius converter from K&R, that does not work.
If this is a stupid question, sorry. Just started learning C two weeks ago.

 #include <stdio.h>

    main()
   {
     int fahr, celsius;
     int lower, upper, step;
     lower = 0;     
     upper = 300;    
     step = 20;    
     fahr = lower;
     while (fahr <= upper) {
         celsius = 5 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr = fahr + step;
      }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埋葬我深情 2025-01-12 23:19:45

您引用的程序将一系列华氏温度转换为摄氏度,打印结果,然后立即退出。这就是源代码所说的:其中没有任何内容可以使其保持打开状态。

如果您在 Unix 环境中编译和运行,您很可能从(虚拟)终端内的 shell 运行它,并且当程序完成时,shell 会给出一个新的提示符,但会在其上方显示程序的输出。然而,在 Windows 上,启动程序的标准方法不涉及 shell 或终端窗口,因此程序只是将其输出写入您看不到的地方并退出(或者它打开一个终端窗口,但在它之后立即将其关闭)运行完毕)。

要在 Windows 中查看输出,您可以自己打开一个终端窗口(运行 cmd.exe)并从那里启动程序,或者您可以在末尾添加 getch() 调用来使程序退出之前等待按键。

The program you quote converts a range of fahrenheit temperatures to celsius, prints the results, and exits immediately. That's simply what the source code says: there is nothing in it to make it stay open.

If you compile and run in a Unix environment, you're most likely running it from a shell, inside a (virtual) terminal, and when the program finishes, the shell gives you a new prompt, but leaves the program's output visible above it. On Windows, however, the standard way to launch a program does not involve a shell or terminal window, so the program just writes its output somewhere you can't see and exits (or it opens a terminal window, but closes it right after it finishes running).

To see the output in Windows, you can open a terminal window yourself (run cmd.exe) and start the program from there, or you can add a getch() call at the end to make the program wait for a keypress before it exits.

心在旅行 2025-01-12 23:19:45

您可以直接从命令行而不是从 IDE 运行可执行文件,或者在 main() 末尾调用一些输入以保持窗口打开。

You can run the executable directly from a command line rather than from within the IDE, or call for some input at the end of main() to keep the window open.

呆头 2025-01-12 23:19:44

在最后一个大括号 } 之前放置一个 getch(),这将需要在程序退出之前按下按键,

这是我建议这样做而不是 ctrl-f5 是它教你另一个 C 命令:)

[编辑]
让我为您正在做的事情添加更多信息。

main 的正确签名是 int main(int argc, char **argv),这是您应该在您的程序(替换当前存在的单行 main()

您不必对这些变量(argc 和 argv)执行任何操作,它们可能未被您使用,但是,前面的程序员的存在。函数名称(main)上的 int 意味着它应该返回一个值,但是,将来,您或负责您的代码的人,这意味着您的 main 函数应该返回一些值,向底层操作系统表明它是成功还是失败(此外,如果您在 shell 中使用程序,则需要使用该值)。脚本)。

暂时,一个简单的return(0),在前面提到的 getch() 之后就可以很好地完成工作

[/编辑]

put a getch() before your final brace }, this will require a keypress before the program exits

the only reason I suggest this rather than ctrl-f5 is that it teaches you another C command :)

[edit]
Let me add a little more information to what you're doing.

The correct signature for main is int main(int argc, char **argv), this is the value you should have in your program (to replace the single line main() that is currently there.

You don't have to do anything with those variables (argc & argv), they may be unused by you, the programmer. However, the presence of the preceding int on the function name (main) means that it is expected to return a value. Again, you probably don't care. However, in the future, you or someone who is responsible for your code, will care. What this means is that your main function should return some value, something to indicate it's success or failure to the underlying operating system (also, something to be used should you employ your programs in a shell script).

For the time being, a simple return(0), after the aforementioned getch() will do the job nicely.
[/edit]

一身软味 2025-01-12 23:19:44

听起来您正在尝试使用 F5 在调试器下运行程序。成功完成后,调试器将立即退出。尝试在调试器之外运行,完成后它将暂停以等待按键: Ctrl+F5

注意:术语脚本对于 C 来说并不准确。C 是一种编译语言,因此通常称为程序与脚本相比。

It sounds like you are trying to run the program under the debugger with F5. Upon successful completion the debugger will immediately exit. Try running outside the debugger and it will pause for a key press after completion: Ctrl+F5

Note: The terminology script is not really accurate for C. C is a compiled language and hence generally referred to as programs vs. a script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文