Skip to main content

Programming in C on Android

C is a powerful General Purpose Programming Language. Learning C could be beneficial as it is one of the basic but powerful language, there are several powerful tools which had been developed using C. The Python interpreter is written in C language known as CPython, Linux  OS was built using C, Android Kernels , MacOS and even Crunch a pentest password word list generator tool is built using C language.
So to start with C programming, a newbie must have an Integrated Development Environment(IDE).
It would be so difficult to write in a notepad. IDE can make things much simpler, makes every task like Compiling, Linking, Debugging easy to perform. Without an IDE it would be difficult for developer to mange a project.
There are several IDE's for Windows, but the two popular ones are listed below with print screen:
1. Dev C++
Dev C++












2. Codeblocks
Codeblocks

But the colleges and Universities prefer using Borland Turbo C++
Borland Turbo C++

But when we come to Android Devices. There are very few IDE's that follow C rules of Dennis Ritchie. Hence, this blog focuses on how to start programming on Android devices in C. 
There are several ways to install Borland Turbo C++ IDE on Android. But in this post, we'll use Termux (an Android terminal emulator and Linux environment app that works directly with no rooting or setup required). 
Screenshot
Welcome Screen

Installation:

1. Install Termux from Play Store.

2. On first start, Termux will take to start as dependencies are being installed.
      Note: Do not interrupt while installing on first start.
3. After you get the screen as shown in screenshot. Type or Copy-Paste the Command in Termux below.
     Note: During Installation of any script here, if you are prompted 'Do you want to continue[Y/n]', Enter Y and press Enter/Send on keyboard.
    $ apt-get update
Output should be something like this

4. For installing  vim(editor) for terminal. Use
    $ pkg install vim

vim had been successfully installed 
5. The last step for installation is to install the clang(compiler).
    $ pkg install clang
clang after installation
The Installation is complete.

Now, we must know how to use these applications/tools.

How to use:

Before starting you should be aware of basic terminal commands, some of them are as follows

 i.  make directory:

     Syntax: mkdir directory_name
     example: mkdir C_Programs

 ii. list: 

     This command is used to list of files in the directory in which we are working.
     example: ls   
     (this will print all the files in current directory)

iii. change directory:

     Syntax: cd directory_name
     example: cd C_Programs
     (this will change my directory which we have created named as C_Programs)

iv. remove file:

     Syntax: rm file_name
     example: rm Hello.C
     (Hence, Hello.C  C file will be removed)

 v. touch command:

     This command will create a empty file.
      Syntax: touch file_name
      example: touch temp.c
      (Self Explanatory)

vi. vim commands:      

:e filenameOpen filename for edition
:wSave file
:qExit Vim
:q!Quit without saving
:xWrite file (if changes has been made) and exit
:sav filenameSaves file as filename
.Repeats the last change made in normal mode

Steps:

1. Create a directory in terminal.(This step can be skipped)

     using mkdir command.

     $ mkdir C_Programs
     
    
    ls command can be used to view the directory.

2. Changing directory to C_Program:

    $ cd C_Programs

/ is optional 

3. Creating an empty c file:

    $ touch temp.c
The empty file can be viewed using ls command

4. Using vim editor to write a C program:

   $ vim temp.c
Press Send/Enter/Return key to edit and write a program

5. Editing the file:

After Editing
Empty C File

    To Save the edited file, Press Esc, then type :x to save and exit the file.

6. Compiling The file:

    To compile the file use gcc command followed by the filename then -o(output file)  followed by <output_filename>
    Syntax: gcc file_name -o ouput_file_name
     
     $ gcc temp.c -o temp

File has been successfully compiled 

  If you encounter any error there's is something wrong with the program.
  You can view compiled file using  ls command.
temp green colored file is the compiled file

7. Executing the compiled file:

    To execute the compiled file simply type ./filename
    Syntax: ./file_name
    Example:./temp
  
Successfully Executed..
Explore C in Termux.

Comments

Popular posts from this blog

How to get started with C Programming

Getting Started with C There are several ways to start with any programming language. But in this post I'm gonna tell you how I started C as my first programming language. To get started with any language you must know why you need to learn that language. You can google to learn about "why do we need C language" . After you're done googling. you've must have learnt various reasons why do we need C.

SSH Server on Android

  How to create a SSH server on Android Termux-SSH: Termux is one of the best ways to use SSH and perform many more other operations on Android. It is one of the powerful application for android. We can use Termux to open a SSH terminal that can be accessed over LAN using Termux-SSH a python script or using OpenSSH.