How to compile a C program in Vim text editor

Step#1: Create a C file

Open your terminal by Ctrl+Alt+ T  and type the command mentioned below to create a C.extention file

vim demo.c

Step#2: Edit the C file

To edit the C.extension file you need to press i to insert text into the file

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello world!\n");
    return 0;
}

Step#3: Exit and save the file

To exit the C. extension file first press Esc then type the command mentioned below

:wq

You can also check, whether your stdio.h library is present or not by using the below command

cd /usr/include/

Next, you can use a list command to list all the available libraries

ls

You can also open stdio.h library by below command

vim stdio.h

As you have noticed, we have used the hello world program to print the hello world on the screen and we have used the printf() function from stdio.h library

You can also view the prototype of the function printf () from the file stdio.h

To view printf() function in the stdio.h First, press Esc then type the command mentioned below

/printf

Similarly, you can also view the remaining functions of stdio.h such as scanf()

To exit the stdio.h file

First, press Esc then type the command mentioned below

:q

Step #4: compile the program

To compile the demo.c  first, navigate to the folder where demo.c  is saved

gcc demo.c

To execute the demo.c program use the below command

./a.out

Finally, to be effective in vim environment you need to set up your vimrc file and use plugins and one such plugin is Syntastic

The Syntastic will help you to view syntax errors and you can view errors by typing the below command

:w

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.