Hello Everyone !!!
If you don't have an IDE or Software with you to run your code, then don't worry !!! We have Hulk. Oops !! Sorry that was Tony Stark's dialogue... We have Nano to Compile & Run the program.
At the end of this tutorial, you will be able to compile & run a C program using Nano Text Editor on the Terminal.
If you don't have an IDE or Software with you to run your code, then don't worry !!! We have Hulk. Oops !! Sorry that was Tony Stark's dialogue... We have Nano to Compile & Run the program.
At the end of this tutorial, you will be able to compile & run a C program using Nano Text Editor on the Terminal.
Intro
In the previous tutorial, we learned about the basic functions of Nano Text Editor using Command Line. If you haven't seen the previous tut, then click here.
I found this feature of running C program with Nano while Code Blocks Software on my Mac was continuously crashing. So, when you don't have any IDE (Integrated Development Environment), you should use Nano.
Let's Get Started
Let's open up the Terminal. We can use (Ctrl + Alt + T) shortcut to open the terminal. The terminal is a very powerful tool of the operating system.
Creating a new file
DCs-MacBook-Air:~ Dhanraj$ nano fileName.c
Here, we have created a new file with Nano Command. Also, we can open an existing file by performing the following command.
DCs-MacBook-Air:~ Dhanraj$ nano -w Home/Desktop/fileName
Editing the file
Now, you are all set up. Nano is ready to edit. So, Start typing or you can also paste your code in the Nano Text Editor.
Here, I have written a simple C program.
You can add multiple files & can edit them. At the bottom, there are some commands which are very useful while editing the file like cut, current position, help, search.
Saving the file & Exit
If you want to save the changes you have made, use (CTRL + O). Then, you can exit the editor by using (CTRL + X). If you have pressed exit (CTRL + X) without saving(CTRL + O) it, then the editor will ask you "Save newFile?". If you want to save changes & exit, then type "y" & press enter. If you don't want to save it, then type "n" & press enter. Press (CTRL + C) to cancel & continue editing.
Then, it will ask you to verify your file name. Also, you can modify your file's name here.
Then, Press Enter (Return). You will return to the main page where you were started.
Compile & Run the Program
Compile the program using GCC command. GCC stands for "GNU Compiler Collection".
DCs-MacBook-Air:~ Dhanraj$ gcc fileName.c
It will take a few seconds to compile (less than 10 seconds). After compiling, it won't show any notifications. The bash cursor will go to the next line after compiling the program successfully. If you have some errors in your code, then it will show you the errors with line numbers & specific information.
I have just corrected the error in my code. Nano is the best tool to practice code very effectively.
Now, use the "ls" command to locate our Compiler's Execution File (.out). It will show you the list of all files in the current directory.
Here, it is showing all files in my directory. We will see two files 1) Current File (.c) 2) Execution File (.out). Check the execution file name.
Now, use the following command to run the program
DCs-MacBook-Air:~ Dhanraj$ ./a.out
Don't wait !!! Press Enter & Run the program.
After running the program, a sentence is printed at the new line.
Congratulations! You have successfully compiled & run the C in the Nano Text Editor using Commands Line.
!!! Thank For Reading !!!
~ Coding Potter ~
~ Out Of Box Learning Series ~
~ Out Of Box Learning Series ~
Comments
Post a Comment