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

后端 未结 9 2150
渐次进展
渐次进展 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 01:54

    To do this:

    1. open terminal

    2. type in the terminal: nano ; which is a text editor available for the terminal. when you do this. something like this would appear.

    3. here you can type in your C program

    4. type in control(^) + x -> which means to exit.

    5. save the file by typing in y to save the file

    6. write the file name; e.g. helloStack.c (don't forget to add .c)

    7. when this appears, type in gcc helloStack.c

    8. then ./a.out: this should give you your result!!
    0 讨论(0)
  • 2020-11-28 01:56

    Working in 2019 By default, you can compile your name.c using the terminal

     cc name.c
    

    and if you need to run just write

     ./name.out
    
    0 讨论(0)
  • 2020-11-28 02:04

    1) First you need to install a GCC Compiler for mac (Google it and install it from the net )

    2) Remember the path where you are storing the C file

    3) Go to Terminal and set the path

    e.g- if you have saved in a new folder ProgramC in Document folder

       then type this in Terminal
        cd Document
        cd ProgramC
    

    4) Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)

    5) Now Compile your program

       make Hello
       ./hello
    
    0 讨论(0)
  • 2020-11-28 02:09

    First save your program as program.c.

    Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How to find App Store? Do a "Spotlight Search" by typing Space and start typing App Store and hit Enter when it guesses correctly.

    App Store looks like this:

    Xcode looks like this on App Store:

    Then you need to install the command-line tools in Terminal. How to start Terminal? You need to do another "Spotlight Search", which means you type Space and start typing Terminal and hit Enter when it guesses Terminal.

    Now install the command-line tools like this:

    xcode-select --install
    

    Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:

    gcc -Wall -o program program.c
    

    Note: On newer versions of OS X, you would use clang instead of gcc, like this:

    clang program.c -o program
    

    Then you can run it with:

    ./program
    Hello, world!
    

    If your program is C++, you'll probably want to use one of these commands:

    clang++ -o program program.cpp
    g++ -std=c++11 -o program program.cpp
    g++-7 -std=c++11 -o program program.cpp
    
    0 讨论(0)
  • 2020-11-28 02:11

    to compile c-program in macos simply follow below steps

    using cd command in terminal go to your c-program location
    then type the command present below
    make filename
    then type
    ./filename

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

    For compiling a c program on your latest macOS just type the following in terminal after saving the file with a .c extension and on reaching the path where the file is saved :

    cc yourfilename.c

    Once you have checked all the errors after compilation (if any), type the following for executing the code :

    ./a.out

    These commands are tested on macOS Mojave and are working perfectly fine, cheers coding!

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