How to write C program in Termux?

To write and compile a C program in Termux, you will need to install a C compiler and text editor. The gcc the compiler is commonly used to compile C programs and the nano text editor is a good choice for writing and editing code in Termux.

Here is an example of how to write and compile a C program in Termux:

  1. Install the gcc compiler and nano text editor by running the following command:
pkg install gcc nano
  1. Create a new file for your C program using the nano text editor. For example, to create a file named “hello.c”, you can use the following command:
nano hello.c

This will open the nano text editor with an empty file named “hello.c”.

  1. Type the C code for your program into the file.
  2. Save the file and exit the text editor by pressing, then Y, and then Enter.
  3. Compile the C program using the gcc compiler. For example, to compile the “hello.c” program, you can use the following command:
gcc hello.c -o hello

This will compile the “hello.c” program and create an executable file named “hello”.

  1. Run the program by typing its name and pressing Enter. For example, to run the “hello” program, you can use the following command:
./hello

This will execute the “hello” program and display the output.

You can use the gcc compiler with various options to customize the compilation process. For example, you can use the -o option to specify a different name for the executable file, or the -O option to enable optimization. To view a full list of available options, you can use the gcc --help command.

Leave a Comment

Your email address will not be published. Required fields are marked *