How to compile and run a C program in Linux

C Programming language is a well-known programming language because of its rich library of functions. C program does not execute without a compiler in Linux. Therefore a dedicated compiler is needed to compile programming languages in Linux distribution. In this post, we will learn what C programming is and how it is used to compile C programs in Linux.

How C Program is run in Linux

C program code is first compiled by a compiler which converts the strings-based code into machine code that can be readable by a computer machine. In Linux, the most common compiler used for C programming is GCC compiler and is available in the default repository of the many distributions of Linux that can be installed easily using the apt package manager:

$ sudo apt install gcc

Once the GCC package has been installed, now it’s time to create a file using the nano editor with the name myfile.c, (“.c” is the extension that tells the computer that this file contains a C program):

$ nano myfile.c

Write the simple code in the C language of printing “Hello Linux Hint! ”:

#include <stdio.h>

Int main(){

             printf(”Hello Linux Hint World !\n”);

             return 0;

}

In the above code, we included a header file the stdio, which is used to include input and output related information, the main() is the function of the program, printf is used to print the output, and return 0 is the exit status.

Press CTRL+S to save the file and exit the editor by pressing CTRL+X. List down the contents of the file by using the ls command to verify the creation of the file:

$ ls

The file is being successfully created, to compile the file using the GCC compiler, run the following command:

$ gcc myfile.c -o myfile

In the above command the gcc is the compiler that compiles the file which was created with the name of myfile.c and then checks whether it has an error or not, and if there is no error, then the “myfile” (binary file) will be generated in the same directory. To execute the binary file use:

$ ./myfile

In the above output, we have seen that the output of our C program has been displayed.

Conclusion

In this era of Information technology, everyone is keen to learn coding. C programming is recommended to beginners from which they can start learning and besides this, C programming is a general-purpose language that can be used not only for learning the basics of programming but also on advanced level development of applications. In this post, we learned how the GCC compiler is used in Linux to compile the C program. Other than the terminal, there are different compilers like Visual Studio that are GUI(Graphical User Interface) based which can be installed on Linux to compile and run C programs in Linux.



from https://ift.tt/3dl7BoX

Post a Comment

0 Comments