I'm trying to run my first "process" program, but I get the following error :

./fork.c: line 4: syntax error near unexpected token `('
./fork.c: line 4: `int main()'

I'm pretty sure that the code is correct:

 #include <sys/types.h>
 #include <stdio.h>
 int main() {
     pid_t pid;
     printf("Hello fork()\n");
     switch(pid=fork())  {
         case -1: printf("Error by fork().....\n"); exit(0);
         case 0: printf("I'm the child process \n"); break;
         default: printf("I'm the dad \n"); break;

What is wrong?

  • 16
    Just for the sake of your own sanity later, put a "break;" on the "case -1:" line. You'll thank yourself for it later. Also, have the child process call _exit(0), and the parent call exit(0).
    – user732
    Aug 13, 2012 at 1:22
  • 4
    @BruceEdiger Why the need for _exit? What's wrong with doing any cleanup that's been registered?
    – OrangeDog
    Aug 13, 2012 at 12:49
  • 5
    exit(0) will flush stdout and stderr. _exit(0) will not. You can end up with double outputs if there's some bytes on stdout when your program does the fork(), and the child calls exit(0). Because you're learning how fork() works why confuse yourself?
    – user732
    Aug 13, 2012 at 14:38
  • @BruceEdiger learning how fork() works includes learning that it can copy buffered output. Being a reasonably complicated system call, some confusion is probably necessary in the learning process.
    – OrangeDog
    Aug 14, 2012 at 9:36

2 Answers 2


You can't just run ./fork.c. It's not a program; it's the source for a program. Using ./ assumes that the file is a script (which it isn't) and treats it accordingly.

However, as noted in another answer, there are compilers (like Tiny C Compiler) that can execute C code without explicitly compiling it.

Since it's a C program, you have to compile the program. Try cc -o fork fork.c then ./fork; it worked here.

  • And if your on Linux, it's probably GCC, not CC.
    – Linuxios
    Aug 13, 2012 at 18:49
  • 6
    @Linuxios in most Linux systems, cc is a symbolic link to gcc.
    – Renan
    Aug 13, 2012 at 18:50
  • Good to know. Although it points to something else on systems like Slaris, right?
    – Linuxios
    Aug 13, 2012 at 18:54
  • 2
    Just checked a couple of Solaris boxes, and /usr/bin/cc links to /opt/SUNWspro/bin/cc on one and /opt/sunstudio12.1/bin/cc on the other. Regardless, it's still a C compiler.
    – OrangeDog
    Aug 14, 2012 at 9:38
  • 1
    And on an HPUX box it's /opt/ansic/bin/cc.
    – OrangeDog
    Aug 14, 2012 at 9:40

That's not a program, that's the source code for a program.

C is a compiled language, meaning it must be "compiled" into machine-readable instructions before you can run it. As you are using C, the "C Compiler" (cc) can do this.

cc -o fork for.c   # compile the code
chmod +x fork      # ensure it it executable
./fork             # run the compiled program

As you move on to more complicated programs, using multiple source files and external libraries, you'll likely move on to using the "GNU Compiler Collection" (gcc) and make to describe how to turn the source code into a working executable.

This question has various information on the difference between scripts (as you are attempting to treat your source code) and compiled programs.

  • If "compiler" is part of the name of the tool, I would call that reasonably explicit.
    – OrangeDog
    Aug 13, 2012 at 10:24
  • Just a note that the tools I named are not the only ones available, but are likely to be available by default on a GNU/Linux system and are widely used.
    – OrangeDog
    Aug 13, 2012 at 10:33
  • 4
    Regarding that "must" word, tcc (Tiny C Compiler) can execute C code without explicitly compiling it. pastebin.com/5FZiMpEn (Edited comment by re-adding it. Sorry for messing the order.)
    – manatwork
    Aug 13, 2012 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .