Compile C Program in Ubuntu

In this article, we would focus on how to compile C Program in Ubuntu distribution.

C is a programming language designed by Dennis Ritchie. It was first released in the year 1972. The programming language can be implemented on multiple platforms. We would first write a basic C program and thereafter, compile it through GCC (GNU Compiler Collection).

GCC was originally an acronym for GNU C Compiler. Initially, it supported only C Programming language. But, later on we could compile other programming languages too. Some of other supported programming languages are/were Objective-C++, Objective-C, Fortran, Ada and Java, among others. Now, GCC is an acronym for GNU Compiler Collection. It is developed by GNU Project and was first released on May 23, 1987. At the time of writing the article, latest stable release for the GNU Compiler Collection is 9.2.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

Install GCC in Ubuntu

Although GCC – GNU Compiler Collection comes installed as default in Ubuntu distribution. If, for some reason, its not installed. Then, the gcc package is already available in standard Ubuntu repository.

We need to update standard Ubuntu repository first to make the latest version of the package available. Therefore, issue the following in terminal –

sudo apt update

Thereafter, gcc can be installed through build-essential package –

sudo apt install build-essential

To check for the gcc version installed –

gcc -v

Write a basic C Program

Open a text editor in terminal. Choose any of the text editor you are comfortable with – nano, vim, vi etc. Furthermore, C program file name have .c as file extension.

nano first-code.c

Next, we will write a basic C program which takes string input from the user and returns with an output.

#include <stdio.h>

int main(void)
     {
              char name[100];
              printf("Enter your name: ");
              scanf("%s", name);
              printf("Hello %s!\n", name);
              return 0;
}

Since, the primary purpose of the article is to show how to compile C programs in Ubuntu. Therefore, the above code has been explained in a separate articleYour first C program. Now, save the file and exit from the text editor.

To compile the C program, we need to run the following in terminal –

gcc -o compiled-code first-code.c

where,

-o places the output of the file – first-code.c – to compiled-code

To execute the output file –

./compiled-code

It will ask you to input your name first and display the output as instructed.

In conclusion, we have discussed how to compile C Program in Ubuntu distribution through package gcc (GNU Compiler Collection).

Similar Posts