-2

I am new to C and am trying to figure out how do I run my code.

#include<stdlib.h>
#include<stdio.h>

int main()
{
  printf("Hello World!");
  return 0;
}

I just want to run this simple Hello World program but I don't know how to. I am using sublime if that helps.

m0meni
  • 16,006
  • 16
  • 82
  • 141
user3064228
  • 81
  • 1
  • 3
  • 8
  • 7
    maybe check this out: [How to run C program on Mac OS X using Terminal?](http://stackoverflow.com/q/32337643/1248974) – chickity china chinese chicken Jan 06 '17 at 20:26
  • Go to the directory the file is saved in in terminal, then run `make [filename]` then run `./[filename]` – Eli Sadoff Jan 06 '17 at 20:26
  • Also, you don't need to include stdlib – Eli Sadoff Jan 06 '17 at 20:27
  • ... in this case. Because you don't rely on anything declared therein. – John Bollinger Jan 06 '17 at 20:27
  • @JohnBollinger Sorry, that's what I meant. Obviously you need to for some programs. – Eli Sadoff Jan 06 '17 at 20:28
  • This might be better asked on [Apple.SE](http://apple.stackexchange.com) since getting a C compiler on OS X is very Mac specific. – Schwern Jan 06 '17 at 20:30
  • @Schwern cc and c99 aren't parts of POSIX? – Amin Negm-Awad Jan 06 '17 at 20:56
  • @AminNegm-Awad Huh? I think you're making a very narrow reading of the literal question title and none of the rest of their question. They're asking how to run a C program, but don't know what a compiler is. Presumably they come from a background of interpreted languages and so expect something like "ruby program.rb" or even some IDE thing. OS X does not come with a C compiler and it has to be installed in a specific way. Installing a compiler, using the compiler, and running the resulting program are all parts this complete breakfast a complete answer. – Schwern Jan 06 '17 at 21:28
  • I responded to your comment, not to the Q. You do not have to install Xcode to get a C compiler under macOS. You can install it the "usual" way. Installing Xcode is the easiest way and therefore the recommended way. The explanation is shorter than your last comment. After installing it you can use it the POSIX way. – Amin Negm-Awad Jan 07 '17 at 05:13

1 Answers1

1

Use the C compiler from terminal in Mac, like:

cc -o outputFileName inputFileName.c

The output file is executable, so you can double click it or in the terminal simply provide the path to the file. If your working directory is already holding that outputFileName (check with ls -al) then you can type:

./outputFileName

The program will then execute.

Phantom Photon
  • 768
  • 2
  • 10
  • 20
  • 1
    Please always advise people add `-Wall`. It staves off a whole bunch of otherwise obvious "why isn't my program working" questions. – Schwern Jan 06 '17 at 20:29
  • 2
    I'd add `-Wextra` too. – John Szakmeister Jan 06 '17 at 20:31
  • @Schwern This was *not* the Q. Without `-Wall` the standard is applied. However, one should add `-Werror`, too. – Amin Negm-Awad Jan 06 '17 at 21:00
  • 1
    Please add the command to start the program. – Amin Negm-Awad Jan 06 '17 at 21:02
  • 1
    @AminNegm-Awad - indeed it was the question. It is always the question. If it's not the question, then it's still the question. – KevinDTimm Jan 06 '17 at 21:27
  • @KevinDTimm I'm not that religious. But I'm surprised that remembering all the questions programming language and compilers in general and C in particular ask us, these hundreds and thousands of questions one can have, that there is a church of -Wall. (And obviously there is another confession -Wextra.) – Amin Negm-Awad Jan 07 '17 at 05:02