Running C or C++ program in Linux
Information Technology December 4th, 2009Steps to run C program in Ubuntu:
1. Go to Applications > Accessories > Terminal, to open terminal window.
2. Type the following in command line
sudo gedit first.c
3. Enter the code bellow in the editor.
/* Program first.c */
#include<stdio.h>
main()
{
printf(”Hello World.\n”);
}
4. Save the file and close to return to terminal window.
5. Firstly compile the code using the following command
cc -c first.c
That would produce an object file you may need to add to the library.
6. Then create an executable using the following command
cc -o first first.c
7. Now run this executable using the following command
./first
8. Output should show as follows
Hello World.
Steps to run C++ program in Ubuntu:
1. Go to Applications > Accessories > Terminal, to open terminal window.
2. Type the following in command line
sudo gedit first.cpp
3. Enter the code bellow in the editor.
/* Program first.cpp */
#include<iostream>
main()
{
std::cout<<”Hello World.”<<std::endl;
}
4. Save the file and close to return to terminal window.
5. Now compile the code using the following command
g++ first.cpp -o second
6. Then run this executable using the following command
./second
7. Output should show as follows
Hello World.
A sample factorial program in C
/* Factorial using C */
#include<stdio.h>
int main()
{
int i,n,f=1;
printf(”\nEnter the number: “);
scanf(”%d”,&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(”\nFactorial of %d is %d \n\n”,n,f);
}
Output:
$ sudo gedit factorial.c
$ cc -c factorial.c
$ cc -o factorial factorial.c
$ ./factorial
Enter the number: 6
Factorial of 6 is 720
May 4th, 2010 at 4:50 PM
Hello, I have been viewing your site for some time now and read most of your entries. Is there any way that I can subscribe so I get updates sent to my email? Regards
May 4th, 2010 at 11:50 PM
You can get updates by subscribing feeds http://www.biogem.org/feed
May 26th, 2010 at 1:47 PM
Recently, I did not give whole lot thought to commenting on weblog articles and have left feedback even less. Viewing your useful piece of writing, might inspire me to do this again.
May 27th, 2010 at 12:31 AM
Hi. I wanted to drop you a quick note to express my thanks. I’ve been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you’ve structured your site. I am attempting to run my own blog but I think its too general and I want to focus more on smaller topics. Being all things to all people is not all that its cracked up to be.
June 1st, 2010 at 5:14 AM
Cool stuff, but the sidebar don’t format properly on my Mac…maybe you could check that out. Thanks, anyway.