If you are a beginner in c programming. Then the first thing you need to know is how to compile a simple c program ("hello world") in your operating system and to execute it. It is more beneficial to do c programming on linux OS. I am using Ubuntu 11.10.
C programming is very very interesting programming language.
C language is called mother of all languages.
Do you know interesting thing about Ubuntu. It already contain a compiler for you to compile c program (i.e. "gcc"). "GNU's compiler collection.
so What you have to do is follow these steps : -
- STEP 1. Start your Ubuntu OS.
- STEP 2. Open up the Terminal. (use 'Ctrl' + 'Alt' + 'T') i.e. press these keys together. A violet colored window will open. Here you can type various commands. In windows we use console.
- STEP 3. (type source code and save it). Now on the terminal type (in the front of $ symbol) . "gedit hello.c" without quotes & press enter. "gedit" is a program or an editor where you can write the source code for any language. Now we will type our C program here.
type the following soure code
#include
<stdio.h>
main(){
printf("Hello World\n");
}
save file with name "hello.c"
- STEP 4: Now switch back to the terminal and Press ('Ctrl' + 'C') together to exit from the current process.
- STEP 5: Now type "gcc hello.c" without quotes. It will your source code and generate an executable file with name "a.out"
- STEP 6: Now to execute the "a.out" file. You type the following command at the Terminal. "./a.out" (without quotes) and press Enter key. You will get the output Hello World at the terminal. If you are successful in doing so you have setup a system for doing your c programming.
--------------------Alternate way to compile----------------
you can use "cc" c compiler too, for compiling your c source code.
But before that your need to install it. You can use the following command to install this compiler --
sudo apt-get install build-essential
type and press enter key. It will ask for root/admin password enter that password and press enter.
Now it will download the build-essential package from internet and install it in your OS (operating System).
So after installation type following command at terminal.
cc hello.c
it will compile your c source code and generate the executable file "a.out".
youc can execute it by using the following command.
./a.out [press enter]
Hello World
------------------Another options to be used with gcc and c---------------------------
gcc -c hello.c
It will compile your c source code and generate a hello.o object file. you can use this file to save it in your library and use it in the future.
gcc -o dell hello.c
It will create a executable file with name "dell" (you can give use name instead of using dell like "hello, first, a.out too etc.), you can use this file to see the output. You should type following command at terminal for execution-
./dell and press the enter key.
Note:-
You can use the "ls" command to list the various files in the current directory.
ls Press enter key
gcc -o dell hello.c
It will create a executable file with name "dell" (you can give use name instead of using dell like "hello, first, a.out too etc.), you can use this file to see the output. You should type following command at terminal for execution-
./dell and press the enter key.
Note:-
You can use the "ls" command to list the various files in the current directory.
ls Press enter key
That's it, Have fun.
-----------------Happy Programming------------------
-----------------Happy Programming------------------
Comments
Post a Comment