How can I write and run C language on Linux?
The steps for writing and running a C program on a Linux system are as follows:
- Open the terminal and use the command line tool to navigate to a directory where you want to save your C language program.
- Create a new C language source file using a text editor (such as vi, nano, etc.), for example, hello.c. The command is as follows:
- create a new file named hello.c using the nano text editor
- Write C language program code in the open editor. For example, you can input the following code:
- The code includes the standard input-output library and has a main function that prints “Hello, World!” and returns 0.
- Save and exit the editor.
- Compile a C program using a C compiler (such as gcc) in the terminal. The command is as follows:
- Compile the hello.c file using the gcc command and name the output file hello.
- This will create an executable file named hello.
- Run the executable file. Here is the command:
- execute the hello program
- “Hello, World!” will be output on the terminal.
In this way, you have successfully written and executed a simple C language program.