Ads Here

Saturday, 10 March 2018

Set up Nano editor, C Compiler & running a C program on Android

Hello everyone, so, in the previous post we discussed about how to run basic linux commands on Android using an app called Termux, click here for check the post. Now, in this post we start how to install linux Nano editor and C complier on your android device and after that we write, compile and run a simple C program same like you do on your linux computer. So, let's start -
Now, when we use a text editor like Nano we need to use some key combinations that are not possible with normal touch type android keyboard. Now, here we have two method for that -
1. If you have a physical keyboard and your device support OTG then you can use that keyboard directly.
2. Download a keyboard app called CodeBoard Keyboard For Coding. Using this keyboard we can perform the key combinations directly without any physical keyboard.
After ready keyboard now first we setup Nano text editor. For that go through the following steps -
1. Open Termux
2. You must connected to internet before start. Now, simply type - pkg install nano and press enter.
3. After that it Termux will ask you for confirmation of installation, type Y and press enter.
4. It takes 5-10 seconds to complete and now you have successfully installed Nano Text editor.
Now, we install C complier. Actually, this is not only a C complier, you can compile C++ programs also if you want. Now, let's see the installation of Compiler -
1. Simply type this - pkg install clang and press enter. Similarly, this package will also ask for confirmation of your installation, simply select Y and press enter.
2. It may take a 2-3 minutes depending on your internet connection and now you have successfully installed C complier.
After installing this two packages, you are ready to code, compile and run C program on your Android device. So, let's start by simple C program -
1. Now  for write a c program with Nano type in this way - nano prog1.c and press enter.
Now, here first we given the text editor name that is Nano. After that prog1 is the the name of our program and ".c" is the extension of a c program.
2. Now, we type our c program. So, here i have created a simple program that show the sum of two numbers. Here is the code -
#include<stdio.h>
int main()
{
int a,b,s;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
s=a+b;
printf("Sum=%d\n",s);
return 0;
}
Remember that, when you give the final output you must add a \n at last for new line. If you don't use this then your terminal prompt will be shown with your output.
3. After that for save the code here i used the CodeBoard Keyboard. Now, for save text file in Nano -
First use the key combination ctrl+x. Now, type y and press enter. Now, you have successfully saved the code.
4. Now, need to compile the code. For compliance type this cc prog1.c
If your program is error free then it will be successfully compiled.
5. Now, for run the program type this- ./a.out
This will execute your program. Now, you enter two numbers and press enter for get the sum value.
So, in this way you can code & run C program in your android device. So, this is for now, don't forget to share, thank you.


No comments:

Post a Comment