How to run C program on Mac OS X using Terminal?

后端 未结 9 2151
渐次进展
渐次进展 2020-11-28 01:47

I am new to C. Here is my \"Hello,world!\" program.

#include 

int main(void)    
{
  printf(\"Hello, world!\\n\");
  return 0;
}
相关标签:
9条回答
  • 2020-11-28 02:13

    First make sure you correct your program:

    #include <stdio.h>
    
    int main(void) {
       printf("Hello, world!\n"); //printf instead of pintf
       return 0;
    }
    

    Save the file as HelloWorld.c and type in the terminal:

    gcc -o HelloWorld HelloWorld.c
    

    Afterwards just run the executable like this:

    ./HelloWorld
    

    You should be seeing Hello World!

    0 讨论(0)
  • 2020-11-28 02:14

    A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal. You need a compiler for that.

    Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)

    0 讨论(0)
  • 2020-11-28 02:20

    Answer is chmod 755 hello - it makes file executable... It is funny, so no-one answered it. I had same problem on MacOS, which is now solved.

    nano hello.c make hello chmod 755 hello Then you run it by ./hello

    clang --version Apple LLVM version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin15.6.0

    nothing was installed, nano make (clang) chmod - all inside MacOS already

    0 讨论(0)
提交回复
热议问题