In this we can discuss how to compile and run a C Program in Ubuntu
Normally C Compiler is installed in Ubuntu by default. If you are not able to find the compiler we can start discussing from installing Compiler.
Step #1 : You have to install build-essential package. build-essential package will install C libraries to Ubuntu.
for that open terminal and type :
you can open terminal by "Ctrl + Alt + T " or find it from your Dashboard
Step #2 : Create a folder for storing your C Programs (Optional). But for better understanding, it is good to create a New Folder.
You can create Folder different ways, use anyway that is comfortable for you.
Open up terminal and type : "mkdir C" (This will create a Folder named 'C')
Then navigate to newly created folder by issuing following command
Step #3 : Create the C source code with any of the text editors available in Ubuntu. You can use gedit or other text editor and save the source code in the folder that you created earlier.
You must save the file with extension .c
Save the file as first.c
Step #4 : Compile Your first c program in Ubuntu
* in terminal
but as a better programmer, it is better to create an executable with following code
* in terminal type:
This will create an executable file named "first" in respected folder itself.
Step #5 : Run Your first C Program with the executable that you created
* in terminal type:
Hope you got what i was trying to convey.
Normally C Compiler is installed in Ubuntu by default. If you are not able to find the compiler we can start discussing from installing Compiler.
Step #1 : You have to install build-essential package. build-essential package will install C libraries to Ubuntu.
for that open terminal and type :
you can open terminal by "Ctrl + Alt + T " or find it from your Dashboard
sudo apt-get install build-essentials
Step #2 : Create a folder for storing your C Programs (Optional). But for better understanding, it is good to create a New Folder.
You can create Folder different ways, use anyway that is comfortable for you.
Open up terminal and type : "mkdir C" (This will create a Folder named 'C')
Then navigate to newly created folder by issuing following command
cd c
Step #3 : Create the C source code with any of the text editors available in Ubuntu. You can use gedit or other text editor and save the source code in the folder that you created earlier.
You must save the file with extension .c
#include<stdio.h>
main()
{
printf("\n My First C Program In Ubuntu \n");
return 0;
}
Save the file as first.c
Step #4 : Compile Your first c program in Ubuntu
* in terminal
cc -c first.cthis will create an object file with default name "a.out".
but as a better programmer, it is better to create an executable with following code
* in terminal type:
cc -o first first.c
This will create an executable file named "first" in respected folder itself.
Step #5 : Run Your first C Program with the executable that you created
* in terminal type:
./firstResult :
My First C Program In Ubuntu
Hope you got what i was trying to convey.
Compile and run the program in a single step:
ReplyDeletegcc first.c -o first && ./first
Thanks for the Info. But it is not a good way to Run and Compile at one go ....
DeleteThere is nothing wrong with the method that I have mentioned. If the program doesn't compile then the run portion after the && will not execute.
DeleteThanks Bro For your info :)
Delete