Where to put the file and how to compile and run i

2019-09-21 17:28发布

问题:

I want to learn how to write a C program under Linux. Now I have installed Centos, and I'm using vim to write a 'hello world' program in the C language. But I don't know where to put it, or how to compile it? I have installed gcc. I am sorry. I am a newbie. Thank you. Could you make me an example how to write a simple program in C then test and compile it.?

回答1:

Save it anywhere, then in a terminal run

gcc path/to/file.c

That's all it takes. The default output will be a file called a.out which you can run directly from the terminal.

If you're uncomfortable with the terminal (though since you're using vim I'll assume you're not), there are many IDEs that make it even easier for beginners.



回答2:

type this in your terminal:

$ mkdir ~/learnC
$ cd ~/learnC
$ cat > hello.c 

   #include <stdio.h> 
   int main () { 
      printf("Hello World\n"); 
   } 

/* Press Ctrl+D */

$ gcc hello.c -o hello
$ ./hello


标签: c linux