0

I am incredibly new to the C language: I am trying to run sample C programs from codecademy, such as the hello world command below:

#include <stdio.h>

int main() {
  // output a line
  printf("Hello World!\n");
}

Bottom line is, every time I try to run any code in my macOS terminal, I always get the following error:

zsh: parse error near `\n'

What can I do to resolve this problem?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • https://stackoverflow.com/q/2603489/4788546, https://www.google.com/search?q=compile+c+file+linux&rlz=1C1CHBD_enRO881RO881&oq=compile+c. [\[SO\]: LNK2005 Error in CLR Windows Form (@CristiFati's answer)](https://stackoverflow.com/a/34777349/4788546) explains the process (but on *Win*). – CristiFati Jan 13 '23 at 18:07
  • 1
    because you are trying to run the code in zsh instead of compile it with a C compiler – user253751 Jan 13 '23 at 18:08

1 Answers1

1

is a language where you need to compile the code you've written. You do that by starting a C compiler and give the file containing your C code as input.

Example:

File: myfirstcprogram.c

#include <stdio.h>

int main(void) {
    printf("Hello World!\n");
}

Then at the prompt, invoke the compiler:

cc myfirstcprogram.c -o myfirstcprogram

-o myfirstcprogram is here an argument to the compiler telling it what to call the final program.

cc may be clang or gcc or any other compiler you've got installed if cc isn't already linked to the proper compiler.

When the compilation is done, the executable myfirstcprogram should have been created. You can now run it from your prompt:

./myfirstcprogram

You can run it without recompiling it as many times as you like. Only when you change the source code (myfirstcprogram.c) do you need to compile the program into an executable again.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108