How to compile and run C language in Ubuntu?

To compile and run a C language program on Ubuntu system, you can follow these steps:

  1. To open the terminal: you can use the shortcut key Ctrl + Alt + T to open the terminal.
  2. Write a C program: Use any text editor to create a C source code file with a .c extension, such as hello.c. For example, in the terminal, enter the following command to use the nano editor to create a file named hello.c:
nano hello.c

Next, input the following content in the editor.

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Press Ctrl + X to save and exit.

  1. GNU Compiler Collection
gcc hello.c -o hello

This will compile the hello.c file into an executable file named hello.

  1. Run the program: Enter the following command in the terminal to execute the compiled program.
./hello

You will see the output result “Hello, World!” in the terminal.

In this way, you have successfully compiled and run a C program on the Ubuntu system.

bannerAds