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:
- Install the
gcc
compiler andnano
text editor by running the following command:
pkg install gcc nano
- 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”.
- Type the C code for your program into the file.
- Save the file and exit the text editor by pressing, then
Y
, and thenEnter
. - 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”.
- 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.