How to write and run a C program on Ubuntu?
In Ubuntu, the steps to write and run a C program are as follows:
- Open the terminal. You can quickly open the terminal by pressing Ctrl+Alt+T.
- Create a new C source file by entering the following command in the terminal, for example, hello.c:
$ touch hello.c
- Open the hello.c file with your preferred text editor (such as vi, nano, gedit, etc.) and write C program code. For example, you can use the following command to open the hello.c file with the nano editor:
$ nano hello.c
- Write your C program code in the editor. For example, you can paste the following code into a file named hello.c:
#include<stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- Save and close the file. If you are using the nano editor, press Ctrl+X, then press Y to save and exit.
- Compile a C program in the terminal. Enter the following command in the terminal to compile the hello.c file and generate an executable file:
$ gcc hello.c -o hello
- Run the executable file. Enter the following command in the terminal to run the generated executable file:
$ ./hello
- The program will display “Hello, World!” in the terminal.
These are the basic steps for writing and running C programs on Ubuntu system. You can use different text editors and compilation options to meet your needs.