简体   繁体   English

为什么我没有得到非常简单的Hello World程序的输出?

[英]Why do I get no output for very simple Hello World program?

I am wondering why my C program does not print any information. 我想知道为什么我的C程序不打印任何信息。 I am 100% new to programming, and have been able to learn a few things in Ruby and Python these past few weeks, but I am getting nowhere with the C stuff. 我是100%的编程新手,过去几周我已经能够在Ruby和Python中学到一些东西,但是我对C的东西无处可去。 Here is the total baseline, simple program that everyone learns first: 这是每个人首先学习的总基线,简单程序:

#include <stdio.h>

int main()

    {
        printf("Hello World\n");
        return 0;
    }

So I've got that written, and I save as hello.c. 所以我已经写了,我保存为hello.c。 Now I cd to the directory where it is, and then I try 现在我cd到它所在的目录,然后我尝试

gcc hello.c

and nothing happens - no errors, just nothing. 没有任何反应 - 没有错误,没有任何错误。 If I write instead 如果我写的话

gcc hello.c -o hello

a new executable file is written to my directory, and when I open it, it looks like the normal command line, and has "Hello World" written there, as I expected in my terminal the first time. 一个新的可执行文件被写入我的目录,当我打开它时,它看起来像普通的命令行,并在那里写了“Hello World”,正如我第一次在终端中所期望的那样。

I also tried make hello.c an executable itself, but when I do that I get 我也试过让hello.c成为一个可执行文件,但是当我这样做时,我得到了

syntax error near unexpected token `('
`int main()'

When you type 当你输入

gcc hello.c

and nothing happens, that is to be expected. 没有任何反应,这是可以预期的。 Or more precisely, GCC will compile an executable with the default name, which for me is a.out . 或者更确切地说,GCC将使用默认名称编译可执行文件,对我来说这是一个a.out If I were then to type ./a.out on the command line, I see the output. 如果我在命令行输入./a.out ,我会看到输出。

I think there may be a slightly bigger conceptual issue here though from your Ruby/Python background. 我认为从你的Ruby / Python背景中可能会有一个稍大的概念问题。 Ruby and Python are (usually) interpreted languages, which means that when you create a script file, you can mark it as executable and through some magic the OS will start up a program that reads the files and executes them for you. Ruby和Python是(通常)解释语言,这意味着当您创建脚本文件时,您可以将其标记为可执行文件,并且通过一些魔术操作系统将启动一个程序来读取文件并为您执行它们。 C, however, is compiled. 但是,C是编译的。 So when you run GCC, that takes your source file and turns it into an executable, with either the default or specified name. 因此,当您运行GCC时,它将获取源文件并将其转换为可执行文件,具有默认名称或指定名称。 At this point, one would not expect to see any output unless there is a problem with the compilation process. 此时,除非编译过程出现问题,否则不会期望看到任何输出。 Then you can run ./hello , which is an executable, and see your output. 然后你可以运行./hello ,它是一个可执行文件,并查看你的输出。

That also explains why you can't mark hello.c as executable and run it. 这也解释了为什么你不能将hello.c标记为可执行文件并运行它。 It needs to be compiled into an executable first. 它需要首先编译成可执行文件。 It looks like the system is pretending it's a shell script, which is isn't, and giving you a syntax error. 看起来系统假装它是一个shell脚本,它不是,并且给你一个语法错误。

gcc hello.c will generate a file named a.out gcc hello.c将生成一个名为a.out的文件
gcc hello.c -o hello will generate a file named hello gcc hello.c -o hello将生成一个名为hello的文件

These are executable files, and you need to execute/run these to get the output. 这些是可执行文件,您需要执行/运行这些文件才能获得输出。

Run these as 运行这些

./a.out or ./hello ./a.out./hello

gcc hello.c -o hello specifies that you want to create an executable named hello.exe. gcc hello.c -o hello指定要创建名为hello.exe的可执行文件。

You must compile all C files and then run the executable to run the program. 您必须编译所有C文件,然后运行可执行文件以运行该程序。 You cannot actually run the .c files. 您实际上无法运行.c文件。 When you don't specify an exe, it just compiles. 如果不指定exe,则只编译。 The fact that you have no errors is a good thing, because it means that your code is correct! 你没有错误的事实是一件好事,因为这意味着你的代码是正确的! :) :)

The first time, your executable was named "a.out" (or "a.exe" on Windows), which is the historic default name given to an executable by gcc. 第一次,您的可执行文件被命名为“a.out”(或Windows上的“a.exe”),这是gcc为可执行文件指定的历史默认名称。

You should spend some time figuring out the difference between source and executable, and between opening and executing, too... 你应该花点时间搞清楚源和可执行文件之间的区别,以及开放和执行之间的区别......

Welcome to programming, and Stack Overflow! 欢迎使用编程和Stack Overflow!

gcc hello.c

compiles the file and generates an executable file 'a.out', but does not execute it. 编译文件并生成可执行文件'a.out',但不执行它。 That is why nothing more happened on your command line. 这就是为什么你的命令行没有发生任何事情。
To start execution, you would have to type: 要开始执行,您必须输入:

./a.out

just as you did for 'hello' if i understand correctly. 正如你对“你好”的说法一样,如果我理解正确的话。
You will have to learn the difference between compilation, linking and execution if you want to program in C, find yourself a good beginner's book or tutorial, it's worth it! 你必须学习编译,链接和执行之间的区别,如果你想用C编程,找到一个好的初学者的书或教程,这是值得的!
Cheers! 干杯!

You got the 你得到了

syntax error near unexpected token `('
`int main()'

error when you set the protections on your hello.c source file and tried to run it. 在hello.c源文件上设置保护并尝试运行它时出错。

Using gcc transforms your .c source file into a binary program you can run. 使用gcc将.c源文件转换为可以运行的二进制程序。

gcc -o hello hello.c

The order I've used is not critical. 我使用的订单并不重要。 I am just used to compiling this way. 我只是习惯于编译这种方式。 You compiled this way 你用这种方式编译

gcc hello.c -o hello

which is fine. 这很好。

The successful compilation and link created an output file called hello, which is what you would run 成功的编译和链接创建了一个名为hello的输出文件,这是您要运行的

./hello

Have you tried including #include<conio.h> and include getch() before return 0; 您是否尝试过包含#include<conio.h>并在return 0;之前包含getch() return 0; to display the terminal window. 显示终端窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM